Skip to content

Instantly share code, notes, and snippets.

@javawolfpack
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javawolfpack/e408b1cc29c6aa506176 to your computer and use it in GitHub Desktop.
Save javawolfpack/e408b1cc29c6aa506176 to your computer and use it in GitHub Desktop.
Code from lecture 4/7/15
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
/**
* Created by bryandixon on 4/6/15.
*/
public class ActivityDetectionIntentService extends IntentService {
private static final String TAG = "ThesisProject-AD";
public ActivityDetectionIntentService() {
super("ActivityDetectionIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(intent)){
ActivityRecognitionResult activityRecognitionResult = ActivityRecognitionResult.extractResult(intent);
DetectedActivity detectedActivity = activityRecognitionResult.getMostProbableActivity();
int confidence = detectedActivity.getConfidence();
String recognizeActivity = getActivityName(detectedActivity.getType());
/*
* If confidence in recognized activity is over 50% use the detected activity.
* This is an artificial constraint I chose for the system
*/
if(confidence>50) {
Log.d(TAG, "Confidence : " + confidence);
Log.d(TAG, "RecognizedActivity : " + recognizeActivity);
if(recognizeActivity=="STILL"){
if(/*No location*/) {
//Request location update
}
}
else if(recognizeActivity=="WALKING" || recognizeActivity=="ON_FOOT" || recognizeActivity=="TILTING"){
//user in motion slow so request location update
}
else{
//setlocation to null;
}
}
}
}
/**
* Method to convert DetectedActivity int constants into strings
*/
private String getActivityName(int activityType){
switch (activityType){
case DetectedActivity.IN_VEHICLE:
return "IN_VEHICLE";
case DetectedActivity.ON_BICYCLE:
return "ON_BICYCLE";
case DetectedActivity.STILL:
return "STILL";
case DetectedActivity.TILTING:
return "TILTING";
case DetectedActivity.RUNNING:
return "RUNNING";
case DetectedActivity.ON_FOOT:
return "ON_FOOT";
case DetectedActivity.WALKING:
return "WALKING";
case DetectedActivity.UNKNOWN:
return "UNKNOWN";
}
return "";
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.github.satyan:sugar:1.3'
}
/*global to class definitions*/
// Provides the entry point to Google Play services.
protected GoogleApiClient mGoogleApiClient;
//Frequency in milliseconds activity detection is set to.
private static int DETECTION_INTERVAL_IN_MILLISECONDS = 60000;
/*Code that w/ GoogleApiClient that calls the ActivityDetectionIntentService class in my onCreate()*/
GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to GoogleApiClient");
Intent intent = new Intent(getBaseContext(), ActivityDetectionIntentService.class);
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final PendingResult<Status> statusPendingResult =
ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, DETECTION_INTERVAL_IN_MILLISECONDS, pendingIntent);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
};
GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
};
GoogleApiClient.Builder googleApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(connectionCallbacks)
.addOnConnectionFailedListener(onConnectionFailedListener); //this is refer to connectionCallbacks interface implementation. .addOnConnectionFailedListener(this) //this is refer to onConnectionFailedListener interface implementation. .build();googleApiClient.connect();
mGoogleApiClient = googleApiClient.build();
mGoogleApiClient.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment