0

I have tried to simplify my code as much as possible, basically hte issue is that the ActivityRecognitionIntentService appears to be called a couple of times, then stalls out. It appears to be related to the requestCode in the PendingIntent, but I am not sure, can someone please advise me as to what is going wrong? Thanks.

Dashboard.java

public class Dashboard extends Activity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

    private BroadcastReceiver receiver;
    private TextView tvActivity;
    private GoogleApiClient mGoogleApiClient;
    //String Locationp = "null";
    //private LocationRequest mLocationRequest;


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

        setContentView(R.layout.activity_dashboard);
        tvActivity = (TextView) findViewById(R.id.tvActivity);

        int resp =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if(resp == ConnectionResult.SUCCESS){
        // Create a GoogleApiClient instance
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                //.addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mGoogleApiClient.connect();
        }
        else{
            Toast.makeText(this, "Please install Google Play Service.", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onConnected(Bundle arg0) {
        Intent i = new Intent(this, ActivityRecognitionIntentService.class);
        PendingIntent mActivityRecognitionPendingIntent = PendingIntent.getService(this, 2000, i, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.e("MAIN", "Connected to ActRec");

        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 1000, mActivityRecognitionPendingIntent);

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        Log.e("MAIN", "Connection suspended to ActRec");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e("MAIN", "Not Connected to ActRec");
    }

ActivityRecognitionIntentService.java

public class ActivityRecognitionIntentService extends IntentService {

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

protected void onHandleIntent(Intent intent) {
    if (ActivityRecognitionResult.hasResult(intent)) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        DetectedActivity mostProbAct = result.getMostProbableActivity();
        int confidence = mostProbAct.getConfidence();
        String mostProbActName = getActivityName(mostProbAct.getType());

        Intent i = new Intent("com.xxx.abc.ACTIVITY_RECOGNITION_DATA");

        i.putExtra("act", mostProbActName);
        i.putExtra("confidence", confidence);
        Log.e("ARS", mostProbActName + "," + confidence);

        //sendBroadcast(i);
    } else
        Log.e("ARS", "Intent had no ActivityRecognitionData");
}

private String getActivityName(int activityType) {
    switch (activityType) {
        case DetectedActivity.IN_VEHICLE:
            return "in_vehicle";
        case DetectedActivity.ON_BICYCLE:
            return "on_bicycle";
        case DetectedActivity.ON_FOOT:
            return "on_foot";
        case DetectedActivity.STILL:
            return "still";
        case DetectedActivity.UNKNOWN:
            return "unknown";
        case DetectedActivity.TILTING:
            return "tilting";
    }
    return "unknown";
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tigerblood.com.node" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <service android:enabled="true" android:name="com.xxx.abc.ActivityRecognitionIntentService"></service>

        <activity
            android:name="com.tigerblood.node.Dashboard"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
eWizardII
  • 1,916
  • 4
  • 32
  • 55
  • You're not really using GoogleApiClient correctly. You should be calling .connect in onStart() and .disconnect in onStop(). – PaulR Jan 17 '15 at 04:15
  • Thanks for the tip, I implemented that and I am still not receiving any updates for activity. – eWizardII Jan 17 '15 at 04:27
  • Did you see in the documentation the note about activity updates being paused sometimes when the activity is STILL or the device was put into low power mode? It's also possible the context you pass in may be invalidated and you need to use createPackageContext instead of using `this` for creating the Intent. Finally, PlayServicesUtil.isPlayServicesAvailable is no longer needed the way you're coding this. The onConnectionFailed listener is called when there's an issue. See https://github.com/googlesamples/android-play-location/blob/master/ActivityRecognition/ – PaulR Jan 17 '15 at 05:10
  • Thanks for the help! STILL might be the issue, I'll have to try going outside and see if anything changes. I'm not in power saving mode, hopefully it's just the first issue, if not I'll take a look at the example you linked. – eWizardII Jan 17 '15 at 05:40
  • I lean more toward the theory that your context is becoming invalid and you need to create the package one and use that when creating your Intent. – PaulR Jan 17 '15 at 06:00
  • Okay working on adding that now then - you mean replace both .getService(this...) and Intent(this...) with createPackageContext("packagename", CONTEXT_INCLUDE_CODE ? – eWizardII Jan 17 '15 at 06:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69041/discussion-between-ewizardii-and-paulr). – eWizardII Jan 17 '15 at 21:46

0 Answers0