Created
December 12, 2014 23:51
-
-
Save eneim/a2df7d0e16dd93a016f9 to your computer and use it in GitHub Desktop.
IntentService that process the Activity Recognition Response/Result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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