从“最近使用的应用程序”中删除应用程序后,如何正确调用警报活动?

时间:2019-04-18 14:47:05

标签: java android

我正在尝试制作一个可以使用用户定义的文本设置警报的应用程序。

问题是,当用户设置警报然后关闭应用程序(从“最近的应用程序”中删除)时,仅出现一个AlarmWindow活动(即使同时设置了许多警报)。更重要的是,当MainActivity没有从“最近的应用程序”中删除时,一切正常。关于如何解决此问题的任何想法?

在MainActivity中,我使用AlarmManager和PendingIntent设置警报:

Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmIntent.putExtra("requestCode", id); //id used to load text from database
alarmIntent.putExtra("date_long", cal.getTimeInMillis()); //alarm time

pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

我的AlarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {
    public void onReceive(final Context context, Intent intent) {

        int id =  intent.getIntExtra("requestCode", 0);
        Long date_long =intent.getLongExtra("date_long",0);

        Intent nIntent = new Intent(context, AlarmWindow.class);
        nIntent.putExtra("alarmId", id);
        nIntent.putExtra("alarmDate", date_long);
        nIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(nIntent);
    }

}

AlarmWindow活动,我需要为每个警报打开一个窗口(删除了一些不重要的行):

public class AlarmWindow extends AppCompatActivity  {
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_alarmwindow);

        context = this.getApplicationContext();

        alarmTextView = findViewById(R.id.alarmTitle);

        if (savedInstanceState != null) {
            id = savedInstanceState.getInt("alarmId");
            date_long = savedInstanceState.getLong("alarmDate");
        } else {
            id = getIntent().getExtras().getInt("alarmId");
            date_long = getIntent().getExtras().getLong("alarmDate");
        }

        alarmTextView.setText(text_for_alarmTextView(id)); // get alarm text from database

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        finish();
        startActivity(new Intent(intent));
    }
}

0 个答案:

没有答案