0

我必须按照以下代码调用 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);
        }
    ...
    }

和广播接收器:

public class WorkItemAlarmHandler extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        ....
    }

}

但是 WorkItemAlarmHandler 永远不会被调用。

更新:我首先尝试在清单中注册广播接收器。但这没有用:

<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);

再次感谢你的帮助!

4

2 回答 2

1

试试这个 :

在 WorkItemAlarmManager 中;

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();
            }
    }

}
于 2012-06-06T15:39:09.773 回答
1

您的代码使用:

intent.setClass(helpMe, WorkItemAlarmManager.class);

但是使用 setClass(...) 方法,您可以指定最终应该处理意图的类。

因此你应该写:

intent.setClass(helpMe, WorkItemAlarmHandler.class);

据我记得,如果您手动指定类或组件(例如使用 setClass() 方法),则必须在清单中指定此对象,因此请确保在此处提及接收者。

于 2012-06-06T15:46:39.023 回答