Broadcast receiver Activity
2015-02-27 12:15
120 查看
If you want to catch a broadcasted intent on an Activity, you may get the following error:
02-22 08:18:46.874: E/AndroidRuntime(276): java.lang.RuntimeException:Unableto instantiate receiver com.helloandroid.broadcasttest.BroadcastTestActivity$MyBroadcastReceiver:
...
java.lang.InstantiationException:com.helloandroid.broadcasttest.BroadcastTestActivity$MyBroadcastReceiver
...
02-22 08:18:46.874: E/AndroidRuntime(276): Caused
by:java.lang.InstantiationException:com.helloandroid.broadcasttest.BroadcastTestActivity$MyBroadcastReceiver
This is because you can't instantiate a receiver in an inner class.
Instead of inner receiver, you can manually instantiate a broadcast receiver yourself in the activity.
private BroadcastReceiver myBroadCastReceiver =new BroadcastReceiver()
{
@Override
public void onReceive(Context context,
Intent intent )
{
Log.d( "Broadcastreceiver: " + intent.getAction() + "
package: "+intent.getPackage() + " @" + System.currentTimeMillis() );
}
};
No need to set this receiver in the manifest xml file, register it in the activity's onresume method and unregister in the onpause:
public void onResume() {
super.onResume();
....
registerReceiver(myBroadcastReceiver, newIntentFilter("your.custom.BROADCAST"));
}
public void onPause() {
super.onPause();
...
unregisterReceiver(myBroadcastReceiver);
}
...
}
Thats all, the receiver will catch the broadcasts, if the activity is on the screen.
To broadcast custom intents, use the following method:
Intent broadCastIntent = new Intent();
broadCastIntent.setAction( "your.custom.BROADCAST" );
broadCastIntent.setPackage("com.helloandroid.broadcasttest" );
ApplicationObject.applicationContext.sendBroadcast(broadCastIntent );
Log.d( "Broadcast sent" );
The setPackage() method set an explicit application package name that limits the components the Intent will resolve to.
相关文章推荐
- Service 通过 BroadcastReceiver与Activity 进行通信
- Android有四大组件:Activity、Service、Broadcast Receiver、Content Provider。
- Android初学习 - 在BroadcastReceiver中启动Activity的问题
- (转)Android:Activity/Service/Broadcast Receiver/Content Provider
- android四大组件(总结)activity、service、content provider、broadcast receiver
- activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver解释
- Android零基础开发专题五:Activity、BroadcastReceiver、Service、Content Provider
- Android Activity& service&Broadcast receiver 生命周期
- 15_Android四大组件:Activity 、Service、Broadcast receiver 、Content provider
- Android 知识点(Activity,Service,BroadcastReceiver)
- 在BroadcastReceiver中启动Activity
- android 在基类activity中注册BroadcastReceiver,子activity类实现响应
- Unable to start receiver com.demo.floatwindowdemo.OpenActivityBroadCast: android.util.AndroidRuntime
- activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver解释
- 在BroadcastReceiver中启动Activity,需为Intent添加Flag:Intent.FLAG_ACTIVITY_NEW_TASK
- broadcastreceiver中显示dialog以及重启当前activity
- activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver解释
- Android 编程五要诀:Activity、Service、BroadcastReceiver、ContentProvider、Intent
- activity BroadcastReceiver Service 三大组件的启动或触发
- 简要解释一下activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver