Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android ActivityRecognition not calling onHandleIntent

I have put together this piece of code to get the users activity, to see is he walking or driving or still, but its not working, onHandleIntent never called. it is connecting to GoogleApiClient.

Here is my code

Activity Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/msg" />

MainActivity

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;

public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Context mContext;
private GoogleApiClient mGApiClient;
private BroadcastReceiver receiver;
private TextView textView;

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

    //get the textview
    textView = (TextView) findViewById(R.id.msg);

    //Set the context
    mContext = this;

    //Check Google Play Service Available
    if(isPlayServiceAvailable()) {
        mGApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        //Connect to gPlay
        mGApiClient.connect();
    }else{
        Toast.makeText(mContext, "Google Play Service not Available", Toast.LENGTH_LONG).show();
    }

    //Broadcast receiver
    receiver  = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String v =  "Activity :" +
                    intent.getStringExtra("act") + " " +
                    "Confidence : " + intent.getExtras().getInt("confidence") + "n";

            v += textView.getText();
            textView.setText(v);
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction("SAVVY");
    registerReceiver(receiver, filter);
}

private boolean isPlayServiceAvailable() {
     return GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext) == ConnectionResult.SUCCESS;
}

@Override
public void onConnected(Bundle bundle) {
    Intent i = new Intent(this, ActivityRecognitionIntentService.class);
    PendingIntent mActivityRecongPendingIntent = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

    Log.d("Saquib", "connected to ActRecog " + "PI " +mActivityRecongPendingIntent.toString() );
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGApiClient, 0, mActivityRecongPendingIntent);
}

@Override
public void onConnectionSuspended(int i) {
    Log.d("Saquib", "suspended to ActRecog");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d("Saquib", "Not connected to ActRecog");
}

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mGApiClient.disconnect();

        unregisterReceiver(receiver);
     }
   }

ActivityRecognitionIntentService

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;

/**
 * Created by tutsberry on 17/03/15.
 */
public class ActivityRecognitionIntentService extends IntentService {

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

@Override
protected void onHandleIntent(Intent intent) {

    if(ActivityRecognitionResult.hasResult(intent)) {
        ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
        DetectedActivity detectedActivity = result.getMostProbableActivity();

        int confidence = detectedActivity.getConfidence();
        String mostProbableName = getActivityName(detectedActivity.getType());

        Intent i = new Intent("SAVVY");
        i.putExtra("act", mostProbableName);
        i.putExtra("confidence", confidence);

        Log.d("Saquib", "mostProbableName " + mostProbableName);
        Log.d("Saquib", "Confidence : " + confidence);

        //Send Broadcast
        this.sendBroadcast(i);

    }else {
        Log.d("Saquib", "Intent had no data returned");
    }
}

private String getActivityName(int type) {
    switch (type)
    {
        case DetectedActivity.IN_VEHICLE:
            return "in_vehicle";
        case DetectedActivity.ON_BICYCLE:
            return "on_bicycle";
        case DetectedActivity.WALKING:
            return "walking";
        case DetectedActivity.STILL:
            return "still";
        case DetectedActivity.TILTING:
            return "tilting";
        case DetectedActivity.UNKNOWN:
            return "unknown";
        case DetectedActivity.RUNNING:
            return "running";

    }
    return "n/a";
}
}

and manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutsberry.moveyours" >

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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:name=".ActivityRecognitionIntentService">

</service>
    </application>

</manifest>

Guys please help, google doc is no use for ActivityRecognition

like image 845
Saqueib Avatar asked Mar 18 '15 04:03

Saqueib


2 Answers

Maybe you are missing the

<uses-permission android:name="android.permission.INTERNET" />
like image 68
Teo Inke Avatar answered Nov 14 '22 22:11

Teo Inke


Make sure that you are connected to the internet beforehand. I think otherwise it will not work. The first time you connect to google play services you must be connected to the internet.

like image 30
rabz100 Avatar answered Nov 14 '22 22:11

rabz100