在我的应用程序中,我设置了一个警报,当触发时将启动前台服务。我这样做:
public class Stage2Alarm extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
@Override
public void onReceive(Context context, Intent intent) {
if (!MyPrefs.isActivity1Visible()) {
MyPrefs.setStageOneCompleted(true, context);
StageNotification.sendNotification(context);
context.startService(new Intent(context, MyService.class));
}
}
public void setAlarm(Context context) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, Stage2Alarm.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, MyPrefs.getWakeUp(), alarmIntent);
}
}
问题是它根本没有启动服务,只发送通知,可能是什么问题?
编辑:当服务应该启动时,我也在 LogCat 中得到 NullPointerException
编辑2:这是服务的重要部分
@Override
public void onCreate() {
super.onCreate();
db = new DataHelper(this);
broadcaster = LocalBroadcastManager.getInstance(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("Service", intent.toString());
initialize();
return START_REDELIVER_INTENT;
}