0

单个警报管理器能否以 2 个不同的时间间隔触发,例如第一个间隔应该是 1 分钟,第二个间隔应该是 2 分钟

我正在尝试使用以下代码,但它没有像我预期的那样工作:(

{
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 *x, pi); 
}

//Initialized x=1;
//In onReceive what i did was...
public void onReceive(Context context, Intent intent) {
   if(x===1) 
       x=2;
   else
       x=1; 
}

这是错的吗?

4

1 回答 1

0

来自 Google 开发者文档

BroadcastReceiver 对象仅在调用 toonReceive(Context, Intent) 期间有效。一旦您的代码从此函数返回,系统就会认为该对象已完成且不再处于活动状态。

因此,您的 x 将始终为 1。使 x 变量为静态。

于 2014-02-27T03:18:09.433 回答