-1

Hav been trying to manually port the sample from http://developer.android.com/training/location/activity-recognition.html to the new GoogleApiClient.

Unfortunately I only got 1 update from the ActivityRecognition sometimes, most times none.

As an attempt to locate the problem I boiled the code down to as few lines as possible. No checks for Google Play Services and no error handling:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the main layout
    setContentView(R.layout.activity_main);


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    Log.d("ActivityRecognition", "Google Play Services connected");
                    Intent i = new Intent(MainActivity.this, ActivityRecognitionIntentService.class);
                    PendingIntent mActivityRecognitionPendingIntent = PendingIntent.getService(MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                    ActivityRecognition.ActivityRecognitionApi.
                            requestActivityUpdates(mGoogleApiClient, 1000, mActivityRecognitionPendingIntent);
                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .build();

    mGoogleApiClient.connect();

}

The ActivityRecognitionIntentService has all the code from the sample, but have added a log to the start of onHandleIntent in the hopes to see 'something' happen.

public class ActivityRecognitionIntentService extends IntentService {

    ...

    /**
     * Called when a new activity detection update is available.
     */
    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d("ActivityRecognitionIntentService", "onHandleIntent " + ActivityRecognitionResult.hasResult(intent));

       ...

    }
}

And finally the manifest:

    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>

    <application android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.Sample">

        <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:uiOptions="splitActionBarWhenNarrow" android:description="@string/app_description">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".ActivityRecognitionIntentService"></service>

    </application>

</manifest>

Any ideas to what I am doing wrong is appreciated.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33

2 Answers2

1

Seems if the problem had to do with the construction of PendingIntent

PendingIntent.getService(MainActivity.this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

If I change that code to:

PendingIntent.getService(MainActivity.this, (int)Math.random(), i, PendingIntent.FLAG_UPDATE_CURRENT);

The intents started firering.

From the documentation on getService() about the second parameter:

requestCode Private request code for the sender

So is no obvious reason for this parameter to have any influence on wheter or not the intent is fired.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
0

Adding Math.Random doesn't solve the problem for me.

In my case, I forgot to add the Service to the AndroidManifest. See: https://stackoverflow.com/a/29726050/1143263

Community
  • 1
  • 1
matteolel
  • 519
  • 4
  • 14