如果应用程序进入后台并在超过 15 分钟后恢复,我需要从应用程序中注销。
请在实现提供的解决方案后参考代码
public class BaseActivity extends Activity {
BaseActivity context;
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BaseActivity() {
context = this;
}
@Override
protected void onStart() {
super.onStart();
if (alarmMgr != null) {
alarmMgr.cancel(alarmIntent);
}
}
@Override
protected void onStop() {
super.onStop();
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(BaseActivity.this, SampleBootReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1 * 30 * 1000, alarmIntent); // 15
}
class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "alarm", Toast.LENGTH_SHORT).show();
Intent intent_login = new Intent(context, SignIn.class);
intent_login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_login);
overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
}
}