gpt4 book ai didi

android - Activity 转换 API 不起作用

转载 作者:行者123 更新时间:2023-12-01 08:46:43 26 4
gpt4 key购买 nike

我希望使用新的 Activity 转换 API 并遵循教程 here我无法得到想要的结果。

这是我用于设置我希望检测的 Activity 转换的代码:

public void setActivityTransitions() {
transitionList = new ArrayList<>();
ArrayList<Integer> activities = new ArrayList<>(Arrays.asList(
DetectedActivity.STILL,
DetectedActivity.WALKING,
DetectedActivity.ON_FOOT,
DetectedActivity.RUNNING,
DetectedActivity.ON_BICYCLE,
DetectedActivity.IN_VEHICLE));
for (int activity :
activities) {
transitionList.add(new ActivityTransition.Builder()
.setActivityType(activity)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER).build());

transitionList.add(new ActivityTransition.Builder()
.setActivityType(activity)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT).build());

}

}

然后请求 Activity 转换更新:
 ActivityTransitionRequest activityTransitionRequest = new ActivityTransitionRequest(transitionList);
Intent intent = new Intent(context, ActivityDetectorTransitionService.class);
intent.setAction("com.test.activityrecognition.START_ACTIVITY_TRANSITION_DETECTION_ALARM");
PendingIntent pendingIntent = PendingIntent.getService(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Task<Void> task = ActivityRecognition.getClient(context).requestActivityTransitionUpdates(activityTransitionRequest, pendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
System.out.println("onSuccess");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
System.out.println("onFailure");
}
});

这是广播接收器:
    public class ActivityDetectorTransitionService extends BroadcastReceiver {
private static final String TAG = "ActivityDetectorTransitionService";

@Override
public void onReceive(Context context, Intent intent) {
if (ActivityTransitionResult.hasResult(intent)) {
ActivityTransitionResult activityTransitionResult = ActivityTransitionResult.extractResult(intent);
ActivityDetectorTransitionAPI.getInstance().handleActivityRecognitionResult(activityTransitionResult);
}
}
}

(该名称中包含服务,因为最初我保留了它的服务但仍然无法正常工作。)

并在 list 中:
<receiver
android:name=".tracking.activityrecognition.ActivityDetectorTransitionService">
<intent-filter>
<action android:name="com.test.activityrecognition.START_ACTIVITY_TRANSITION_DETECTION_ALARM"/>
</intent-filter>
</receiver>

最佳答案

您正在使用 PendingIntent.getService()结合 BroadcastReceiver .

使用 BroadcastReceiver 接收待处理的 Intent 您必须检索 PendingIntent使用 PendingIntent.getBroadcast() 的实例.可以找到有关 Intent 和 Intent 过滤器的相应开发人员指南 here .

从 Android 8 开始有几个 background service limitations .使用 IntentService仅在应用程序处于前台时有效。要在应用程序关闭后接收 Activity 转换更新,您甚至必须使用 BroadcastReceiver .为此,BroadcastReceiver正如 Jan Maděra 已经提到的,必须在具有相应权限的应用程序 list 中注册。

   <receiver android:name="com.mypackage.ActivityTransitionBroadcastReceiver"
android:exported="false"
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION">
<intent-filter>
<action android:name="com.mypackage.ACTION_PROCESS_ACTIVITY_TRANSITIONS" />
</intent-filter>
</receiver>

此外 onReceive()应该只响应您的具体操作,因为 intent filters are not guaranteed to be exclusive .

public class ActivityTransitionBroadcastReceiver extends BroadcastReceiver {

public static final String INTENT_ACTION = "com.mypackage" +
".ACTION_PROCESS_ACTIVITY_TRANSITIONS";

@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);
// handle activity transition result ...
}
}
}
}

使用 PendingIntent.getBroadcast() 请求 Activity 转换更新:

ActivityTransitionRequest request = new ActivityTransitionRequest(transitionList);

Intent intent = new Intent(context, ActivityTransitionBroadcastReceiver.class);
intent.setAction(ActivityTransitionBroadcastReceiver.INTENT_ACTION);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

Task<Void> task = ActivityRecognition.getClient(context)
.requestActivityTransitionUpdates(request, pendingIntent);

请注意, Activity 转换更新可能会延迟接收。这取决于设备,也可能受电源管理限制的影响。

关于android - Activity 转换 API 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51859096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com