• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Exception in using LocationClient with a service

student71

Lurker
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() {
		
	}
}

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());
			}	
	}
}
 
Can you debug to the point that you have some idea of where the exception is being thrown? Or is someone going to have to duplicate your code and hope it throws an exception, so he can debug it for you?

(Hint: Put the entire class in a try/catch block and see what the exception is. If the entire class in in the block and you can't catch the exception, it's either in a different class or it's not an exception - or you've made a very important discovery and the ACM would probably be very interested in how Java can throw an uncatchable exception).
 
Back
Top Bottom