📜 ⬆️ ⬇️

ActivityRecognitionClient from the Google Play Services Library - "recognition of user actions"

Foreword


A new component has sneaked into one of the updates to the Google Play Services library, which can be very useful when implementing some type of application. The name of this component is ActivityRecognitionClient .

ActivityRecognitionClient allows an application to retrieve user activity data. For example, an application can determine the user's current action, for example, walking, driving in a car, reading a book, and even cycling.
Many have already managed to guess that the mechanism works due to the sensors of the device. However, despite the use of multiple sensors, the power consumption of the component is very low, which allows it to be used without restrictions.

I hope I explained it clearly, but for clarity, I suggest watching the 4-minute video provided by Google:
')


Implementation


Let's start the implementation and outline an example of use, but first, do not forget to connect the Google Play Services library to your project.

Add a TextView to main.xml to display the current action and its accuracy:

<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> 


Let's start writing the code of our MyActivity.java , which should implement the interfaces GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener.

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


Declare objects:

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


In the onCreate method, we initialize the previously declared objects and check for the presence of Google Play Services on the device:

  @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); } 


In the onConnected method, we associate our activityRecognitionClient with an ActivityRecognitionService , the implementation code of which will be presented below:

  @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); } 


Finally, go to the implementation of the ActivityRecognitionService.java , which inherits the IntentService class:

 public class ActivityRecognitionService extends IntentService 


Override the onHandleIntent method:
  @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); } } 


We add the getType method, which returns us the current state:

 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 ""; } 


Do not forget to add permission in AndroidManifest.xml :

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


Demonstration


image
Screenshots
image

image

image


Conclusion


In principle, ActivityRecognitionClient seemed to me a very funny and useful thing, but testing on several devices showed that the “action / state” is not always determined adequately.

I hope I have not forgotten anything.

If you have questions, ask.

See the complete source for GitHub .

Source: https://habr.com/ru/post/230595/


All Articles