我在应用程序标记内的AndroidManifest中声明了一个NotifyService:
<service android:name="com.ssaurel.myapp.services.NotifyService"
android:enabled="true"
android:exported="false"/>
我使用AlarmManager来计划我的服务的执行:
Intent intent = new Intent(MyActivity.this, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
PendingIntent pendingIntent = PendingIntent.getService(MyActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
我的服务包含以下代码:
public class NotifyService extends Service {
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
public static final String INTENT_NOTIFY = "com.ssaurel.myapp.INTENT_NOTIFY";
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
if(intent.getBooleanExtra(INTENT_NOTIFY, false))
showNotification();
return START_NOT_STICKY;
}
//...
}
我的问题是AlarmManager永远不会启动我的NotifyService。可以肯定的是,我使用以下adb命令检查我的服务是否已正确规划:
adb shell dumpsys alarm > dump.txt
结果包含有关我的服务的以下内容:
RTC_WAKEUP#12:报警{39dc5d5输入0时1459752420000 com.ssaurel.myapp} 标签= walarm :com.ssaurel.myapp / .services.NotifyService type = 0 whenElapsed = + 1d0h2m7s689ms when = 2016-04-04 08:47:00 window = -1 repeatInterval = 86400000 count = 0 operation = PendingIntent {343251ea:PendingIntentRecord {135e5cdb com.ssaurel.myapp startService}}
似乎没错,但从未打过电话。有人会有一些想法来帮助我吗?
请注意,使用意图直接启动通知服务:
Intent t = new Intent(SettingsActivity.this, NotifyService.class);
t.putExtra(NotifyService.INTENT_NOTIFY, true);
startService(t);
感谢。
西尔