【问题标题】:Alarm Receiver for multiple alarms is not getting invoked未调用多个警报的警报接收器
【发布时间】:2016-01-31 20:27:43
【问题描述】:

我正在尝试在不同的特定时间重复多个警报。 问题是警报接收器在任何给定时间都没有被调用。

我在活动中的代码:

private void setAlarms() {
    Intent myIntent=new Intent(this, AlarmReceiver.class);
    myIntent.putExtra("MedName",medication_name);
    myIntent.setAction("b5.project.medibro.receivers.AlarmReceiver");
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    //am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
    int counter=0;
    for(String timer: timers){
       //timers is an array of Strings in the format hh:mm
         try {
            String[] comps=timer.split(":");
            Calendar cal=Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(comps[0]));
            cal.set(Calendar.MINUTE, Integer.valueOf(comps[1]));
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Log.d(TAG,comps[0]+" "+comps[1]+ "Alarm Time: " + cal.getTime().toString());
            PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    cal.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY,
                    pendingIntent);
            counter++;
        } catch (ParseException e) {
            e.printStackTrace();
            Log.d(TAG,"Time Parsing error: "+e.getMessage());
        }
    }
}

这是我的警报接收器:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("b5.project.medibro.receivers.AlarmReceiver")) {
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
        Log.d("Alarm Receiver", "Alarm is invoked. ");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.messenger_bubble_large_blue);
        builder.setContentTitle("Time to take your medicine");
        String medName = intent.getStringExtra("MedName");
        String text = "Medicine name: " + medName;
        builder.setContentText(text);

        Notification notification = builder.build();
        NotificationManagerCompat.from(context).notify(0, notification);
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
    }
}

}

清单声明:(在应用程序标签内)

<receiver android:name="b5.project.medibro.receivers.AlarmReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="b5.project.medibro.receivers.AlarmReceiver"/>
        </intent-filter>
    </receiver>

我觉得上面的代码应该至少执行一次广播接收器,但 logcat 中什么也没有显示。 有人能告诉我我错过了什么吗?

【问题讨论】:

  • 报警时间日志的输出是什么?
  • @Gavriel 02-01 02:27:41.855: D/Add Prescription(29441): No. of Alarms: 2 02-01 02:27:41.881: D/Database Handler(29441): Inserting: Testing22:29$$$2:301/2/2016CONTINUOUSTesting 02-01 02:27:41.890: D/Add Prescription(29441): 2 29Alarm Time: Mon Feb 01 02:29:00 GMT+05:30 2016 02-01 02:27:41.906: D/Add Prescription(29441): 2 30Alarm Time: Mon Feb 01 02:30:00 GMT+05:30 2016。在“警报时间”日志之后没有任何显示。

标签: android broadcastreceiver android-alarms repeatingalarm android-broadcastreceiver


【解决方案1】:

用 getBroadcast 代替 getService:

PendingIntent pendingIntent = PendingIntent.getService(this, counter, myIntent,0);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, counter, myIntent,0);

同时查看http://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int),并找出您需要的标志,因为如果只有最后一个警报(在 for 循环中)是,我不会感到惊讶工作。

【讨论】:

  • 谢谢! flag_update_current 完成了这项工作。你能告诉我稍后如何取消这些警报吗?(在另一个活动中。我在这里也遇到了一些困难)
  • 试试这个:stackoverflow.com/questions/14485368/…,你会通过他们的 id 来识别他们(在你的情况下是计数器)
  • 我已经看到了。我的问题是有多种药物。并且计数器将为每种药物重置。因此,当有两个具有相同计数器值的 PI 时,我无法访问特定的 pendingIntent。
  • 这听起来像是一个算法问题。用它打开一个新问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多