<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>
public class MyActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener
private ActivityRecognitionClient activityRecognitionClient; private PendingIntent pIntent; private BroadcastReceiver receiver; private TextView tvActivity;
@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); }
@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); }
public class ActivityRecognitionService extends IntentService
@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); } }
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 ""; }
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
Source: https://habr.com/ru/post/230595/