I am trying to create a broadcast receiver that receives MOUNT & UNMOUNT & EJECT of sd card. My device has 2 external directories which are /storage/sdcard0 and second one(this is what I want to listen) /storage/sdcard1.
I tried some approachs those are not working.
android: how to listen to "sd card removed unexpectedly"
listener for sd-card removal
Also here is my code :
<!-- 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)){
}
}
}
My device is 4.4.2 version and booted device.
I do not want to check anytime If Sd Card exists or not