0

I'm developing an app that communicates with three remote services on my phone. For the communication i'm trying to figure out which method is the best (intents or AIDL). I receive a stream via bluetooth in a rate of 4 streams/second with 15 bytes each and now i want to add my gps location in order to execute data logging. I need my gps location every second. Since the time in requestLocationUpdates is just a hint, i created a timer which asks for the last known location every second.

Unfortunately I'm getting an error that I can't fix when i try to communicate using AIDL. The strange of this is, when i'm using intents to pass the received data to the remote services all works fine.

Can anyone help me out ?

Here my code:

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; //meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; //in Milliseconds
    private final MyLocationListener listener;
    private final LocationManager mLocationManager;
    private final List<String> providers;


    public Parser() {
         mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
         listener = new MyLocationListener();
         listener.onProviderEnabled(LocationManager.GPS_PROVIDER);
         mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
                listener);
         providers = mLocationManager.getProviders(true);
         timer = new Timer();
    }

    public boolean parse(byte[] data) {
         gpsUpdater();
         ...
    }

    private void gpsUpdater() {
        timer.scheduleAtFixedRate(new TimerTask() {
            Location l = null;

            @Override
            public void run() {
                for(String s: providers) {
                    l = mLocationManager.getLastKnownLocation(s);
                    if(l != null) {
                        gps = true;
                        latitude = l.getLatitude();
                        longitude = l.getLongitude();
                    }
                }
            }
       }, 0, MINIMUM_TIME_BETWEEN_UPDATES);
   }


   public class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location arg0) {
        //TODO Auto-generated method stub
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

and here is the error that i got:

04-06 15:06:43.015: E/AndroidRuntime(844): FATAL EXCEPTION: Timer-0 04-06 15:06:43.015: E/AndroidRuntime(844): java.lang.SecurityException: Binder invocation to an incorrect interface 04-06 15:06:43.015: E/AndroidRuntime(844): at android.os.Parcel.readException(Parcel.java:1322) 04-06 15:06:43.015: E/AndroidRuntime(844): at android.os.Parcel.readException(Parcel.java:1276) 04-06 15:06:43.015: E/AndroidRuntime(844): at android.location.ILocationManager$Stub$Proxy.getLastKnownLocation(ILocationManager.java:896) 04-06 15:06:43.015: E/AndroidRuntime(844): at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1020) 04-06 15:06:43.015: E/AndroidRuntime(844): at pt.ua.droidshark.handler.Parser$1.run(Parser.java:446) 04-06 15:06:43.015: E/AndroidRuntime(844): at java.util.Timer$TimerImpl.run(Timer.java:284)

Could it be, that the location manager uses a kind of aidl to provide my app with the location and the phone can't handle such amount of information when all the communication is made using AIDL ? I can't understand why it works fine when I use intents and got this error always when i use AIDL.

PS: Since someone suggested that the cause could be that the timer isn't in a service or activity class, I created a service in which i execute the code and i got the same situation. Works fine when all communication is made using intents and got the error when using AIDL.

João Nunes
  • 711
  • 4
  • 11
  • 22

2 Answers2

2

I think you're implementing a reccuring task (timer.scheduleAtFixedRate() ) in a class that is not an activity or service. that might be the cause

1) Either you implement the listeners in an activity that stays alive so the location is updated according to the parameters set in requestLocationUpdates(), but this leaves the gps on and eats the battery

2) You set up a timer in your activity and you poll location from your activity using an implementation with callbacks (see this example: What is the simplest and most robust way to get the user's current location on Android?)

3) You can implement a service and there is this compoment made by Commonsware that will do the job for you: https://github.com/commonsguy/cwac-locpoll

Community
  • 1
  • 1
znat
  • 13,144
  • 17
  • 71
  • 106
0

Make sure you've added the correct permissions to your AndroidManifest.xml file.

Does it include this tag?

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
slayton
  • 20,123
  • 10
  • 60
  • 89