gpt4 book ai didi

android ActivityRecognition 不调用 onHandleIntent

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:31:25 27 4
gpt4 key购买 nike

我已经将这段代码放在一起来获取用户 Activity ,看看他是走路还是开车还是静止不动,但它不起作用,onHandleIntent 从未调用过。它正在连接到 GoogleApiClient

这是我的代码

Activity 布局

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

主 Activity

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>

伙计们请帮忙,谷歌文档对 ActivityRecognition 没有用

最佳答案

也许你错过了

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

关于android ActivityRecognition 不调用 onHandleIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29113919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com