Last active
September 8, 2017 10:20
-
-
Save amay077/5603028 to your computer and use it in GitHub Desktop.
Google I/O 2013 で発表された行動認識(Activity Recognition)を使ってみる ref: http://qiita.com/amay077/items/a6b4ff0d69dd4d787ce2
This file contains hidden or 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
| <?xml version="1.0" encoding="utf-8"?> | |
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.example.activityrecognizingsample" | |
| android:versionCode="1" | |
| android:versionName="1.0" > | |
| <uses-sdk | |
| android:minSdkVersion="8" | |
| android:targetSdkVersion="17" /> | |
| <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/> | |
| <application | |
| android:allowBackup="true" | |
| android:icon="@drawable/ic_launcher" | |
| android:label="@string/app_name" | |
| android:theme="@style/AppTheme" > | |
| <activity | |
| android:name=".MainActivity" | |
| android:label="@string/app_name" | |
| android:screenOrientation="portrait"> | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| <service | |
| android:name=".ReceiveRecognitionIntentService" | |
| android:label="@string/app_name" | |
| android:exported="false" /> | |
| </application> | |
| </manifest> |
This file contains hidden or 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 com.example.activityrecognizingsample; | |
| import com.google.android.gms.common.ConnectionResult; | |
| import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; | |
| import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; | |
| import com.google.android.gms.location.ActivityRecognitionClient; | |
| import com.google.android.gms.location.DetectedActivity; | |
| import android.os.Bundle; | |
| import android.app.Activity; | |
| import android.app.PendingIntent; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.IntentFilter; | |
| import android.text.format.DateFormat; | |
| import android.view.Menu; | |
| import android.view.View; | |
| import android.view.View.OnClickListener; | |
| import android.widget.Button; | |
| import android.widget.TextView; | |
| public class MainActivity extends Activity { | |
| private ActivityRecognitionClient _recClient; // 行動認識のメインクラス | |
| private TextView _textResult; // 認識結果を表示するところ | |
| // 認識結果は PendingIntent で通知してくれる | |
| // PendingIntent に、Service を起動する Intent を仕込んでおいて、 | |
| // 認識結果の取得はそっちで行う。 > ReceiveRecognitionIntentService.java | |
| private PendingIntent _receiveRecognitionIntent; | |
| // ReceiveRecognitionIntentService で取得した認識結果は、Broadcast で通知されるので、 | |
| // それを受け取る Receiver 。ここで画面に認識結果を表示する。 | |
| private final BroadcastReceiver _receiveFromIntentService = new BroadcastReceiver() { | |
| @Override | |
| public void onReceive(Context context, Intent intent) { | |
| final int activityType = intent.getIntExtra("activity_type", 0); | |
| final int confidence = intent.getIntExtra("confidence", -1); | |
| final long time = intent.getLongExtra("time", 0); | |
| MainActivity.this.runOnUiThread(new Runnable() { | |
| @Override | |
| public void run() { | |
| String text = _textResult.getText().toString(); | |
| text = DateFormat.format("hh:mm:ss.sss", time) + " - " | |
| + getNameFromType(activityType) + "(" + | |
| + confidence + ")" + "\n" + text; | |
| _textResult.setText(text); | |
| } | |
| }); | |
| } | |
| // http://developer.android.com/training/location/activity-recognition.html | |
| // からパクってきた関数 | |
| /** | |
| * Map detected activity types to strings | |
| *@param activityType The detected activity type | |
| *@return A user-readable name for the type | |
| */ | |
| private String getNameFromType(int activityType) { | |
| switch(activityType) { | |
| case DetectedActivity.IN_VEHICLE: | |
| return "in_vehicle"; | |
| case DetectedActivity.ON_BICYCLE: | |
| return "on_bicycle"; | |
| case DetectedActivity.ON_FOOT: | |
| return "on_foot"; | |
| case DetectedActivity.STILL: | |
| return "still"; | |
| case DetectedActivity.UNKNOWN: | |
| return "unknown"; | |
| case DetectedActivity.TILTING: | |
| return "tilting"; | |
| } | |
| return "unknown - " + activityType; | |
| } | |
| }; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| _textResult = (TextView)findViewById(R.id.text_results); | |
| // IntentService から Broadcast される認識結果を受け取るための Receiver を登録しておく | |
| registerReceiver(_receiveFromIntentService, new IntentFilter("receive_recognition")); | |
| final Button buttonStart = (Button)findViewById(R.id.button_start); | |
| buttonStart.setOnClickListener(new OnClickListener() { | |
| private boolean _isStarted = false; | |
| @Override | |
| public void onClick(View v) { | |
| if (!_isStarted) { | |
| startReckoning(); | |
| buttonStart.setText("Stop"); | |
| } else { | |
| stopReckoning(); | |
| buttonStart.setText("Start"); | |
| } | |
| _isStarted = !_isStarted; | |
| } | |
| }); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| stopReckoning(); | |
| // ConnectionCallbacks.onDisconnected が呼ばれるまで待った方がいい気がする | |
| unregisterReceiver(_receiveFromIntentService); | |
| super.onDestroy(); | |
| } | |
| private void startReckoning() { | |
| _recClient = new ActivityRecognitionClient(this, new ConnectionCallbacks() { | |
| @Override | |
| public void onConnected(Bundle bundle) { | |
| Intent intent = new Intent( | |
| MainActivity.this, ReceiveRecognitionIntentService.class); | |
| _receiveRecognitionIntent = PendingIntent.getService( | |
| MainActivity.this, 0, intent, | |
| PendingIntent.FLAG_UPDATE_CURRENT); | |
| // 2. 行動認識開始! | |
| // 1秒間隔で認識間隔を通知。 | |
| // 認識したら ReceiveRecognitionIntentService が呼び出されるようにしている。 | |
| _recClient.requestActivityUpdates(1000, _receiveRecognitionIntent); | |
| } | |
| @Override | |
| public void onDisconnected() { | |
| _recClient = null; // NOTE disconnect してもここにこないよ? | |
| } | |
| }, new OnConnectionFailedListener() { | |
| @Override | |
| public void onConnectionFailed(ConnectionResult result) { | |
| // 接続でエラーが発生したらここにくるらしい | |
| } | |
| }); | |
| // 1. 行動認識サービスに接続! | |
| _recClient.connect(); | |
| } | |
| private void stopReckoning() { | |
| if (_recClient == null || !_recClient.isConnected()) { | |
| return; | |
| } | |
| _recClient.removeActivityUpdates(_receiveRecognitionIntent); | |
| _recClient.disconnect(); | |
| // ConnectionCallbacks.onDisconnected が呼ばれるまで待った方がいい気がする | |
| } | |
| } |
This file contains hidden or 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 com.example.activityrecognizingsample; | |
| import com.google.android.gms.location.ActivityRecognitionResult; | |
| import com.google.android.gms.location.DetectedActivity; | |
| import android.app.IntentService; | |
| import android.content.Intent; | |
| import android.text.format.DateFormat; | |
| import android.util.Log; | |
| /** | |
| * 行動認識結果を取得するための IntentService | |
| * | |
| * ActivityRecognitionClient.requestActivityUpdates に仕込んでおくと | |
| * 認識結果を受信する度にこれが呼ばれる。 | |
| * | |
| */ | |
| public class ReceiveRecognitionIntentService extends IntentService { | |
| private static final String TAG = "ReceiveRecognitionIntentService"; | |
| public ReceiveRecognitionIntentService() { | |
| super("ReceiveRecognitionIntentService"); | |
| } | |
| @Override | |
| protected void onHandleIntent(Intent intent) { | |
| if (!ActivityRecognitionResult.hasResult(intent)) { | |
| // 行動認識結果持ってないよ | |
| 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