我正在尝试为我的应用获取活动识别更新,但似乎仅能部分工作。首先,我认为它不起作用,但是调试日志偶尔会显示一个活动类型,这意味着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?

最佳答案

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

07-27 21:58