How to use Google Location service API to get location updates even in background?

非 Y 不嫁゛ 提交于 2019-12-12 01:45:33

问题


First of all this is not a duplicate of any other question on the site. I've seen location updates using Service and Google API both.

  • Here is the link which uses the service to get location updates
  • This is the link which uses Google API for the same. This works perfectly.

I've tried both, but in case of service, it is not working. But the 1st link's code is working perfectly fine, and gives location updates correctly. But I think It can't get update when the app is in background.

I want that code inside a service, which can continuously get location updates even my app is in background.

But can't figure out how to merge this two codes. If you are asking what I've tried? Than I've just copy the common methods in the service class. But it gives me too many errors :(

If there are alternative available for this please suggest me. I'm new to android.

Thanks in advance!


回答1:


Try:

Service.java

import android.app.Service;
import android.content.Context;
import android.content.Intent;
 import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationServices;


import java.io.IOException;
import java.io.OutputStreamWriter;

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



private GoogleApiClient mGoogleApiClient;

private LocationRequest mLocationRequest;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    mGoogleApiClient.connect();

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {


    mGoogleApiClient.disconnect();
    super.onDestroy();



}


@Override
public void onConnected(Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000); // Update location every second

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(this,"GoogleApiClient connection has been suspend",Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(this,"GoogleApiClient connection has failed",Toast.LENGTH_LONG).show();
}

and MainActivity.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this,GPSService.class);
    startService(i);


}


来源:https://stackoverflow.com/questions/39327713/how-to-use-google-location-service-api-to-get-location-updates-even-in-backgroun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!