Location Awareness service in Android

Steps.

1. First we need to get the API key, go to terminal and type,


keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

if JDK not installed, you will get a error like,

No Java runtime present, requesting install

A dialog appears click on more info and it will take directly to oracle JDK download site. Download JDK 9 and install and try again the above command. You will get the SAH1 and SAH256 fingerprints. 

2. Click on "Get key" button, then either select an existing project from google developer console or create a new project from this site

3. Now add the API_KEY to manifest file under <application> as

<meta-data android:name="com.google.android.awareness.API_KEY" android:value="@string/google_awareness_api_key">
</meta-data>

4.  Now we are ready to use Google awareness API in our app. Same like how we use fused location provider, create a googleApiClient and add the callbacks. From google awareness API we can get whether the user is WALKING, RUNNING, RIDING etc. We should use google activity with google confidence. If confidence is low, then we can completely rely on the google activity.

Sample code snippet is,






  GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
              .addApi(Awareness.API).build();
  googleApiClient.connect();

  Awareness.SnapshotApi.getWeather(googleApiClient).setResultCallback(new ResultCallback() {
    @Override
    public void onResult(@NonNull WeatherResult weatherResult) {
      if (!weatherResult.getStatus().isSuccess()) {
        Toast.makeText(context, "Could not get weather.", Toast.LENGTH_LONG).show();
        return;
      }
    }
 });


  Awareness.SnapshotApi.getDetectedActivity(googleApiClient).setResultCallback(new ResultCallback() {
    @Override
    public void onResult(@NonNull DetectedActivityResult detectedActivityResult) {
      if (!detectedActivityResult.getStatus().isSuccess()) {
        Toast.makeText(context, "Could not get the current activity.", Toast.LENGTH_LONG).show();
        return;
      }
    }
  });

Comments

Popular posts from this blog

Open Gallery and get the selected image in appplication

AlarmManager and Notification in Android

Dismiss or Cancel dialog by swipe gesture