【发布时间】:2018-01-10 21:14:34
【问题描述】:
我正在开发android应用程序,所以我正在启动一个带有警报的服务:
public void scheduleLocationCheckerAlarm() {
Intent intent = new Intent(getApplicationContext(), LocationCheckerReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, LocationCheckerReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 600000, pIntent);
}
LocationCheckerReceiver:
public class LocationCheckerReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, LocationNotificator.class);
context.startService(i);
}
服务:
public class LocationNotificator extends Service {
public LocationNotificator() {
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Location checker", "Service running");
//My code is here
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Location checker", "Service destroyed");
}
所以我希望该服务每 1 分钟检查一次并一直运行,即使应用程序已被用户关闭。
【问题讨论】:
-
你遇到了什么问题?
-
你是否在 Manifest 中声明了服务?
-
应用程序关闭时服务被杀死 Android manifest: