Close application from broadcast receiver

前端 未结 3 729
梦如初夏
梦如初夏 2021-01-14 00:05

I\'m new in android programming. I\'ve tried registering broadcast receiver in activity, but my receiver not working when apps onPause. So i found that i need to registering

3条回答
  •  青春惊慌失措
    2021-01-14 00:55

    Use code below to send new intent to MainActivity from BroadcastReceiver:

    Intent i = new Intent(context, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP);
    i.putExtra("close_activity",true);
    context.startActivity(i);
    

    in MainActivity use OnNewIntent as below:

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent.getBooleanExtra("close_activity",false)){
            this.finish();
    
        }
    }
    

    FYI, I haven't tried the above code, But I have similar code working.

提交回复
热议问题