在Splash Activity
的{{1}}中,我以这种方式启动服务
onResume()
在AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
ystemClock.elapsedRealtime() + 20000, 60000, pi);
中,我以这种方式停止服务
onStop()
和服务类是:
stopService(new Intent(this, LocationService.class));
我在public class LocationService extends Service implements LocationListener {
private LocationManager mgr;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
//get Location details
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10 * 60 * 1000, 50, this);
return START_NOT_STICKY;
}
private void dumpLocation(Location l) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onDestroy() {
super.onDestroy();
mgr.removeUpdates(this);
}
}
的{{1}}停止服务后,Splash Activity
的{{1}}被触发(这意味着服务已停止?),但仍然在在onStop()
服务运行中指定的分钟。
答案 0 :(得分:1)
将警报的待处理意图存储在变量中。服务结束时,请致电取消。