首页 > 代码库 > BroadcastReceiver之SD的挂载监听

BroadcastReceiver之SD的挂载监听

首先,新建一个类,继承于BroadcastReceiver,然后去配置Manifest.xml这就不用说了,

注意配置Manifest.xml时候的一些细节

必须加上<data android:scheme="file"/>

1 <receiver android:name=".SdReceicer">
2             <intent-filter>
3                 <action android:name="android.intent.action.MEDIA_MOUNTED"/>
4                 <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
5                 <data android:scheme="file"/>
6             </intent-filter>
7         </receiver>

然后简单了两句判断就行了

 1 public class SdReceicer extends BroadcastReceiver {
 2     @Override
 3     public void onReceive(Context context, Intent intent) {
 4         String action = intent.getAction();//得到执行的是哪个action
 5         if ("android.intent.action.MEDIA_MOUNTED".equals(action)){
 6             Toast.makeText(context,"SD卡加载了",Toast.LENGTH_SHORT).show();
 7         }else if ("android.intent.action.MEDIA_UNMOUNTED".equals(action)){
 8             Toast.makeText(context,"SD卡被卸载了",Toast.LENGTH_SHORT).show();
 9         }
10     }
11 }

 

BroadcastReceiver之SD的挂载监听