如何从已滑动关闭的活动启动locationManager时,如何从服务停止locationManager pendingIntent更新?

时间:2016-04-04 19:44:32

标签: java android service locationmanager

我有一个启动locationManager的活动。

Intent intent = new Intent(context, LocationReceiver.class);
    intent.setAction("myBroadcast");
    intent.putExtra("session_id", session_id);
    intent.putExtra("device_id", device_id);
    //intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
    lpendingIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 58534, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    //Register for broadcast intents
    locationManager  = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, lpendingIntent);

broadcastReceiver获取此意图并启动服务。

public class LocationReceiver extends BroadcastReceiver {
//private static boolean semaphore = true;
SharedPreferences sharedpreferences;
@Override
public void onReceive(Context context, Intent intent) {

    Location location = (Location) intent.getExtras().get(android.location.LocationManager.KEY_LOCATION_CHANGED);

    Intent locationServiceIntent = new Intent(context, DistanceFilterLocationService.class);
    locationServiceIntent.putExtra("device_id", intent.getStringExtra("device_id"));
    locationServiceIntent.putExtra("session_id", intent.getStringExtra("session_id"));

     Double longi = location.getLongitude();
     Double lati = location.getLatitude();

    locationServiceIntent.putExtra("longitude", longi);
    locationServiceIntent.putExtra("latitude", lati);
    locationServiceIntent.putExtra("accuracy", location.getAccuracy());
    locationServiceIntent.putExtra("time", location.getTime());
    //context.unregisterReceiver(this);
    context.startService(locationServiceIntent);


}

}

服务启动并很好地杀死自己。我可以很容易地取消注册我的广播接收器,但是如何阻止pendingIntent继续ping gps,即使没有广播接收器可以接收它?我的服务似乎无法从活动中看到pendingIntent,因为活动已被关闭,因此我无法调用removeUpdates(pendingIntent);

0 个答案:

没有答案