ActivityRecognitionApi keeps crashing

Hi I am trying to create an android app for using ActivityRecognition API. But when I install it on my mobile it craches after some movement.

Can you help?

Manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".TrackerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:enabled="true" android:exported="false" android:name="TrackIntentService"></service>

    <receiver android:name="com.penelope.pepie.trackmovement.TrackBroadcastReceiver"></receiver>
    <meta-data android:name="com.google.android.gms.version"  android:value="@integer/google_play_services_version" />
</application>

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

The constructor of ActivityGoogleApiTracker:

public ActivityGoogleApiTracker(TrackerActivity context){
    this.context = context;
    connecting = false;
    recognitionClient = new GoogleApiClient.Builder(context)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    Intent intent = new Intent(context, TrackIntentService.class);
    recognitionIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

The onconnected call back:

public void onConnected(Bundle connectionHint){
    try {
        connecting = false;
        if (requestUpdates) {
            Log.d("api_client", "Connected");


            // send request
            ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(recognitionClient, 100, recognitionIntent);

        } else {
            Log.d("api_client", "Disconnected");

            // cancel updates
            ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(recognitionClient, recognitionIntent);
            //recognitionClient.removeActivityUpdates(recognitionIntent);
        }

    }
    catch(Exception ex)
    {
        Toast.makeText(context, "onConnected-exception " + ex.getMessage() + " called..", Toast.LENGTH_LONG).show();
    }
}

public class TrackBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ActivityRecognitionResult result = intent.getParcelableExtra(TrackIntentService.RECOGNITION_RESULT);

    if (context != null && context.getClass() == TrackerActivity.class)
        ((TrackerActivity)context).UpdateUI(result);
}

}


Source: android

Leave a Reply