androidandroid-intentandroid-pendingintentandroid-task

How to get Task to call pendingIntent


I've been trying to pass a pendingIntent to the Activity Recognition Client as part of a Task, however the pendingIntent doesn't seem to be being called.

I'm using Google's Activity Recognition Client (https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient). I have already looked at the example code (found here: https://github.com/googlesamples/android-play-location/blob/master/ActivityRecognition/app/src/main/java/com/google/android/gms/location/sample/activityrecognition/MainActivity.java) but can't seem to see my issue.

My code looks like this (I've extracted parts of the class to make it easier to read):


public class SignIn extends AppCompatActivity {

    private ActivityRecognitionClient mActivityRecognitionClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in);

        mActivityRecognitionClient = new ActivityRecognitionClient(this);

        Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(1000L, getActivityDetectionPendingIntent());

        task.addOnSuccessListener(result -> Log.i("test", "test"));
        task.addOnFailureListener(e -> Log.e("test", e.toString()));
    }

    private PendingIntent getActivityDetectionPendingIntent() {
        Intent intent = new Intent(this, DetectedActivitiesIntentService.class);

        return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

package com.example.kangaroute;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;

import java.util.ArrayList;

class DetectedActivitiesIntentService extends IntentService {

    protected static final String TAG = "DetectedActivity";

    public DetectedActivitiesIntentService(){
        super(TAG);
    }

    @Override
    public void onCreate(){
        Log.i("test", "works");
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

        // Get the list of the probable activities associated with the current state of the
        // device. Each activity is associated with a confidence level, which is an int between
        // 0 and 100.
        ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

        for (DetectedActivity activity : detectedActivities){
            Log.i(TAG, activity.toString());
        }

    }
}

When I run the code, I expect to see "works" be printed to Logcat as I'm logging it from the onCreate() method in DetectedActivitiesIntentService class. However, I see nothing.

However, "test" is logged from the OnSuccessListener assosiated to the Task. When I log the result, nothing comes out...


Solution

  • Lol guess who forgot to put "public class" and instead just put "class". Solved it!