Код: Выделить всё
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
ensurePermissionsGranted();
textView = findViewById(R.id.textView1);
button1 = findViewById(R.id.button1);
activityRecognitionClient = ActivityRecognition.getClient(this);
//prepare the PendingIntent
Intent intent = new Intent(this, BikeReceiver.class);
activityPendingIntent = PendingIntent.getBroadcast(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
button1.setOnClickListener(v -> {
String text = button1.getText().toString();
if (text.equals("Start")) {
startTracking();
button1.setText("Stop");
} else {
//stop tracking
stopTracking();
button1.setText("Start");
}
});
}
Код: Выделить всё
private void startTracking() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Toast.makeText(this, "permission to track activity changes is required", Toast.LENGTH_LONG).show();
return;
}
activityRecognitionClient.requestActivityUpdates(
5000, // detection interval in ms
activityPendingIntent
);
}
Код: Выделить всё
private void stopTracking() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
activityRecognitionClient.removeActivityUpdates(activityPendingIntent)
.addOnSuccessListener(aVoid -> {
Toast.makeText(this, "Tracking stopped", Toast.LENGTH_SHORT).show();
// Example: display distance
float totalDistance = BikeService.getTotalDistance(); // or SharedPreferences
textView.setText("Total Distance: " + totalDistance + " m");
})
.addOnFailureListener(e ->
Toast.makeText(this, "Failed to stop tracking", Toast.LENGTH_SHORT).show()
);
}
Код: Выделить всё
manifest
package="com.example.bikedistancetracker"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
Код: Выделить всё
public class BikeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int activityType = intent.getIntExtra("activity_type", -1);
if (activityType == DetectedActivity.ON_BICYCLE) {
// Start the distance tracking service
Intent serviceIntent = new Intent(context, BikeService.class);
ContextCompat.startForegroundService(context, serviceIntent);
}
else if (activityType == DetectedActivity.ON_FOOT) {
// Stop the distance tracking service
Intent serviceIntent = new Intent(context, BikeService.class);
context.stopService(serviceIntent);
}
}
};
Подробнее здесь: https://stackoverflow.com/questions/797 ... istance-in
Мобильная версия