Google Play服务 - 活动识别在几次结果后冻结

时间:2015-01-17 01:58:31

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

我试图尽可能地简化我的代码,基本上问题是ActivityRecognitionIntentService似乎被调用了几次,然后停止了。它似乎与PendingIntent中的requestCode有关,但我不确定,有人可以告诉我出了什么问题吗?感谢。

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

公共类ActivityRecognitionIntentService扩展了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>

0 个答案:

没有答案