Reputation: 71
I have used GoogleApiClient for getting GPS location. i have create one service and every 10 seconds getting data from GPS but the problem is there are so many times onConnectionSuspended() called and i am not geeting any GPS data. following is my service code that i am getting GPS location
<service
android:name="com.GPSService"
android:enabled="true"
android:exported="false"
android:process=":ServiceProcess"
android:stopWithTask="false" />
Service code
public class GPSService extends Service implements LocationListener, ConnectionCallbacks, OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
protected GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
protected boolean requestingLocationUpdate = true;
private Location mLastLocation;
// Location updates intervals in sec
private static int UPDATE_INTERVAL = 10000; // 10 sec
private static int FASTEST_INTERVAL = 5000; // 5 sec
private static int DISPLACEMENT = 5; // 5 meters
private static final String LOCATION = "Location Not Available";
private double latitude;
private double longitude;
private String mLastUpdateTime;
Intent intents;
RequestParams requestParams = null;
private static final String TAG = "GPSService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
helper.insertServiceLog("oncreate");
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d("GPSService", "onCreate");
//
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onDestroy() {
Log.i("MyReceiver", "onTaskRemoved called Oooooooooooooppppssssss!!!!");
mGoogleApiClient.disconnect();
}
@Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d("GPSService", "onStart");
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected() && requestingLocationUpdate) {
startLocationUpdates();
}
}
@Override
public final int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onLocationChanged(Location loc) {
//String imei = "2";
mLastLocation = loc;
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
if (requestingLocationUpdate) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
//LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
startLocationUpdates();
Toast.makeText(this, "Started the location update", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int arg0) {
Log.d("GPSService", "onConnectionSuspended called");
}
/**
* Starting the location updates
*/
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.i("MyReceiver", "onTaskRemoved called Oooooooooooooppppssssss!!!!");
super.onTaskRemoved(rootIntent);
}
}
can somebody please help me to figure out this issue.
Upvotes: 1
Views: 76
Reputation: 4123
Is there a particular reason why you're running the service in a different process? I think it might be related that. Try changing the declaration of the service to
<service
android:name="com.GPSService"
android:enabled="true"
android:exported="false" />
Upvotes: 1