PopUp activity starts again when it starts from the "latest apps"

I have a PopUp activity that starts when the AlarmManager receives an alarm.

AlarmReceiver extends WakefulBroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    Intent service = new Intent(context, AlarmService.class);
    service.putExtras(intent);

    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, service);
}

AlarmService extends IntentService:

@Override
protected void onHandleIntent(Intent intent) {

    Intent i = new Intent();
    i.setClass(this, PopUpActivity.class);
    startActivity(i);
    AlarmReceiver.completeWakefulIntent(intent);
}

PopUpActivity:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
    getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    setContentView(R.layout.layout_dialog);



    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ClientConstants.WAKE_LOCK_NOTIFICATION);
    // Acquire the lock
    wl.acquire();

    if (canVibrate){
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[]{ 0, 200, 500 },0);
    }
    if (canRing){
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(this, getAlarmUri());
            final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IOException e) {
        }
    }

    findViewById(R.id.dialog_ok_button).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopRinging();
            finish();
        }
    });
    // Release the lock
    wl.release();
}
private void stopRinging(){
    if (canRing && mediaPlayer.isPlaying())
        mediaPlayer.stop();
    if (canVibrate){
        vibrator.cancel();
    }
}

PopUpActivity is launched from the alarm manager. If PopUpActivity starts when the application is not an active application, and if the user clicks the OK button, the activity disappears. There is nothing wrong with that. The problem is that if the user opens the screen of the latest applications and selects activity, the new PopUpActivity function starts again . How can I get rid of this problem?

+4
5

PopupActivity , , android:noHistory="true". , "recents", , , , .

+4

Ok, Activity.finish(). Activity. , , Activity .

Activity, , Activity.finish() Activity.moveTaskToBack().

findViewById(R.id.dialog_ok_button).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        stopRinging();
        moveTaskToBack(true);
    }
});

, Activity - .

FLAG_ACTIVITY_SINGLE_TOP Intent Intent, , , .

+3

, Activity, :

<activity>            
     android:launchMode="singleTask"
</activity>

, Activity ,

<activity>            
    android:excludeFromRecents="true"
</activity>

, .

0

OK, finish() , , .

, /, android:excludeFromRecents="true"

launchMode = "singleInstance" , .

0

, , , , :

Intent i = new Intent();
i.setClass(this, PopUpActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(i);

Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS PopupActivity .

0

Source: https://habr.com/ru/post/1535030/


All Articles