下面的动态广播接收器测试并在Android工作室模拟器API 25(Android N)上工作但未能在API 26(Android O)上接收。
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(getClass().getName(),intent.getAction());
}
};
IntentFilter intent = new IntentFilter();
intent.addAction(Intent.ACTION_MEDIA_MOUNTED);
intent.addAction(Intent.ACTION_MEDIA_EJECT);
intent.addDataScheme("file");
registerReceiver(mReceiver, intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
有谁可以指出它有什么问题?