0

I got some example for activity recognition from here

The thing is I wish to update my GUI with the results, so I have tried with callback but I get null exception. Can you please help me or give other option?

Main Class:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, MyCallback  {

public GoogleApiClient mApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Intent intent = new Intent(this, ActivityRecognizedService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, 3000, pendingIntent);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}


@Override
public void callbackCall(int i)
{
    //Here will be gui Update
    Log.d("DTAG", "is: "+i);
}
}

Callback interface:

interface MyCallback {
void callbackCall(int i);
}

ActivityRecognizedService:

public class ActivityRecognizedService extends IntentService {

    MyCallback callback;

    public ActivityRecognizedService() {
        super("ActivityRecognizedService");
    }

    public ActivityRecognizedService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if(ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities( result.getProbableActivities() );
        }
    }

    void onEvent() {

    }

    private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
        for( DetectedActivity activity : probableActivities ) {
            switch( activity.getType() ) {
                case DetectedActivity.IN_VEHICLE: {
                    Log.e( "ActivityRecogition", "In Vehicle: " + activity.getConfidence() );
                    callback.callbackCall(activity.getConfidence());
                    //return value for GUI update
                    break;
                }

                case DetectedActivity.UNKNOWN: {
                    Log.e( "ActivityRecogition", "Unknown: " + activity.getConfidence() );
                    break;
                }
            }
        }
    }
}

Exception:

java.lang.NullPointerException: Attempt to invoke interface method 'void dan.com.movetest.MyCallback.callbackCall(int)' on a null object reference
                                                                           at dan.com.movetest.ActivityRecognizedService.handleDetectedActivities(ActivityRecognizedService.java:49)
                                                                           at dan.com.movetest.ActivityRecognizedService.onHandleIntent(ActivityRecognizedService.java:36)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dim
  • 4,527
  • 15
  • 80
  • 139

3 Answers3

1

You've not initialised callback anywhere in ActivityRecognizedService. Callbacks may not be the best approach in this case, to quote an answer from another question:

You might be looking for the ResultReceiver class (http://developer.android.com/reference/android/os/ResultReceiver.html). You extend it, pass it along with the intent you invoke the Service with (it's parcelable), then when your task completes in the Service, you call the ResultReceiver's onReceiveResult() method with the results.

Community
  • 1
  • 1
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
0

Use ((MyCallback)activity) .callbackCall(activity.getConfidence());

Inside

case DetectedActivity.IN_VEHICLE:{

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • ClassCastException: com.google.android.gms.location.DetectedActivity cannot be cast to danm.com.movetest.MyCallback – Dim May 18 '17 at 15:04
  • What is detectedactivity? In you code you have got only mainactivity that has MyCallback inetrface – Vyacheslav May 18 '17 at 15:52
0

You need to initiate the callback in order for it to work.

Try passing a MyCallback instance in the ActivityRecognizedService constructor and when you initiate the ActivityRecognizedService on the MainActivity do something like the following:

new ActivityRecognizedService(new MyCallback(...))

And implement the methods of the callback. I hope this helps

Valdio
  • 101
  • 1
  • 6