Android - BroadcastReceiver's onReceive crashing from Intent?

久未见 提交于 2019-12-13 01:12:33

问题


So I'm trying to start a new activity from my onReceive function in a `BroadcastReceiver, yet I seem to be crashing. Without further adieu here is the code:

public void onReceive(Context context, Intent intent) {     

    //... other stuff that's not relevant

    Intent j = new Intent(context, myClass.class);  
    context.startActivity(j); 
    //If I comment the above two lines out and replace with a Toast, the toast shows up
}

Thoughts?

Edit - did some more testing, and I can start this activity from other places using the same kind of intent. I just can't do it from the BroadcastReceiver...

Thanks.


回答1:


Try this:

Intent j = new Intent(context, myClass.class);
j.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(j); 


来源:https://stackoverflow.com/questions/6404397/android-broadcastreceivers-onreceive-crashing-from-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!