android ActivityRecognition没有调用onHandleIntent

时间:2015-03-18 04:01:23

标签: android google-play-services activity-recognition

我已将这段代码放在一起以获取用户活动,看他是走路还是开车还是静止,但它无效,onHandleIntent从未打过电话。它正在连接到GoogleApiClient

这是我的代码

活动布局

<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";
}
}

并显示

<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>

伙计们请帮忙,google doc对ActivityRecognition无用

2 个答案:

答案 0 :(得分:4)

也许你错过了

<uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:2)

确保您事先连接到互联网。我认为否则它将无效。首次连接到Google Play服务时,您必须连接到互联网。