Google Play ServicesライブラリのActivityRecognitionClient-「ユーザーアクションの認識」

まえがき



Google Play Servicesライブラリーの更新の1つは、何らかのタイプのアプリケーションを実装するときに非常に役立つ新しいコンポーネントを提供しています。 このコンポーネントの名前はActivityRecognitionClientです。



ActivityRecognitionClientを使用すると、アプリケーションはユーザーアクションに関するデータを取得できます。 たとえば、アプリケーションは、ウォーキング、車に乗る、本を読む、サイクリングなど、ユーザーの現在のアクションを判断できます。

多くの人はすでに、デバイスのセンサーが原因でメカニズムが機能すると推測しています。 ただし、複数のセンサーを使用しているにもかかわらず、コンポーネントの消費電力は非常に低いため、制限なく使用できます。



明確に説明したいと思いますが、明確にするために、Googleから提出された4分間のビデオを視聴することを提案します。







実装



使用を開始して使用例の概要を説明しましょう。最初に、 Google Play Servicesライブラリをプロジェクトに接続することを忘れないでください。



Text.View main.xmlに追加して、現在のアクションとその精度を表示します。



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="20dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="20dp"> <TextView android:id="@+id/tvActivity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:text=""/> </RelativeLayout>
      
      







MyActivity.javaのコードを書き始めています。これは、インターフェースGooglePlayServicesClient.ConnectionCallbacksおよびGooglePlayServicesClient.OnConnectionFailedListenerを実装する必要があります。



 public class MyActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener
      
      







オブジェクトを宣言します:



  private ActivityRecognitionClient activityRecognitionClient; private PendingIntent pIntent; private BroadcastReceiver receiver; private TextView tvActivity;
      
      







onCreateメソッドで、以前に宣言されたオブジェクトを初期化し、デバイス上のGoogle Play開発者サービスの可用性を確認します。



  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvActivity = (TextView) findViewById(R.id.tvActivity); int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if(resp == ConnectionResult.SUCCESS){ activityRecognitionClient = new ActivityRecognitionClient(this, this, this); activityRecognitionClient.connect(); } else { Toast.makeText(this, "Please install Google Play Service.", Toast.LENGTH_SHORT).show(); } receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String v = "Activity :" + intent.getStringExtra("Activity") + " " + "Confidence : " + intent.getExtras().getInt("Confidence") + "\n"; v += tvActivity.getText(); tvActivity.setText(v); } }; IntentFilter filter = new IntentFilter(); filter.addAction("com.alexche.recognitionservice.ACTIVITY_RECOGNITION_DATA"); registerReceiver(receiver, filter); }
      
      







onConnectedメソッドでは、activityRecognitionClientをActivityRecognitionServiceに関連付けます。その実装コードを以下に示します。



  @Override public void onConnected(Bundle arg0) { Intent intent = new Intent(this, ActivityRecognitionService.class); pIntent = PendingIntent.getService(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); activityRecognitionClient.requestActivityUpdates(1000, pIntent); }
      
      







最後に、IntentServiceクラスを継承するActivityRecognitionService.javaの実装に進みます。



 public class ActivityRecognitionService extends IntentService
      
      







onHandleIntentメソッドをオーバーライドします。

  @Override protected void onHandleIntent(Intent intent) { if(ActivityRecognitionResult.hasResult(intent)){ ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); Log.i(TAG, getType(result.getMostProbableActivity().getType()) + "t" + result.getMostProbableActivity().getConfidence()); Intent i = new Intent("com.alexche.recognitionservice.ACTIVITY_RECOGNITION_DATA"); i.putExtra("Activity", getType(result.getMostProbableActivity().getType()) ); i.putExtra("Confidence", result.getMostProbableActivity().getConfidence()); sendBroadcast(i); } }
      
      







現在の状態を返すgetTypeメソッドを追加します。



 private String getType(int type){ if(type == DetectedActivity.UNKNOWN) return "Unknown"; else if(type == DetectedActivity.IN_VEHICLE) return "In Vehicle"; else if(type == DetectedActivity.ON_BICYCLE) return "On Bicycle"; else if(type == DetectedActivity.ON_FOOT) return "On Foot"; else if(type == DetectedActivity.STILL) return "Still"; else if(type == DetectedActivity.TILTING) return "Tilting"; else return ""; }
      
      







AndroidManifest.xmlに許可を追加することを忘れないでください:



 <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
      
      







デモンストレーション



画像

スクリーンショット
画像



画像



画像





おわりに



原則として、ActivityRecognitionClientは私にとって非常に面白くて便利なもののように思えましたが、いくつかのデバイスでテストしたところ、「アクション/状態」が常に適切に決定されないことがわかりました。



何も忘れないでね



ご質問がある場合は、お問い合わせください。



GitHubの完全なソースコードを参照してください



All Articles