簡體   English   中英

Android活動識別問題

[英]Android activity recognition issue

我正在開發android原生應用程序。 我的要求是獲取設備的當前活動,如running,in_vehicle,still等。 我使用ActivityRecognitionAPI並將pendingintent設置為通過IntentService接收活動更改。 我給每個更新的時間間隔為5秒。 它未能在一定時間內提供活動變化,並再次開始提供活動。 之后我更喜歡Awareness SnapshotAPI來獲取活動狀態。 它也同樣的結果,未能定期提供活動。 這兩種API有時提供,有時不提供。 我使用GooglePlayServices 10.2.0版本進行開發。 任何人都告訴這些事情的原因和解決方案,以獲得定期的活動更新..

提前致謝。

我正在嘗試顯示我的應用用戶的活動,比如他正在走路,跑步等等。 這是我的要求。

方式1:

//使用ActivityRecognition.API的Google API客戶端

Intent intent = new Intent(mContext, RequestActivityService.class);
PendingIntent callbackIntent = PendingIntent.getService(mContext, 111,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,5000, callbackIntent);

public class RequestActivityService extends IntentService {
public RequestActivityService() {
    super("RequestActivityService");
}

@Override
protected void onHandleIntent(Intent intent) {

        if (ActivityRecognitionResult.hasResult(intent)) {

            ActivityRecognitionResult result = ActivityRecognitionResult
                    .extractResult(intent);

            DetectedActivity mostProbableActivity = result
                    .getMostProbableActivity();

            int confidence = mostProbableActivity.getConfidence();

            int activityType = mostProbableActivity.getType();
            String activityName = getNameFromType(activityType);

        }
}

**我嘗試這種方式來獲取活動更新。 它開始提供活動信息,但有時不調用Intent服務,並再次啟動觸發意圖。 所以我無法顯示正確的活動信息。

方式2:

//使用Awareness.API的Google API客戶端

Awareness.SnapshotApi.getDetectedActivity(googleApiClient).

                setResultCallback(new ResultCallback<DetectedActivityResult>() {
                    @Override
                    public void onResult(@NonNull DetectedActivityResult detectedActivityResult) {
                        if (!detectedActivityResult.getStatus().isSuccess()) {
                            Log.i("Could not get the activity","");
                            return;
                        }
                        ActivityRecognitionResult ar = detectedActivityResult.getActivityRecognitionResult();
                        DetectedActivity probableActivity = ar.getMostProbableActivity();
                        String activityType = RequestActivityService.getNameFromType(probableActivity.getType());
                        int activityConfidence = probableActivity.getConfidence();
                    }
                });

**我嘗試了另一種方式,使用這個Api我們可以遞歸調用此方法並經常獲取活動信息。 但它也有時提供並且有時檢測到的ActivityResult.getStatus()沒有成功。 如果我試圖獲取狀態代碼和狀態消息,狀態代碼將返回15並且狀態消息將為null。

這兩種方式都無法定期更新活動,而且我使用的是GooglePlayService 10.2.0版本。 我已經從Github測試了ActivityScognition的GoogleSamples。 這也是同樣的結果。 我不知道如何達到我的要求。 希望你現在能理解我的要求和我所面對的事情。

得到回復后再次調用此方法:

我建議將detectionIntervalMillis設置為0.這將導致盡快獲取數據。(注意電池消耗)

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,o, callbackIntent);

Google的ActivityRecognitionAPI不會定期提供更新。 根據我的注意,如果活動的置信度或活動完全改變,那么它將發送更新。 即便如此,確保數據讀取不僅僅是某種噪聲還需要時間。

我不確定為什么你需要定期更新,但我建議改變邏輯以接受最后給定的活動作為當前活動,並且只對變化采取行動(即, STILL - > IN_VEHICLE )。

如果長時間沒有變化,它將不會發送更新,直到發生變化...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM