Android 谷歌活动识别API
我将使用google play services活动识别api编写一个应用程序。在中的培训是直接的,但在过去的几个小时里,我写了一个简单的应用程序,但我无法从中得到任何结果。Android 谷歌活动识别API,android,google-play-services,activity-recognition,Android,Google Play Services,Activity Recognition,我将使用google play services活动识别api编写一个应用程序。在中的培训是直接的,但在过去的几个小时里,我写了一个简单的应用程序,但我无法从中得到任何结果。 更新:事实上,我将在5秒内以toast消息的形式显示用户的当前活动(如ActivityRecognitionServiceIntent服务中的OnIntentHandler方法所示)。我认为调用Intent有问题,因为正如您在我的代码中看到的,toast表示ActivityRecognitionClient是通过OnCon
更新:事实上,我将在5秒内以toast消息的形式显示用户的当前活动(如ActivityRecognitionServiceIntent服务中的OnIntentHandler方法所示)。我认为调用Intent有问题,因为正如您在我的代码中看到的,toast表示ActivityRecognitionClient是通过OnConnected方法连接的 我错过什么了吗 提前谢谢 清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nikapps.activityrecognition"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission
android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name="com.nikapps.activityrecognition.ActivityRecognitionService"
android:label="@string/app_name"
android:exported="false">
</service>
<activity
android:name="com.nikapps.activityrecognition.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>
</application>
</manifest>
ActivityRecognitionService.java
package com.nikapps.activityrecognition;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
public class ActivityRecognitionService extends IntentService{
public ActivityRecognitionService() {
super("ActivityRecognitionService");
Toast.makeText(this, "here", Toast.LENGTH_SHORT).show();
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "here2", Toast.LENGTH_SHORT).show();
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity activity = result.getMostProbableActivity();
int type = activity.getType();
Toast.makeText(this, getNameFromType(type), Toast.LENGTH_SHORT).show();
}
private String getNameFromType(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";
}
}
您只能从设置了处理程序/循环器的线程调用Toast。要实现你的设想,你有两个选择
您可以广播从服务向主类发送消息的意图
例如,在ActivityRecognitionService.java中使用以下内容:
Intent mIntent = new Intent("myCustomIntentMessage")
.putExtra("ActivityType", getNameFromType(type));
getLocalBroadcast().sendBroadcast(mIntent);
然后,您只需在MainActivity.java上注册“myCustomIntentMessage”的广播接收器,并将Toast消息代码放入广播接收器OnReceive事件中。请准确描述未发生的情况,事实上,我将在5秒内以toast消息的形式显示用户的当前活动(如ActivityRecognitionServiceIntent服务中的OnIntentHandler方法所示)
Intent mIntent = new Intent("myCustomIntentMessage")
.putExtra("ActivityType", getNameFromType(type));
getLocalBroadcast().sendBroadcast(mIntent);