我的服务定义如下:
public class TestService extends IntentService {
private static final String TAG = TestService.class.getSimpleName();
public TestService() {
super(TAG);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Logger.e(TAG, "Shoot !!! at : " + new Date(System.currentTimeMillis()));
}
private static PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent(context, TestService.class);
return PendingIntent.getService(context, INTENT_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
清单声明:
<service
android:name="com.testapp.service.TestService"
android:exported="true"></service>
使用以下方法在主要活动中设置警报:
public static void setNextAlarm(Context context) {
long triggerTime = System.currentTimeMillis() + 120000l;
Logger.e(TAG, "Next trigger time : " + new Date(triggerTime).toString());
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = TestService.getPendingIntent(context);
am.cancel(pi);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerTime, pi);
}
当我在日志中看到或调试时,“下次触发时间:”消息显示正确的时间比当前时间晚两分钟。但onHandleIntent永远不会被触发,即实际上不运行该服务。很少有StackOverflow线程建议在清单中使用完全限定的类名,也建议设置android:exported="true"
我做了但不起作用。可能出现什么问题?