我必须按照代码调用BroadcastReceiver:
public class WorkItemAlarmManager {
...
public void initAlarm(HelpMe helpMe, String delay,String period,WorkItem workItem){
//HelpMe = Activity
helpMe.registerReceiver(new WorkItemAlarmHandler() , new IntentFilter("WORK_ITEM_ALARM"));
Intent intent = new Intent("WORK_ITEM_ALARM");
intent.setClass(helpMe, WorkItemAlarmManager.class);
intent.putExtra("work_item", workItem);
PendingIntent mAlarmSender = PendingIntent.getService(helpMe, 0,intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//System.out.println("START"+System.currentTimeMillis());
calendar.add(Calendar.SECOND, calcDelayMillis(delay));
//System.out.println("END"+calendar.getTimeInMillis());
AlarmManager am = (AlarmManager) helpMe.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender);
}
...
}
和BroadcastReceiver:
public class WorkItemAlarmHandler extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
....
}
}
但是从不调用WorkItemAlarmHandler。
更新: 我首先尝试在清单中注册BroadcastReceiver。但那不起作用:
<receiver
android:name="de.helpme.alarm.WorkItemAlarmHandler"
android:enabled="true"
android:label="WorkItemAlarmHandler" >
<intent-filter>
<action android:name="WORK_ITEM_ALARM" />
</intent-filter>
</receiver>
感谢您的回答:)。现在它有效 - 但我还不知道哪一个是正确的,因为我立刻实现了两个答案。如果一个数字是正确的,我将右边标记为答案。
更新 我认为问题的原因是:
PendingIntent mAlarmSender = PendingIntent.getService(helpMe, 0,intent, 0);
使用此代码可以正常工作:
PendingIntent mAlarmSender = PendingIntent.getBroadcast(helpMe, 0, intent, 0);
再次感谢您的帮助!
答案 0 :(得分:1)
试试这个:
在工作项目AlarmManager中;
public class WorkItemAlarmManager {
...
public void initAlarm(HelpMe helpMe, String delay,String period,WorkItem workItem){
Intent intent =new Intent(helpMe, WorkItemAlarmHandler.class);
intent.setAction("WORK_ITEM_ALARM");
intent.putExtra("work_item", workItem);
PendingIntent mAlarmSender=
PendingIntent.getBroadcast(helpMe, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//System.out.println("START"+System.currentTimeMillis());
calendar.add(Calendar.SECOND, calcDelayMillis(delay));
//System.out.println("END"+calendar.getTimeInMillis());
AlarmManager am = (AlarmManager) helpMe.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mAlarmSender);
}
...
}
并在WorkItemAlarmHandler中:
public class WorkItemAlarmHandler extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("WORK_ITEM_ALARM")){
Toast.makeText(context, "WORK_ITEM_ALARM", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "repeating alarm",
Toast.LENGTH_LONG).show();
}
}
}
答案 1 :(得分:1)
您的代码使用:
intent.setClass(helpMe, WorkItemAlarmManager.class);
但是使用setClass(...)方法指定应该最终处理意图的类。
因此你应该写:
intent.setClass(helpMe, WorkItemAlarmHandler.class);
据我记忆,如果您手动指定类或组件(例如使用setClass()方法),则必须在清单中指定此对象,因此请确保在那里提及接收器。