ActivityRecognitionIntentService onHandleIntent 仅偶尔触发

ActivityRecognitionIntentService onHandleIntent only occasionally fires

我正在尝试为我的应用获取 Activity 识别更新,但它似乎只能部分工作。首先,我认为它不起作用,但调试日志偶尔会显示一个 activity 类型,这意味着 ActivityRecognitionIntentService onHandleIntent() 被命中,但它从未进入 ActivityBroadCastReceiver

我在 Main 中有以下代码Activity

    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);
        }
    }
}

我的清单显示:

    <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();
}

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

detectionIntervalMillis 将不准确。如果 phone 仍然存在,它可能不会报告任何内容,直到 activity 发生变化。以我的经验,当电流 activity 没有变化时,间隔会更长。

引用文档,To conserve battery, activity reporting may stop when the device is 'STILL' for an extended period of time. It will resume once the device moves again.您的设备是否静止不动?尝试摇晃或四处走动以进行测试。

尝试在您的设备上激活 Google 位置记录

我已经坚持了很长时间。我会在启动时获得更新,然后几分钟没有任何更新,然后是一些更新,然后几分钟没有任何更新。我已经尝试了几乎所有我能想到的方法。更改请求的更新率并没有太大变化,摇动 phone 或行走也没有变化。

今天,我一直在阅读其他帖子,他们抱怨他们收到的更新太多,其中一位将其归咎于 Google 在设备上安装 运行。我激活了 Google 位置历史记录(在设置/位置/位置服务下),并且从那时起,我每隔几秒就会使用与以前完全相同的代码进行更新。

似乎出于某种原因 ActivityRecognitionClient 不会在您需要时生成 activity 识别更新,而是与更新的大致频率挂钩在别处生成?