Exception in using LocationClient with a service

  • Replies:0
student
  • Forum posts: 2

May 20, 2014, 7:01:13 AM via Website

Hello there. I write a service that should work with LocationClient (Google Play Services). I have an IntentService and an AlarmReceiver (to get location periodically). I have an exception that I cannot catch. Please help!

AlarmReceiver.java
[CODE]package com.afusionlocation;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,LocationListener {

private LocationClient locationclient;
private LocationRequest locationrequest;
private Intent mIntentService;
private PendingIntent mPendingIntent;

@Override
public void onReceive(Context context, Intent intent) {

    mIntentService = new Intent(context,MyLocationService.class);
    mPendingIntent = PendingIntent.getService(context, 1, mIntentService, 0);

    int resp = -1;
    try{
    resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    }
    catch (Exception ex)
    {
        Log.d("Error","onReceive(): exception: " +ex.toString());
    }
    if(resp == ConnectionResult.SUCCESS){
        locationclient = new LocationClient(context,this,this);
        locationclient.connect();       
    }
    else{
        Log.e("Error","Google Play Service Error: resp="+resp);
    }   
 }

@Override
public void onLocationChanged(Location arg0) {

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

}

@Override
public void onConnected(Bundle arg0) {

    locationrequest = LocationRequest.create();
    locationrequest.setInterval(10000);
    try{    
     locationclient.requestLocationUpdates(locationrequest, mPendingIntent); //HERE EXCEPTION that I cannot catch
    }
    catch(Exception ex)
    {
        Log.e("Error","Exception: "+ex.toString());
    }       
}
@Override
public void onDisconnected() {

}

}[/CODE]

MyLocationService.java
[CODE]package com.afusionlocation;

import com.google.android.gms.location.LocationClient;
import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.util.Log;

public class MyLocationService extends IntentService {

public MyLocationService(String name) {
    super(name);
}
@Override
protected void onHandleIntent(Intent intent) {

        Location location = intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
        if(location !=null){
            Log.i("Error", "onHandleIntent " + location.getLatitude() + "," + location.getLongitude());
        }   
}

}[/CODE]

Reply