Я пытаюсь отслеживать действия пользователей с помощью Service + BroadCastReceiver.
ActivityTrackingService. java
public class ActivityTrackingService extends Service {
private static final String TAG = "ActivityTracking";
public static ActivityTrackingService instance;
public static boolean isStarted() {
return instance != null;
}
public ActivityTrackingService() {
super();
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate onCreate!");
super.onCreate();
instance = this;
}
@Override
public void onDestroy() {
Log.d(TAG, "Updates stopped!");
ActivityRecognition.getClient(this).removeActivityTransitionUpdates(activityPendingIntent);
super.onDestroy();
}
private static List<ActivityTransition> transitions = new ArrayList<>();
private static PendingIntent activityPendingIntent;
private static ActivityTransitionRequest transitionRequest;
void initActivityTransition(){
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.IN_VEHICLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.WALKING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.STILL)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_BICYCLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.ON_BICYCLE)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.RUNNING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER)
.build());
transitions.add(
new ActivityTransition.Builder()
.setActivityType(DetectedActivity.RUNNING)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT)
.build());
Log.d(TAG,"initActivityTransition " + transitions.size());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
initActivityTransition();
ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);
Intent mintent = new Intent(this, ActivityTransitionBroadcastReceiver.class);
mintent.setAction(ActivityTransitionBroadcastReceiver.INTENT_ACTION);
activityPendingIntent = PendingIntent.getBroadcast(this, 0, mintent,
PendingIntent.FLAG_UPDATE_CURRENT);
Task<Void> task = ActivityRecognition.getClient(this)
.requestActivityTransitionUpdates(request, activityPendingIntent);
task.addOnSuccessListener(command -> Log.i(TAG, "Successfully added transition updates listener"));
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
ActivityTransitionBroadcastReceiver. java
public class ActivityTransitionBroadcastReceiver extends BroadcastReceiver {
public static final String INTENT_ACTION = "package.ACTION_PROCESS_ACTIVITY_TRANSITIONS";
private static final String TAG = "ActivityTracking";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && INTENT_ACTION.equals(intent.getAction())) {
if (ActivityTransitionResult.hasResult(intent)) {
ActivityTransitionResult intentResult = ActivityTransitionResult
.extractResult(intent);
Log.i(TAG, "RECEIVED BROADCAST!");
}
}
}
}
AndroidManifest. xml
<service android:name=".service.services.ActivityTrackingService"></service>
<receiver android:name=".service.services.ActivityTransitionBroadcastReceiver"
android:exported="false"
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION">
<intent-filter>
<action android:name="package.ACTION_PROCESS_ACTIVITY_TRANSITIONS" />
</intent-filter>
</receiver>
Выход журнала:
2020-02-26 16:38:06.503 29363-29363/package D/ActivityTracking: onCreate onCreate!
2020-02-26 16:38:06.510 29363-29363/package D/ActivityTracking: initActivityTransition 10
2020-02-26 16:38:07.014 29363-29363/package I/ActivityTracking: Successfully added transition updates listener
Сама служба запущена и SuccessListener of Задача ActivityRecognitionClient выполнена правильно (см. вывод журнала). Однако BroadcastReceiver никогда ничего не получает. Есть предложения?