为什么警报管理器一直在触发未决意图android

时间:2017-06-20 10:26:39

标签: android

在我的应用中,我正在使用闹钟管理器。我正在设置特定时间的警报。我正在为特定的未来时间设置多个警报,如下面的代码所示:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.e("test", "on create is called");

    SharedPreferences preferences = getSharedPreferences("testing", MODE_PRIVATE);
    if (!preferences.getBoolean("alarm", false)) {
        Log.d("test", "generating alrams");
        setAlarm(getFirstAlarmTime(), 1, "one");
        setAlarm(getSecondAlarmTime(), 2, "two");
        setAlarm(getThirdAlarmTime(), 3 ," three");
        setAlarm(getFourthAlarmTime(), 4, "four");
        setAlarm(getFifthAlarmTime(), 5, "five");

        preferences.edit().putBoolean("alarm", true).apply();
    }
}

private long getFirstAlarmTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 7);
    calendar.set(Calendar.SECOND,0);

    return calendar.getTimeInMillis();
}

private long getSecondAlarmTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 8);
    calendar.set(Calendar.SECOND,0);

    return calendar.getTimeInMillis();
}

private long getThirdAlarmTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 9);
    calendar.set(Calendar.SECOND,0);

    return calendar.getTimeInMillis();
}

private long getFourthAlarmTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 10);
    calendar.set(Calendar.SECOND,0);

    return calendar.getTimeInMillis();
}

private long getFifthAlarmTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 11);
    calendar.set(Calendar.SECOND,0);

    return calendar.getTimeInMillis();
}


private void setAlarm(long alarmTime, int id, String type){
    Intent intent = new Intent(this, NotifyService.class);
    intent.putExtra("id", id);
    intent.putExtra("type", type);
    PendingIntent alarmIntent = PendingIntent.getService(getApplicationContext(), id, intent, 0);
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC_WAKEUP, alarmTime,  alarmIntent);
}

}

以下是我在意图中调用的NotifyService服务

  @Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int id = 0;
    String type = null;
    if (intent != null) {
        id = intent.getExtras().getInt("id");
        type = intent.getExtras().getString("type");
        Log.e("test", "current id is " + id);
        Log.e("test", "type is " + type);
    }
    PendingIntent activity = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    Uri azanSound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/raw/makkah_azan");
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
    if(!muteStatus) {
        builder.setContentTitle(type)
                .setContentText(type + " time has started...")
                .setAutoCancel(false)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setSound(azanSound)     // put sound here
                .setContentIntent(activity);
    }
    Notification notification = builder.build();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(id, notification);

    return START_REDELIVER_INTENT ;
}

每个警报都在setAlarm()方法中提到的特定时间触发,但问题是当我终止我的应用并重启我的应用时,警报管理器再次开始触发待处理的意图。为什么我错过了什么呢?我花了很多时间和谷歌但没有找到任何解决方案。有什么帮助吗?

有一点我注意到,如果手机时间是12点钟,闹钟设置为11点钟,应用程序开始闹钟不断重复。 :(

1 个答案:

答案 0 :(得分:0)

您是否确定在您杀死应用后警报实际上已触发,并且您的意图不仅仅是在您请求时通过返回NotifyService中的START_REDELIVER_INTENT来再次发送。

' START_REDELIVER_INTENT'基本上要求在任务被杀死时重复意图。对于您的用例,您似乎需要返回START_NOT_STICKY,因为您只想触发一次意图。

What is START_STICKY,START_NOT_STICKY and START_REDELIVER_INTENT Service

另外,回答

  

有一点我注意到,如果手机时间可以说是12点   时钟和闹钟设置为11点钟,应用程序启动闹钟保持   重复。

过去一段时间内设置的任何警报都会立即触发。