Android 定位服务无法在后台运行

Android Location service didn't work in background

我正在开发一个定位应用程序,当用户按下 Button 时,它应该会根据经度和纬度开始跟踪一些统计数据。所有这些东西都很好用,但是当涉及到锁定屏幕或将应用程序置于后台时,该服务不再工作了!

我已经阅读了很多有关后台服务和广播接收器的内容,但我不知道如何在服务中实现 Google API 位置侦听器以及在何处实现此 class 在 MainActivity 中。任何人都可以用一个简短的代码示例告诉我如何实现这样的服务或 link 其中有解释吗?

这是可以执行您想要的操作的应用程序:

https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/CosyDVR.java

Main Activity 在 BackgroundVideoRecorder 服务上启动。

Intent intent = new Intent(/*CosyDVR.this*/getApplicationContext(), BackgroundVideoRecorder.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

查看mConnection onServiceConnected,onServiceDisconnected。

这是 BackgroundVideoRecorder class:

https://github.com/sergstetsuk/CosyDVR/blob/master/src/es/esy/CosyDVR/BackgroundVideoRecorder.java

它实现了 LocationListener。 onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged 也是如此。

使用保险丝定位服务并每次保存更新的位置

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

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    public static Location mCurrentLocation;

    .............................

    @Override
    public void onCreate() {

        //show error dialog if GoolglePlayServices not available
        if (isGooglePlayServicesAvailable()) {
            mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(BACKGROUND_INTERVAL);  
        mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        //mLocationRequest.setSmallestDisplacement(10.0f);  /* min dist for location change, here it is 10 meter */
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
         return super.onStartCommand(intent, flags, startId);
    }

    //Check Google play is available or not
    private boolean isGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        return ConnectionResult.SUCCESS == status;
    }


    @Override
    public void onConnected(Bundle bundle) {
        startLocationUpdates();
    }

    protected void startLocationUpdates() {
        try {
             PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
        } catch (IllegalStateException e) {}
    }

    @Override
    public void onLocationChanged(Location location) {

        //Save your location
    }
    ............................
}

您可以获得当前位置onLocationChanged()

有关详细信息,请查看 http://javapapers.com/android/android-location-fused-provider/ or follow official guide https://developer.android.com/training/location/index.html