How to start an Intent if context is not Activity Context but Application Context

后端 未结 2 1635
情书的邮戳
情书的邮戳 2020-12-17 08:26

I\'m trying to start an activity from a class that extends BroadcastReceiver.

public void onReceive(Context context, Intent intent) {

the p

相关标签:
2条回答
  • 2020-12-17 08:53

    Here is sample code how to call another activity using context, set flag as per your requirement:

    public void onReceive(Context context, Intent intent) { 
    
      Intent startActivity = new Intent();  
      startActivity.setClass(context, xxx.class); 
      startActivity.setAction(xxx.class.getName()); 
      startActivity.setFlags( 
                  Intent.FLAG_ACTIVITY_NEW_TASK 
                  | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 
      context.startActivity(startActivity); 
    }
    
    0 讨论(0)
  • 2020-12-17 09:07

    Yup, simply use the context and call the startActivity() method from that context.

    0 讨论(0)
提交回复
热议问题