Reputation: 521
Statically linked broadcast receiver works fine for
But when I am trying to register this dynamically then it doesn't work, Any 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.");
}
}
}
Upvotes: 0
Views: 343
Reputation: 521
Need to add
filter1.addDataScheme("file"); which will look 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);
Upvotes: 1