有人知道Google ActivityRecognitionAPI的出色文档或解释吗?除了这个:https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionApi
这个文档太弱了,我想了解更多关于这个 API 的信息,以及它是如何工作的!如果您对这个 API 的工作原理有一些了解,我将非常高兴!=)
谢谢!
至于它的工作原理,它是一个闭源的谷歌API,因此你无法得到确切的答案,但大多数人说这主要依赖于加速度计传感器,尽管有些人也说它使用陀螺仪和其他一些传感器。
至于如何使用它,因为下面是一个示例代码:
1. 添加 Play 服务依赖项
在你的build.gradle(模块:app)中,添加:
compile 'com.google.android.gms:play-services:10.0.1'
2. 添加权限
在清单中,添加以下权限:
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
3. 在创建主活动时连接 API
(确保您已将公共 GoogleApiClient mApiClient 声明为成员变量)
mApiClient = new GoogleApiClient.Builder(this)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mApiClient.connect();
2. 连接:
(将其粘贴到您的主活动中)
@Override
public void onConnected(@Nullable Bundle bundle) {
Intent intent = new Intent( this, ActivityRecognizedService.class );
PendingIntent pendingIntent = PendingIntent.getService( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT );
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mApiClient, 3000, pendingIntent );
}
3. 活动识别服务类创建一个新类 ActivityRecognizedService 并粘贴以下代码:
public ActivityRecognizedService() {
super("ActivityRecognizedService");
}
public ActivityRecognizedService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
handleDetectedActivities(result.getProbableActivities());
}
}
private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
for (DetectedActivity activity : probableActivities) {
switch (activity.getType()) {
case DetectedActivity.IN_VEHICLE: {
Log.e("ActivityRecogition", "In Vehicle: " + activity.getConfidence());
break;
}
case DetectedActivity.ON_BICYCLE: {
Log.e("ActivityRecogition", "On Bicycle: " + activity.getConfidence());
break;
}
case DetectedActivity.ON_FOOT: {
Log.e("ActivityRecogition", "On Foot: " + activity.getConfidence());
break;
}
case DetectedActivity.RUNNING: {
Log.e("ActivityRecogition", "Running: " + activity.getConfidence());
break;
}
case DetectedActivity.STILL: {
Log.e("ActivityRecogition", "Still: " + activity.getConfidence());
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentText("Are you walking?");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(getString(R.string.app_name));
NotificationManagerCompat.from(this).notify(0, builder.build());
break;
}
case DetectedActivity.TILTING: {
Log.e("ActivityRecogition", "Tilting: " + activity.getConfidence());
break;
}
case DetectedActivity.WALKING: {
Log.e("ActivityRecogition", "Walking: " + activity.getConfidence());
break;
}
case DetectedActivity.UNKNOWN: {
Log.e("ActivityRecogition", "Unknown: " + activity.getConfidence());
break;
}
}
通过onConnected,我们调用此服务,在上面的代码中,我们的回调延迟为3000毫秒或3秒,我们可以改变。当检测到步行且置信度大于 75 时,此代码会生成 Toast 通知。
调用服务时,调用onHandleIntent()
,而服务又调用handleDetectedActivities()
。
4) 断开接口的连接删除回调并断开 API 连接。
if (mApiClient.isConnected()) {
Intent intent2 = new Intent(context, ActivityRecognizedService.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mApiClient, pendingIntent);
mApiClient.disconnect();
}
来源:如何使用活动识别识别用户活动