2016-07-22 79 views
1

我希望能夠在我的Android應用程序中獲取當前位置和活動。我實現了它,但它似乎永遠不會返回任何東西。當我調試它時,從不調用OnResult方法。它只是沒有返回。例如,在下面的代碼中,它應該返回當前用戶活動爲 I/Awareness:DetectedActivity [type = STILL,confidence = 100],但沒有任何顯示。意識快照APi不適用於android

我在Android v6.0上測試了這個,好的位置在我的清單中,並在我的手機上打開。

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

這裏是我用於獲取活動代碼:

public class MainActivity extends AppCompatActivity { 

    private static final String TAG = "Awareness"; 
    private GoogleApiClient mGoogleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this) 
       .addApi(Awareness.API) 
       .build(); 
     mGoogleApiClient.connect(); 
    } 

    private void initSnapshots() { 
     Awareness.SnapshotApi.getDetectedActivity(mGoogleApiClient) 
       .setResultCallback(new ResultCallback<DetectedActivityResult>() { 
        @Override 
        public void onResult(@NonNull DetectedActivityResult detectedActivityResult) { 
         if (!detectedActivityResult.getStatus().isSuccess()) { 
          Log.e(TAG, "Could not get the current activity."); 
          return; 
         } 
         ActivityRecognitionResult ar = detectedActivityResult.getActivityRecognitionResult(); 
         DetectedActivity probableActivity = ar.getMostProbableActivity(); 
         Log.i(TAG, probableActivity.toString()); 
        } 
       }); 
    } 
} 

我也訪問以下網址: https://inthecheesefactory.com/blog/google-awareness-api-in-action/en

回答

0

你有你的manifest.xml有效的API密鑰?在您的項目中啓用了Awareness API? - 欲瞭解更多信息,請參閱谷歌文檔:https://developers.google.com/awareness/android-api/get-a-key

+0

是的我有API密鑰添加到我的清單文件。此代碼適用於moto x,但是當我嘗試使用Nexus 6p運行它時,它不起作用 – user1380637

+0

您的Nexus手機是否支持庫存ROM,或者例如Cyanogenmod?我有同樣的問題,但是當它不工作時,它與cmrom。 – Totoo