ActivityRecognitionIntentService onHandleIntent仅偶尔触发

时间:2015-01-31 23:11:43

标签: android google-play-services activity-recognition

我正在尝试为我的应用获取活动识别更新,但它似乎只是部分工作。首先我认为它不起作用但是每隔一段时间调试日志就会显示一个活动类型,这意味着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);
        }
    }
}

我的清单显示:

    <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" />

使用BroadcastReceiver:

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?

2 个答案:

答案 0 :(得分:2)

detectionIntervalMillis不准确。如果手机仍然存在,则在活动发生变化之前可能无法报告任何内容。根据我的经验,当前活动没有变化时间隔更长。

引用文档,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.您的设备是否静止不动?尝试摇晃或走动一下进行测试。

答案 1 :(得分:0)

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

我已经坚持了很长时间。我会在启动时获得更新,然后在几分钟内没有任何更新,然后是一些更新,然后几分钟就没有了。我已经尝试了很多我能想到的东西。更改请求的更新速率没有太大变化,也没有摇动手机或走路。

今天我一直在阅读那些抱怨他们收到太多更新的人的其他帖子,其中一个人将其归咎于在设备上运行的Google Fit。我已激活 Google位置记录 (在设置/位置/位置服务下),从那时起,我每隔几秒就会使用完全相同的代码获取更新和以前一样。

似乎由于某种原因, ActivityRecognitionClient 不会在您需要时生成活动识别更新,而是在其他地方生成的更新上与该近似频率挂钩?