1

在我的应用程序中,我设置了一个警报,当触发时将启动前台服务。我这样做:

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;
}
4

1 回答 1

0

尝试这个

am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime()
                    + Constants.ALARM_MANAGER_INTERVAL, pendingIntent);

代替 Intent 使用 PendingIntent

于 2014-10-10T11:10:03.757 回答