• 监听SD卡状态


    第一步,创建一个广播接收者,

     SDReceiver   extends BroadcastReceiver ; 
    第二步,在AndroidManifest.xml配置文件中注册广播接收者与配置监SD卡状态发生变化时对应的Action:
      <receiver android:name="com.itheima.sdlistener.SDReceiver">
                <intent-filter >
                    <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                    <action android:name="android.intent.action.MEDIA_REMOVED"/>
                    <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
                    <data android:scheme="file"/>
                </intent-filter>
     </receiver>
    第三步,重写广播接受者中OnReceiver方法:
    public void onReceive(Context context, Intent intent) {
         
    //判断收到的是神马广播
    //获取广播中的action
    String action = intent.getAction();
     
    if(Intent.ACTION_MEDIA_MOUNTED.equals(action)){
    Toast.makeText(context, "sd卡就绪", 0).show();
    }
    else if(Intent.ACTION_MEDIA_REMOVED.equals(action)){
    Toast.makeText(context, "sd卡被拔出了", 0).show();
    }
    else if(Intent.ACTION_MEDIA_UNMOUNTED.equals(action)){
    Toast.makeText(context, "sd卡被卸载了", 0).show();
    }
     }
  • 相关阅读:
    Redis
    Maven总结
    spring知识点总结
    网上好文搜集整理
    python 代码删除空目录
    plantUML使用指南
    python的基础操作
    八卦基础编程学习
    python历年入坑记录大全
    python实现的百度云自动下载
  • 原文地址:https://www.cnblogs.com/SoulCode/p/6393370.html
Copyright © 2020-2023  润新知