Google Play Services Location tutorial part 2

by - 13:03

Hey,
First of all take a deep look at part 1 available here.
Instead what's new on part 2? Well basically two things:

  • in part 1 we built an app to receive constant location update, so this time we will see how to get just on time only
  • we will see how activity recognition works

Part 1 - Getting the last known location

So, how to receive the last gps location once from the google api services?
Follow every instruction of the part 1 and then on the onConnected method write this:

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        mLatitude.setText(String.valueOf(mLastLocation.getLatitude()));
        mLatitude.setText(String.valueOf(mLastLocation.getLongitude()));
    }
}

Part 2 - Activity Recognition

Add the permission in your app manifest file:
<uses-permission android:name="com.google.android.gms.permTission.ACTIVITY_RECOGNITION" />
Then to handle the activity recognition in background we are gonna use an IntentService:
public class DetectedActivitiesIntentService extends IntentService {
    protected static final String TAG = "detections_is";
    public static final String BROADCAST_ACTION = "com.maurocerbai.googleaudacitylocationpart2.BROADCASTACTION";
    public static final String ACTIVITY_EXTRA = "com.maurocerbai.googleaudacitylocationpart2.ACTIVITYEXTRA";
    public DetectedActivitiesIntentService() {
        super(TAG);
    }
    @Override    protected void onHandleIntent(Intent intent) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        Intent localIntent = new Intent(BROADCAST_ACTION);
        ArrayList <DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();
        localIntent.putExtra(ACTIVITY_EXTRA,detectedActivities);
        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
    }
}
Don't forget to declare this service in the manifest:
<service android:name=".DetectedActivitiesIntentService"    android:exported="false" />
In your main activity build your goolge api connection as seen in part 1 but instead of location use activityrecognition as shown here:
private void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}
and add the possibility to listen to the broadcast you made before in a inner class:
class ActivityDetectionBroadcastReceiver extends BroadcastReceiver{
    @Override    public void onReceive(Context context, Intent intent) {
        ArrayList <DetectedActivity> detectedActivities = intent.getParcelableArrayListExtra(Constants.ACTIVITY_EXTRA);
        String res = "";
        for (DetectedActivity d:detectedActivities)
            res+=d.getType()+" "+d.getConfidence();
        text_label.setText(res);
    }
}
Then a this two method and link them to the button of the view:
public void requestActivityUpdateButtonHandler (View view){
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient,1000,getActivityDetectionPendingIntent()).setResultCallback(this);
}
public void removeActivityUpdateButtonHandler (View view){
    ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mGoogleApiClient,getActivityDetectionPendingIntent()).setResultCallback(this);
}
Don't forget to handle the activity lifecycle adding this two method:
@Override protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(mActivityDetectionBroadcastReceiver,new IntentFilter(Constants.BROADCAST_ACTION));
}
@Override protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mActivityDetectionBroadcastReceiver);
    super.onPause();
}
DONE. Good luck testing it because the emulator is not very helpful in this case. Maybe using a GPX file you can simulate the movements using the latest version of the sdk tools.
You can find the code here : https://bitbucket.org/mcmaur/googleaudacitylocationpart2

You May Also Like

0 commenti