gpt4 book ai didi

android - ActivityRecognitionIntentService onHandleIntent 仅偶尔触发

转载 作者:行者123 更新时间:2023-11-29 20:54:07 26 4
gpt4 key购买 nike

我正在尝试为我的应用获取 Activity 识别更新,但它似乎只能部分工作。一开始我以为它不工作但每隔一段时间调试日志就会显示一个 Activity 类型,这意味着 ActivityRecognitionIntentService onHandleIntent() 被击中但它从未进入 ActivityBroadCastReceiver

我在MainActivity中有如下代码

    Intent i = new Intent(this, ActivityRecognitionIntentService.class);
i.setAction("ActivityRecognitionIntentService");
PendingIntent activityRecognitionPendingIntent = PendingIntent.getService(this, 7422, i, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognitionApi rec = ActivityRecognition.ActivityRecognitionApi;
rec.requestActivityUpdates(mClient, 5000, activityRecognitionPendingIntent)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Successfully registered updates");
} else {
Log.i(TAG, "Failed to register updates");
}
}
});;

如果我做对了,那应该每 5 秒触发一次。关联的 IntentService 是:

public class ActivityRecognitionIntentService extends IntentService {

ActivityRecognitionResult result;
Intent i;
DetectedActivity mpactivity;

public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntentService");
i = new Intent("ACTIVITY_RECOGNITION_DATA");
Log.d(MainActivity.TAG, "ActivityRecognitionIntentService created");
}

private String getTypes(int type) {
Log.d(MainActivity.TAG, "type = " + 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.RUNNING)
return "Running";
else if(type == DetectedActivity.ON_FOOT)
return "On Foot";
else if(type == DetectedActivity.STILL)
return "Still";
else if(type == DetectedActivity.TILTING)
return "Tilting";
else if(type == DetectedActivity.WALKING)
return "Walking";
else
return "";
}

@Override
protected void onHandleIntent(Intent intent) {
Log.d(MainActivity.TAG, "onHandleIntent");
if (intent.getAction() == "ActivityRecognitionIntentService") {
if(ActivityRecognitionResult.hasResult(intent)){
result = ActivityRecognitionResult.extractResult(intent);
mpactivity = result.getMostProbableActivity();
i.putExtra("Activity", getTypes(mpactivity.getType()));
i.putExtra("Confidence", mpactivity.getConfidence());
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
}
}
}

我的 list 显示:

    <service android:name=".ActivityRecognitionIntentService" android:exported="false"></service>

<receiver android:name=".ActivityBroadcastReceiver" android:exported="false">
<intent-filter>
<action android:name="ACTIVITY_RECOGNITION_DATA"/>
</intent-filter>
</receiver>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

使用广播接收器:

public class ActivityBroadcastReceiver extends android.content.BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(MainActivity.TAG, "BroadcastReceiver");
Toast.makeText(context, "Service received", Toast.LENGTH_SHORT).show();
}

为什么它只是偶尔有效?调试时不允许手机休眠(N6)。此外,为什么它没有到达 BroadcastReceiver?

最佳答案

detectionIntervalMillis 将不准确。如果电话静止不动,则在 Activity 发生变化之前它可能不会报告任何内容。根据我的经验,当当前 Activity 没有变化时,间隔时间会更长。

引用文档,为了节省电池电量,当设备长时间“静止”时, Activity 报告可能会停止。一旦设备再次移动,它将恢复。 您的设备静止不动吗?尝试摇晃或四处走动以进行测试。

关于android - ActivityRecognitionIntentService onHandleIntent 仅偶尔触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28257601/

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