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 started from an alarm manager. If PopUpActivity is started when the application is not the active application, and if user presses "OK button", activity disappears. Nothing is wrong right here till now. The problem is, if user opens recent apps screen and selects the activity a new PopUpActivity is started again. How can i get rid off this problem?
When you declare your PopupActivity in your manifest, make sure you include android:noHistory="true"
. This will mean that as soon as you open "recents", the popup activity will be forgotten, and you will just return to where you were before when you re-open the app.
When user presses Ok
button, Activity.finish()
method is being called. This results in the Activity
being destroyed. Hence when user selects the app from recent app section, the Activity
is created again.
In case you don't want to destroy the Activity
but want to put it in background, replace Activity.finish() method with Activity.moveTaskToBack(boolean).
findViewById(R.id.dialog_ok_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopRinging();
moveTaskToBack(true);
}
});
You need to handle scenarios wherein Activity
is restarted after being killed due to memory shortage and configuration changes.
You can set FLAG_ACTIVITY_SINGLE_TOP in the Activity
launch Intent
flags, to ensure the new instance is not created if it is already running at the top of the history stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With