我有一个在AlarmManager收到警报时启动的PopUp活动.
AlarmReceiver扩展了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扩展了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从警报管理器启动.如果在应用程序不是活动应用程序时启动PopUpActivity,并且如果用户按下“确定”按钮,则活动将消失.到目前为止,没有什么是错的.问题是,如果用户打开最近的应用程序屏幕并选择活动,则会再次启动新的PopUpActivity.我怎样摆脱这个问题?
解决方法:
当您在清单中声明PopupActivity时,请确保包含android:noHistory =“true”.这意味着只要您打开“最近”,弹出活动就会被忘记,您将在重新打开应用程序时返回到之前的位置.
标签:android,broadcastreceiver,alarmmanager
来源: https://codeday.me/bug/20190612/1225841.html