Skip to content

Instantly share code, notes, and snippets.

@eneim
Created December 12, 2014 23:51
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 eneim/a2df7d0e16dd93a016f9 to your computer and use it in GitHub Desktop.
Save eneim/a2df7d0e16dd93a016f9 to your computer and use it in GitHub Desktop.
IntentService that process the Activity Recognition Response/Result
package im.ene.lab.hakkify;
import android.app.IntentService;
import android.content.Intent;
import android.text.format.DateFormat;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
/**
* Created by eneim on 12/13/14.
*/
public class ActivityRecognitionReceiverIntentService extends IntentService {
private static final String TAG = "ActivityRecognitionReceiverIntentService";
public ActivityRecognitionReceiverIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
if (!ActivityRecognitionResult.hasResult(intent)) {
// 行動認識結果持ってないよ
Log.d("activity result", "no result");
return;
}
// 認識結果を取得する
ActivityRecognitionResult result =
ActivityRecognitionResult.extractResult(intent);
DetectedActivity mostProbableActivity = result.getMostProbableActivity();
int activityType = mostProbableActivity.getType();
int confidence = mostProbableActivity.getConfidence();
Log.d(TAG, "Receive recognition.");
Log.d(TAG, " activityType - " + activityType); // 行動タイプ
Log.d(TAG, " confidence - " + confidence); // 確実性(精度みたいな)
Log.d(TAG, " time - " + DateFormat.format("hh:mm:ss.sss", result.getTime())); // 時間
Log.d(TAG, " elapsedTime - " + DateFormat.format("hh:mm:ss.sss", result.getElapsedRealtimeMillis())); // よく分からん
// 画面に結果を表示するために、Broadcast で通知。
// MainActivity にしかけた BroadcastReceiver で受信する。
Intent notifyIntent = new Intent("receive_recognition");
notifyIntent.setPackage(getPackageName());
notifyIntent.putExtra("activity_type", activityType);
notifyIntent.putExtra("confidence", confidence);
notifyIntent.putExtra("time", result.getTime());
sendBroadcast(notifyIntent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment