你可以为AlarmManager setInexactRepeating使用不同的包吗?

时间:2014-10-27 06:18:15

标签: android

我想设置一个重复警报,它在第一个触发器上发送字符串“test”。

下一个触发器是否可以使用另一个字符串“test2”?

警报管理员:

    // Enable the scheduled alarm to send notifications
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, Service.class);

    Bundle extras = new Bundle();
    extras.putString("test", "test");
    alarmIntent.putExtras(extras);

    PendingIntent alarmPendingIntent = PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Set the alarm in 30 minutes and repeat it every 30 minutes.
    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 1000,
            1000,
            alarmPendingIntent);

IntentService:

public class Service extends IntentService {

public Service() {
    super("service");
}

@Override
protected void onHandleIntent(Intent intent) {
    String test = intent.getStringExtra("test");
    // update the bundle data
    Log.i("result", test);

    // how to update here the string to "test2" ?
}
}

我希望在第二次触发时String test的值为test2

但我总是从第一次触发中获得原始值。

1 个答案:

答案 0 :(得分:1)

您需要单独的闹钟和单独的PendingIntent。您可以同时设置它们,也可以设置不重复的警报,并使服务在运行时设置以下警报。

请注意,如果您同时设置两个警报,则除了额外警报之外还需要其他内容,因为Android在检查给定{{1}的现有PendingIntent时不会比较意图额外内容}}。一种方法是这样的:

Intent