我正在处理将启动两个警报的应用程序,如下所示:
两个警报都使用ELAPSED_REALTIME_WAKEUP类型。我现在面临的问题是,相同的代码在Android 2.3.5上运行良好,但Android 4.0.4。我已经对谷歌进行了一些研究,这个问题似乎没有链接到“在第一次手动启动应用程序之前不会触发广播接收器”的问题。
以下是设置两个警报的待处理意图的代码:
private class AlarmEvent {
private Intent mIntent;
private PendingIntent mPendingIntent;
protected AlarmEvent(Context ctx, Class<?> cls, int pendingIntentType,
int requestCode, int flags, String intentAction) {
mIntent = new Intent(ctx, cls);
mIntent.setAction(intentAction);
switch(pendingIntentType) {
case PENDING_BROADCAST:
mPendingIntent = PendingIntent.getBroadcast(ctx, requestCode, mIntent, flags);
break;
case PENDING_SERVICE:
mPendingIntent = PendingIntent.getService(ctx, requestCode, mIntent, flags);
break;
default:
Log.w(TAG, "AlarmEvent:Invalid pending intent type=" + pendingIntentType);
}
}
protected PendingIntent getPendingIntent() {
return mPendingIntent;
}
}
以下是创建AlarmEvent的代码:
//Create polling event
mPollingEvent = new AlarmEvent(mCtx, EventRouter.class, PENDING_BROADCAST,
POLLING_REQUEST_CODE, PendingIntent.FLAG_CANCEL_CURRENT,
EventRouter.POLLING_ACTION);
//Create monitor event
mReminderEvent = new AlarmEvent(mCtx, EventRouter.class, PENDING_BROADCAST,
REMINDER_REQUEST_CODE, PendingIntent.FLAG_CANCEL_CURRENT,
EventRouter.REMINDER_ACTION);
以下是启动警报的代码:
//Get AlarmManager instance
AlarmManager alarm = (AlarmManager)mCtx.getSystemService(AS);
long triggerAtTime = 0;
//Process the request
switch(type) {
case POLLING:
triggerAtTime = SystemClock.elapsedRealtime();
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime,
POLLING_INTERVAL_MS, mPollingEvent.getPendingIntent());
Log.d(TAG, "alarmRequest:Polling starts at " + System.currentTimeMillis());
break;
case REMINDER:
triggerAtTime = SystemClock.elapsedRealtime() + when;
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, mReminderEvent.getPendingIntent());
Log.d(TAG, "alarmRequest:Reminder to be fired after " + when + " ms");
break;
}
以下是EventRouter的代码:
public class EventRouter extends BroadcastReceiver {
public static final String POLLING_ACTION = "com.xxxxxx.POLLING";
public static final String REMINDER_ACTION = "com.xxxxxx.REMINDER";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "onReceive:Action=" + action);
if(POLLING_ACTION.equals(action)) {
//TODO: Polling handling
}
else if(REMINDER_ACTION.equals(action)) {
//TODO: Reminder handling to fire a status bar notification
}
}
}
我的应用的Android清单:
<application
...
...>
<activity
android:name=".AlarmActivity"
...>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".EventRouter" android:enabled="true" android:exported="true"/>
</application>
通过在终端上执行以下命令,可以正确注册两个警报:
adb -d shell dumpsys alarm
但是,只有POLLING警报传送到我的接收器。我一直在努力解决这个问题,但找不到解决方案。您能否帮助我查看我设置待处理意图的方式,看看是否有任何错误?
答案 0 :(得分:0)
您应该将您的操作作为意图过滤器添加到清单文件中的接收者声明中,以便它可以捕获这些操作。
<receiver android:name=".EventRouter" android:enabled="true" android:exported="true"
<intent-filter>
<action android:name="your.receiver.package.name.POLLING_ACTION"></action>
</intent-filter>
<intent-filter>
<action android:name="your.receiver.package.name.REMINDER_ACTION"></action>
</intent-filter>
<receiver/>
这是引起我注意的第一个错误。