我在清单中设置了启动接收器,启动WakefulBroadcastReceiver然后启动ServiceIntent来创建警报。
我的清单有这个:
<receiver android:name=".controllers.receivers.AlarmBootReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
以及RECEIVE_BOOT_COMPLETED权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
我的AlarmBootReceiver是这样的:
public class AlarmBootReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent alarmBootServiceIntent = new Intent(context, AlarmBootService.class);
startWakefulService(context, alarmBootServiceIntent);
}
}
我的IntentService是这样的:
public class AlarmBootService extends IntentService {
public AlarmBootService() {
super("AlarmBootService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.wtf("Boot", "Boot receiver started");
//Get list of classes from realm and set up alarms for each
Realm.init(getBaseContext());
Realm realm = Realm.getDefaultInstance();
RealmResults<Class> classes = realm.where(Class.class).findAll();
//For each day in days string, create an alarm an hour before fromtime
MainActivity ma = new MainActivity();
for (Class c : classes) {
for (char day : c.getDays().toCharArray()) {
//Convert SMTWRFA char to calendar day int
int d = ClassDialog.charToDay(day);
//Create the alarm
ma.createWeeklyAlarmForDay(d, c.getName(),
c.getBuilding(),
c.getFromAMPM().equals("AM") ? Calendar.AM : Calendar.PM,
c.getTimeFromH(), c.getTimeFromM());
}
}
//Release wakelock
AlarmBootReceiver.completeWakefulIntent(intent);
}
}
但该服务似乎永远不会推出。我从未在输出中看到“Boot receiver started”登录,并且未注册警报。我做错了什么?