获得位置后如何停止服务? Android的

时间:2017-01-02 20:05:20

标签: android service gps location alarmmanager

我想要,不时通过使用GPS开启服务的AlarmManager获取位置。 AlarmManager工作正常,但我的停止服务有问题。

第一种情况:

我得到了位置,但服务每次都有效。

ConnectivityReceiver .java - 这是我的AlarmManager

    public final class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, " onReceive work", Toast.LENGTH_SHORT).show();
        Log.i("ADRIAN", "onRECEIVE ");
        Intent serviceIntent = new Intent(context,LocationUpdaterService.class);
        context.startService(serviceIntent);
}

LocationUpdaterService .java - 服务

public class LocationUpdaterService extends Service implements LocationListener,
                                                               GoogleApiClient.ConnectionCallbacks,
                                                               GoogleApiClient.OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    public LocationUpdaterService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("ADRIAN", "Wlazlem do onCreate");
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API).
                        addConnectionCallbacks(this).
                        addOnConnectionFailedListener(this)
                .build();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ADRIAN", "onStartCommand");
        mGoogleApiClient.connect();
        Toast.makeText(this, "Location services started", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onLocationChanged(Location location) {
        System.out.println("MyLocationService.onLocationChanged");

        String latitude = Double.toString(location.getLatitude());
        String longitude = Double.toString(location.getLongitude());

        Log.i("ADRIAN", "Lat:" + latitude + "Long: " + longitude);

        // do your work here with location
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(10); // Update location every second

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            Location loc = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            Log.i("ADRIAN", "onConnected");
        }
        Log.i("ADRIAN", "onConnected 2 ");
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }


    @Override
    public void onDestroy() {
//        String latitude = "null";
//        String longitude = "null";

        Toast.makeText(this, "Location services stopped", Toast.LENGTH_LONG).show();

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Location loc = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if(loc != null) {
//                latitude = Double.toString(loc.getLatitude());
//                longitude = Double.toString(loc.getLongitude());
//                Log.i("ADRIAN", "Lat:" + latitude + "Long: " + longitude);
            }
        }

        Log.i("ADRIAN", "Location services stopped");
        mGoogleApiClient.disconnect();
        super.onDestroy();
    }
}

Screen LOGCAT

第二种情况

当我添加context.stopService(serviceIntent)时,我的应用程序无法获取位置。我尝试使用SystemClock.sleep(10000);,但它仍然有效。

ConnectivityReceiver .java

public final class ConnectivityReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, " onReceive work", Toast.LENGTH_SHORT).show();
        Log.i("ADRIAN", "onRECEIVE ");
        Intent serviceIntent = new Intent(context,LocationUpdaterService.class);
        context.startService(serviceIntent);

        SystemClock.sleep(10000);
        Log.i("ADRIAN", "STOP SERVICE");
        context.stopService(serviceIntent);

}

LocationUpdaterService.java - 与上述相同。

SCREEN 2 LOGCAT

我做错了什么以及如何解决?

1 个答案:

答案 0 :(得分:-1)

onStartCommand --->服务。START_STICKY

@Override public void onDestroy() { locationManager.removeUpdates(listener);

        locationManager=null;
        super.onDestroy();


        Log.v("STOP_SERVICE", "DONE");

        locationManager.removeUpdates(listener);
        locationManager=null;

}