1

アプリのアクティビティ認識の更新を取得しようとしていますが、部分的にしか機能していないようです。最初は機能していないと思っていましたが、時々デバッグ ログにアクティビティ タイプが表示されます。これは、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 に到達しないのはなぜですか?

4

2 に答える 2

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.は、デバイスは静止していますか? テストするために、少し振ったり、歩き回ったりしてみてください。

于 2015-01-31T23:38:13.290 に答える