意识快照APi不适用于Android

时间:2016-07-22 18:40:50

标签: android google-api google-awareness

我希望能够在我的Android应用中获取当前位置和活动。我实现了它,但它似乎永远不会返回任何东西。当我调试它时,从不调用OnResult方法。它什么都不返回。例如,在下面的代码中,它应该返回当前用户活动为 I / Awareness:DetectedActivity [type = STILL,confidence = 100]但没有显示任何内容。

我正在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

1 个答案:

答案 0 :(得分:0)

您的manifest.xml中是否有有效的API密钥?并在项目中启用了Awareness API? - 有关详细信息,请参阅google-doc:https://developers.google.com/awareness/android-api/get-a-key