静态链接的广播接收器适用于
但是当我试图动态注册它然后它不起作用,有什么建议吗?
@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.");
}
}
}
答案 0 :(得分:1)
需要添加
filter1.addDataScheme( “文件”);看起来像
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);