监听SD卡的状态
public class MyReceiver extends BroadcastReceiver {
public static final String MOUNT = "android.intent.action.MEDIA_MOUNTED" ;
public static final String UNMOUNT = "android.intent.action.MEDIA_UNMOUNTED" ;
public static final String REMOVE = "android.intent.action.MEDIA_REMOVED" ;
@Override
public void onReceive (Context context, Intent intent) {
Log.e("onReceive" , "被执行" );
String action = intent.getAction();
if (MOUNT.equals(action)) {
Log.e("SD卡状态" , "被挂载" );
} else if (UNMOUNT.equals(action)) {
Log.e("SD卡状态" , "被卸载" );
} else if (REMOVE.equals(action)) {
Log.e("SD卡状态" , "被移除" );
}
}
public MyReceiver () {
}
}
<receiver
android:name =".MyReceiver"
android:enabled ="true"
android:exported ="true" >
<intent-filter >
<action android:name ="android.intent.action.MEDIA_MOUNTED" />
<action android:name ="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name ="android.intent.action.MEDIA_REMOVED" />
<data android:scheme ="file" />
</intent-filter >
</receiver >