在 aSplash Activity
中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);
}
}
一旦我在Splash Activity
's中停止服务onStop()
,就会触发 (这意味着服务已停止?),但仍然在服务中指定的一分钟后运行。onDestroy()
LocationService
AlarmManager