【问题标题】:How to stop Service, after get location? Android获取位置后如何停止服务?安卓
【发布时间】:2017-01-02 20:05:19
【问题描述】:

我想不时通过打开 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

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

【问题讨论】:

  • 获取位置后尝试拨打this.stopSelf();
  • 有效!我在搜索答案上浪费了两天时间,你在几分钟内就完成了;)非常感谢;)

标签: android service gps location alarmmanager


【解决方案1】:

onStartCommand ---> Service.START_STICKY

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

        locationManager=null;
        super.onDestroy();


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

        locationManager.removeUpdates(listener);
        locationManager=null;

}

【讨论】:

  • 问题是“我做错了什么以及如何解决?”。只需输入代码,您肯定不会回答第一部分。请添加一些问题和答案的解释,并注意格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
相关资源
最近更新 更多