2015-09-03 44 views
13

我正在使用Google Fit來演示應用程序以獲取當前活動示例。我可以正確地獲得速度以及距離。但是,儘管我處於同一狀態,但它並沒有很頻繁地返回「in_vehicle」或「biking」狀態。查找相同的截圖。我的速度達到59.40KM/H(36.91 M/h),並且當時還沒有返回「in_vehicle」活動狀態。從Google Fit API獲取當前活動Android

請提供相同的解決方案/反饋。

代碼:

@Override 
public void onDataPoint(DataPoint dataPoint) { 
    for (Field field : dataPoint.getDataType().getFields()) { 
     Value val = dataPoint.getValue(field); 
      if(field.getName().trim().toLowerCase().equals("activity")) 
        { 
         if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("biking")) 
         { 
          strState = "Cycling"; 
         } 
         else if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("in_vehicle")) 
         { 
          strState = "Automotive"; 
         } 
         else if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("walking")) 
         { 
          strState = "Walking"; 
         } 
         else 
         { 
          strState = "Not Moving"; 
         } 
        } 
      } 
} 

感謝。

IMAGE

+0

我們會更好地理解一段代碼。 – dhams

+0

posted/edited .. – steve

+0

您應該編輯問題的標題和正文以表明「活動」不是指「android.app.Activity」,而是指「健身活動」字符串。 –

回答

1

正如你所說,你正在獲得正確的速度。你可以把自定義代碼寫在下面。

if (strState.equals("Automotive") && speed == 0.00) 
{ 
   strState = "Not Moving"; 
} 
else if (strState.equals("Not Moving") && speed > 5) 
{ 
   strState = "Automotive"; 
} 
else 
{ 
   strState = "strState"; 
} 

這可能不是正確的,但它會給你附近的狀態結果。

-2

我不熟悉與谷歌適合的API,這樣我就可以給你的唯一的建議是,仔細檢查你的代碼。是
Integer.parseInt(val.toString())
返回正確的int和可
FitnessActivities.getName()
等於 「騎自行車」, 「行走」, 「in_vehicle」 等

,我可以從這裏看到:https://developers.google.com/fit/rest/v1/reference/activity-types

騎自行車,在車輛和步行是0,1和7. 檢查什麼FitnessActivities.getName(0)返回例如,也檢查val是否返回不同的值,或它返回相同的時間。

如果您的代碼存在任何問題,您應該知道代碼在任何行中正在做什麼,返回的方法和函數是什麼......還通知人們,以便他們發現解決方案更簡單。

4

你可以找到我在這裏創建的示例項目。

https://github.com/cyfung/ActivityRecognitionSample

重要提示:你可能無法獲得數據頻繁按照您的要求!

在API 21開始,活動可被較不頻繁地比 接收的detectionIntervalMillis參數如果設備處於節能模式 和屏幕是關閉的。

關鍵組件:

onCreate

mGoogleApiClient = 
     new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API) 
      .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build(); 

連接創建GoogleApiClient和onStartonStop斷開API客戶,如谷歌API文檔中建議。

@Override 
    protected void onStart() { 
    super.onStart(); 
    mGoogleApiClient.connect(); 
    mStatusView.setText("connecting"); 
    } 

    @Override 
    protected void onStop() { 
    super.onStop(); 
    mGoogleApiClient.disconnect(); 
    mStatusView.setText("disconnected"); 
    } 

開始活動識別(不應在Google Api連接之前調用)。使用PendingIntent.getService來創建掛起的意圖回調。

final PendingResult<Status> 
    statusPendingResult = 
    ActivityRecognition.ActivityRecognitionApi 
     .requestActivityUpdates(mGoogleApiClient, DETECT_INTERVAL, PendingIntent 
      .getService(this, 0, new Intent(this, ActivityDetectionService.class), 
          PendingIntent.FLAG_UPDATE_CURRENT)); 
statusPendingResult.setResultCallback(this); 

IntentService是標準的方法,建議回調

public class ActivityDetectionService extends IntentService { 

    protected static final String TAG = "activityDetectionService"; 

    public ActivityDetectionService() { 
    super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
    final ActivityRecognitionResult 
     activityRecognitionResult = 
     ActivityRecognitionResult.extractResult(intent); 
    if (activityRecognitionResult == null) { 
     return; 
    } 

    //process the result here, pass the data needed to the broadcast 
    // e.g. you may want to use activityRecognitionResult.getMostProbableActivity(); instead 
    final List<DetectedActivity> 
     probableActivities = 
     activityRecognitionResult.getProbableActivities(); 

    sendBroadcast(MainActivity.newBroadcastIntent(probableActivities)); 
    } 
} 

清單中註冊該服務。

<service 
      android:name=".ActivityDetectionService" 
      android:exported="false"> 
    </service> 

要使用API​​,還需要在清單中添加以下內容。

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>

<meta-data 
       android:name="com.google.android.gms.version" 
       android:value="@integer/google_play_services_version" /> 

找回數據,我用的onCreate

mBroadcastReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    ... 
    } 
} 

註冊創建一個BroadcastReceiver的活動,分別onResume和註銷。

@Override 
    protected void onResume() { 
    super.onResume(); 
    registerReceiver(mBroadcastReceiver, newBroadcastIntentFilter()); 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 
    unregisterReceiver(mBroadcastReceiver); 
    } 
+0

這是非常不準確的。當我走路時,它說in_vehicle /騎自行車。 – steve

+0

爲此,我真的沒有辦法,你可以檢查代碼,我只是使用api –

+0

這就是我提出的問題,我正面臨Google Fit api。 – steve

相關問題