我正在尝试创建一个接收MOUNT&的广播接收器。 UNMOUNT& EJ卡的EJECT。我的设备有2个外部目录, / storage / sdcard0 ,第二个(这是我想听的) / storage / sdcard1。
我尝试了一些不起作用的方法。
android: how to listen to "sd card removed unexpectedly"
listener for sd-card removal
这里还有我的代码:
<!-- in manifest -->
<receiver android:name=".system.receiver.SDCardBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
</intent-filter>
</receiver>
//this is my java code
public class SDCardBroadcastReceiver extends BroadcastReceiver {
private static final String MOUNTED = "android.intent.action.MEDIA_MOUNTED";
private static final String UNMOUNTED = "android.intent.action.MEDIA_UNMOUNTED";
@Override
public void onReceive(Context context, Intent intent) {
Log.e("SDCARD",intent.getAction());//THIS NEVER TRIGGERS!!
if(intent.getAction()!=null && intent.getAction().equals(UNMOUNTED)){
}else if (intent.getAction()!=null && intent.getAction().equals(MOUNTED)){
}
}
}
我的设备是4.4.2版本和启动设备。
如果Sd卡存在或不存在,我不想随时检查