【问题标题】:Unable to tell if alarm is scheduled- repeating alarm无法判断是否已安排闹钟 - 重复闹钟
【发布时间】:2012-12-28 16:46:51
【问题描述】:

我有一个在 StartActivity 类中启动的重复警报。包是 psychsoft.gps.v1 代码似乎没有捕捉到警报是否启动。

        if ((PendingIntent.getBroadcast(con, 13336,
            new Intent("psychsoft.gps.v1"), 
            PendingIntent.FLAG_NO_CREATE) != null))
    {
        Log.i("aaa", "Alarm is already active");
        Toast.makeText(con, "alarm already active", Toast.LENGTH_SHORT).show();
    }



    Calendar calGPS = Calendar.getInstance();
    Intent intentGPS = new Intent(con, AlarmReceiver.class);
    intentGPS.putExtra("alarm_message", "gps");
    PendingIntent senderGPS = PendingIntent.getBroadcast(this, 13336, intentGPS, PendingIntent.FLAG_UPDATE_CURRENT);        
    // Get the AlarmManager service
    AlarmManager amGPS = (AlarmManager) getSystemService(ALARM_SERVICE);
    amGPS.setRepeating(AlarmManager.RTC_WAKEUP, calGPS.getTimeInMillis(),1000*60*5, senderGPS);

为了测试这段代码,我运行它一次来安排警报(安装应用程序)。然后我再次运行它(通过单击图标)以尝试获取 Toast 或 Log 输出。我都没有。为什么?我原本以为可以通过将 PendingIntent 的请求代码与 if 语句的请求代码匹配来解决问题,但它并没有解决问题。

【问题讨论】:

  • 在这里查看这个线程:stackoverflow.com/a/9575569 一个很好的例子如何做你想要实现的。然后在该示例中,如果 alarmup 为 false,请创建您的意图并再次安排您的闹钟。

标签: android scheduling alarmmanager alarm


【解决方案1】:

尝试像这样创建和检查意图:

Intent intentGPS = new Intent("psychsoft.gps.v1.MY_UNIQUE_ACTION");

if ((PendingIntent.getBroadcast(con, 13336,
            new Intent("psychsoft.gps.v1.MY_UNIQUE_ACTION"), 
            PendingIntent.FLAG_NO_CREATE) != null))
    {
        Log.i("aaa", "Alarm is already active");
        Toast.makeText(con, "alarm already active", Toast.LENGTH_SHORT).show();
    }

【讨论】:

  • 在这种情况下,意图如下所示: Intent intentGPS = new Intent("psychsoft.gps.v1.AlarmReceiver");对吗?
  • 澄清我之前的评论:你的代码中的 MY_UNIQUE_ACTION 是什么?是类(AlarmReceiver)还是别的什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多