StartActivity in android broadcast receiver

2019-05-13 19:26发布

问题:

I registered receiving SMS broadcast in manifest.xml. How can I start new Activity in receive() method of the broadcast. is there any flags of Intent to set or anything?

回答1:

use FLAG_ACTIVITY_NEW_TASK like this

@Override
public void onReceive(Context context, Intent intent)   {

    Intent i = new Intent(context, AlarmDialog.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);    
}