In this article, I will teach you how to detect user activity through android app, you can detect these user activities like still, walking, running, cycling, tilting, driving etc. In this article, we will be using ActivityRecognitionClient API. This API is frequently used in fitness app for taking user information about number of steps taken and how much distance user travelled in km. So if you learn, this API it will be very useful for you in future.
![]() |
recognition in android studio |
Download Demo of this app from play store
Practical Implementation:
I am using background service for this app, which means i will fetch user activity in background and your app will fetching user activity when app is in background state. so you need to stop fetching user activity explicitly.
1. Add this permission in your manifest file.
2. Add this dependency in gradle file and sync your project.
3. Create class for background services
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
implementation 'com.google.android.gms:play-services-location:17.0.0'
3. Create class for background services
4. Create class for constant
5. Create class for broadcasting user activity
5. Create class for broadcasting user activity
6. Create main activity, this is main class. All control are given to this activity and from you can initialize background services and use user activity broadcasting.
Thats it :) Any question and issue, Feel free to ask anything . Thank you
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
BroadcastReceiver broadcastReceiver;
private TextView txtActivity, txtConfidence;
private ImageView imgActivity;
private Button btnStartTrcking, btnStopTracking;
BackgroundDetectedActivitiesService ab;
static int v = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtActivity = findViewById(R.id.txt_activity);
txtConfidence = findViewById(R.id.txt_confidence);
imgActivity = findViewById(R.id.img_activity);
btnStartTrcking = findViewById(R.id.btn_start_tracking);
btnStopTracking = findViewById(R.id.btn_stop_tracking);
TextView pri = (TextView) findViewById(R.id.textView);
pri.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this , privacy_policy.class));
}
});
btnStartTrcking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ab = new BackgroundDetectedActivitiesService();
// services not running already
// start services
if (!isMyServiceRunning(ab.getClass())) {
txtActivity.setText("Loading ..... ");
txtConfidence.setText("");
imgActivity.setImageResource(android.R.drawable.screen_background_light_transparent);
v =0;
startTracking();
}
else{
Toast.makeText(MainActivity.this, "Service Already Running", Toast.LENGTH_LONG).show();
}
}
});
btnStopTracking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopTracking();
}
});
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.BROADCAST_DETECTED_ACTIVITY)) {
int type = intent.getIntExtra("type", -1);
int confidence = intent.getIntExtra("confidence", 0);
handleUserActivity(type, confidence);
}
}
};
}
private void handleUserActivity(int type, int confidence) {
String label = getString(R.string.activity_unknown);
int icon = R.drawable.ic_still;
switch (type) {
case DetectedActivity.IN_VEHICLE: {
label = getString(R.string.activity_in_vehicle);
icon = R.drawable.ic_driving;
break;
}
case DetectedActivity.ON_BICYCLE: {
label = getString(R.string.activity_on_bicycle);
icon = R.drawable.ic_on_bicycle;
break;
}
case DetectedActivity.ON_FOOT: {
label = getString(R.string.activity_on_foot);
icon = R.drawable.ic_walking;
break;
}
case DetectedActivity.RUNNING: {
label = getString(R.string.activity_running);
icon = R.drawable.ic_running;
break;
}
case DetectedActivity.STILL: {
label = getString(R.string.activity_still);
icon = R.drawable.ic_still;
break;
}
case DetectedActivity.TILTING: {
label = getString(R.string.activity_tilting);
icon = R.drawable.ic_tilting;
break;
}
case DetectedActivity.WALKING: {
label = getString(R.string.activity_walking);
icon = R.drawable.ic_walking;
break;
}
case DetectedActivity.UNKNOWN: {
label = getString(R.string.activity_unknown);
icon = R.drawable.unknown;
break;
}
}
Log.e(TAG, "User activity: " + label + ", Confidence: " + confidence);
if(v ==0 ){
v = confidence;
txtActivity.setText(label);
txtConfidence.setText("Confidence: " + confidence);
imgActivity.setImageResource(icon);
}
else{
if(confidence > 50){
v = confidence;
txtActivity.setText(label);
txtConfidence.setText("Confidence: " + confidence);
imgActivity.setImageResource(icon);
}
else if (v <= confidence) {
txtActivity.setText(label);
txtConfidence.setText("Confidence: " + confidence);
imgActivity.setImageResource(icon);
}
}
}
// check your background services
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.i ("Service status", "Running");
return true;
}
}
Log.i ("Service status", "Not running");
return false;
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,
new IntentFilter(Constants.BROADCAST_DETECTED_ACTIVITY));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
}
private void startTracking() {
Intent intent = new Intent(MainActivity.this, BackgroundDetectedActivitiesService.class);
startService(intent);
}
private void stopTracking() {
Intent intent = new Intent(MainActivity.this, BackgroundDetectedActivitiesService.class);
stopService(intent);
}
}
Thats it :) Any question and issue, Feel free to ask anything . Thank you