在Splash 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的onStop()中停止了服务,就会触发LocationService的onDestroy() (这意味着服务被停止了?),但按照AlarmManager中的指定,该服务在一分钟后仍在运行。
发布于 2013-03-22 07:56:11
将警报的挂起意图存储在变量中。当服务结束时请取消它。
https://stackoverflow.com/questions/15565324
复制相似问题