在我的应用程序中,我设置了一个警报,当被触发时将启动前台服务。 我是这样做的:
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;
}
答案 0 :(得分:0)
试试这个
am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()
+ Constants.ALARM_MANAGER_INTERVAL, pendingIntent);
代替Intent使用PendingIntent