Android 即使应用程序从最近的抽屉中被杀死,也可以使用服务获取连续位置

Android 即使应用程序从最近的抽屉中被杀死,也可以使用服务获取连续位置,android,android-service,google-geolocation,google-location-services,Android,Android Service,Google Geolocation,Google Location Services,我从以下主要活动开始服务:- Intent intent = new Intent(this, MyLocationService.class); startService(intent); MyLocationService类看起来像:- public class MyLocationService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks,

我从以下主要活动开始服务:-

Intent intent = new Intent(this, MyLocationService.class);
startService(intent);
MyLocationService类看起来像:-

public class MyLocationService extends Service implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    private static final String TAG = MyLocationService.class.getSimpleName();
    public static Location mCurrentLocation;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate: ");
        initiateGooglePlayService();
    }
    public void initiateGooglePlayService() {
        Log.e(TAG, "initiateGooglePlayService: ");
        if (isGooglePlayServicesAvailable()) {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(5000);
            mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setSmallestDisplacement(10.0f);
            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) {
        Log.e(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: ");
        return null;
    }
    private boolean isGooglePlayServicesAvailable() {
        Log.e(TAG, "isGooglePlayServicesAvailable: ");
        int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getApplicationContext());
        return ConnectionResult.SUCCESS == status;
    }
    @Override
    public void onConnected(Bundle bundle) {
        Log.e(TAG, "onConnected: ");
        startLocationUpdates();
    }
    @Override
    public void onConnectionSuspended(int i) {
        Log.e(TAG, "onConnectionSuspended: ");
    }
    protected void startLocationUpdates() {
        Log.e(TAG, "startLocationUpdates: ");
        try {
            PendingResult<Status> pendingResult;
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
        } catch (IllegalStateException ignored) {
        }
    }
    @Override
    public void onLocationChanged(Location location) {
        Log.e(TAG, "onLocationChanged: " + location.getLongitude());
        if (mGoogleApiClient.isConnected()) {
            mCurrentLocation = location;
            Intent intent = new Intent("GPSLocationUpdates");
            intent.putExtra("location", location);
         LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
            Toast.makeText(this, "location", Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.e(TAG, "onConnectionFailed: ");
    }
}
您可以使用SDK在后台获取位置更新。了解SDK在不同条件下的可靠性

首先是SDK

设置SDK后,只需设置回调以接收位置更新

HyperTrack.setCallback(new HyperTrackEventCallback() {
@Override
public void onEvent ( @NonNull final HyperTrackEvent event){
    switch (event.getEventType()) {
        case HyperTrackEvent.EventType.LOCATION_CHANGED_EVENT:
            Log.d(TAG, "onEvent: Location Changed");
            HyperTrackLocation hyperTrackLocation = event.getLocation();
            LatLng latLng = hyperTrackLocation.getLatLng();
            updateCurrentLocationMarker(event.getLocation());
            break;
    }
}
}

(免责声明:我在HyperTrack工作。)

我不能代表所有人说话,但如果我从最近关闭了一个应用程序,我希望它关闭并停止运行其所有功能code@NickA你说得对,但我正在开发商业应用程序,因此,需要在后台进行一些操作。您是否在给定距离10英尺的情况下将设备从一个地方移动到另一个地方。您的设备必须移动超过10英尺,如您在代码中所述。是的,将其移动100米,然后看看是什么happens@KailasBhakade尝试删除'mLocationRequest.setSmallestDisplacement(10.0f);`行。这是工作,甚至应用程序从最近也被杀。因为我在找这个。如果应用程序在后台,则我的代码可以工作。是的,我可以工作,但请记住不要在示例代码包含相同内容的任何地方调用“networkUtilObj.disconnectGoogleApiClient();”line@KailasBhakade如果从最近的应用程序抽屉中删除了应用程序,则无法执行您正在寻找的任何操作。直到你有一个用例可以通过从广播接收器启动来唤醒你的应用程序。@chandil03好的,谢谢。因为Oreo对后台服务进行了限制,所以SDK需要保留一个粘性通知才能作为前台运行服务。
/**Check out my Github post i did the same for Taxi clone**/

Link ---> https://github.com/yash786agg/GPS/

Note: Remember to remove or comment the below mentioned code if you want the latitude 
and latitude even when the application is in background.

    if(networkUtilObj != null)
    {
        networkUtilObj.disconnectGoogleApiClient();
    }
HyperTrack.setCallback(new HyperTrackEventCallback() {
@Override
public void onEvent ( @NonNull final HyperTrackEvent event){
    switch (event.getEventType()) {
        case HyperTrackEvent.EventType.LOCATION_CHANGED_EVENT:
            Log.d(TAG, "onEvent: Location Changed");
            HyperTrackLocation hyperTrackLocation = event.getLocation();
            LatLng latLng = hyperTrackLocation.getLatLng();
            updateCurrentLocationMarker(event.getLocation());
            break;
    }
}
}