简体   繁体   中英

Awareness Snapshot APi not working for android

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

Do you have a valid API Key in your manifest.xml? And enabled the Awareness API in your project? - for more information see google-doc: https://developers.google.com/awareness/android-api/get-a-key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM