我正在尝试在启动后立即设置警报接收器。因此,我有一个应该注册警报的OnBootReceiver。 onBootReceiver可以正常工作并被调用,但是以某种方式找不到我的AlarmReceiver类。
引导后成功启动的OnBootReceiver:
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = "OnBootReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "called");
Intent i = new Intent(context, com.packagenames.AlarmReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time.getTimeInMillis(), pi);
}
}
如您所见,它配置警报尝试调用com.packagenames.AlarmReceiver.class。此类存在且位于同一包中:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "alarm received");
Intent i = new Intent(context, com.packagename.DataService.class);
i.putExtra("action", "process");
context.startService(i);
}
}
不幸的是,我得到以下错误:
02-03 09:22:25.344:W / ActivityManager(103):无法启动服务
意图{flg = 0x4 cmp = com.phonegap.packagename / .AlarmReceiver(有其他功能)
}: 未找到
Android清单看起来像这样
<application>
// activities etc
<receiver
android:name="com.phonegap.packagename.OnBootReceiver"
android:enabled="true"
android:exported="false"
android:label="OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="com.phonegap.packagename.AlarmReceiver"
android:enabled="true"
android:label="AlarmReceiver">
<intent-filter>
</intent-filter>
</receiver>
</application>
你看到一个错误?也许我忘记了什么?
谢谢
编辑:在清单中,我添加了
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
为了使OnBootReceiver正常工作。我需要类似的闹钟吗?
最佳答案
创建挂起的Intent时,不应该使用getBroadcast而不是getService吗?