2016-07-22 68 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

相关问题