android - Dynamic broadcast receiver registration doesnt work for Intent MEDIA_MOUNTED -
statically linked broadcast receiver works fine for
- android.intent.action.media_mounted
- android.intent.action.media_unmounted
but when trying register dynamically doesn't work, suggestion ?
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.d( tag, "oncreate" ); cardreceiver = new cardreceiver(); filter1 = new intentfilter(); filter1.addaction(intent.action_media_mounted); filter1.addaction(intent.action_media_unmounted); this.getapplicationcontext().registerreceiver(cardreceiver, filter1); } @override protected void ondestroy() { super.ondestroy(); log.d( tag, "ondestroy" ); this.getapplicationcontext().unregisterreceiver(cardreceiver); } public class cardreceiver extends broadcastreceiver { private static final string card_log = "isdcard"; @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if(action.equals("android.intent.action.media_mounted")){ log.v(card_log, "sd card mounted."); } else if(action.equals("android.intent.action.media_unmounted")){ log.v(card_log, "sd card unmounted."); } } }
need add
filter1.adddatascheme("file"); like
cardreceiver = new cardreceiver(); filter1 = new intentfilter(); filter1.addaction(intent.action_media_mounted); filter1.addaction(intent.action_media_unmounted); filter1.adddatascheme("file"); this.getapplicationcontext().registerreceiver(cardreceiver, filter1);