1

I want to be able to get the current location and activity in my Android app. I implemented it, but it seems it never return anything. When I debug it never calls OnResult method. It just returns nothing. For example in below code it should be returning the current user activity as I/Awareness: DetectedActivity [type=STILL, confidence=100] but there is nothing getting displayed.

I'm testing this on Android v6.0 and yes fine location is in my manifest and turned on on my phone.

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

Here's my code for getting the activity:

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());
                    }
                });
    }
}

I am also following this link: https://inthecheesefactory.com/blog/google-awareness-api-in-action/en

4

1 回答 1

0

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

于 2016-07-26T10:38:03.623 回答