希望有大佬能教我别的方法,我明前只能想到这个方法啦。多谢!
书上的源代码是
Intent intent = new Intent("com.example.weiru.broadcasttest.MY_BROADCAST") ;
sendOrderedBroadcast(intent,null);
但是这样在安卓8上面行不通,我在前一篇博文已经处理过了,如下
Intent intent = new Intent("com.example.weiru.broadcasttest.MY_BROADCAST") ;
intent.setComponent(new ComponentName("com.example.weiru.broadcasttest",
"com.example.weiru.broadcasttest.MyBroadcastReceiver")) ;
sendOrderedBroadcast(intent,null);
我以为这样就可以在别的APP里接收这个广播了,结果是错的。用安卓7就可以实现了。但是安卓8实现不了。经过了好久的脑力挣扎。最终弄出来如下的代码:
Intent intent = new Intent("com.example.weiru.broadcasttest.MY_BROADCAST") ;
Intent intent2 = new Intent("com.example.weiru.broadcasttest.MY_BROADCAST") ;
intent.setComponent(new ComponentName("com.example.weiru.broadcasttest",
"com.example.weiru.broadcasttest.MyBroadcastReceiver")) ;
intent2.setComponent(new ComponentName("com.example.weiru.boradcasttest2",
"com.example.weiru.boradcasttest2.AnotherBroadcastReceiver")) ;
sendOrderedBroadcast(intent,null);
sendOrderedBroadcast(intent2,null);
这样子就可以让另一个APP也收到广播啦。开心!