ActivityRecognitionAPI文档/说明

时间:2016-02-20 14:13:05

标签: android google-api sensor activity-recognition

任何人都知道Google ActivityRecognitionAPI 的精彩文档或说明?除此之外:https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionApi

这个文档太弱了,我想更多地了解这个API,以及它是如何工作的! 如果您对此API的工作方式有一些了解,我将非常高兴! =)

谢谢!

1 个答案:

答案 0 :(得分:2)

至于它是如何工作的,它是一个封闭的源码谷歌API,因此你无法得到确切的答案,但大多数人说这主要依赖于加速计传感器,虽然有些人还说它使用陀螺仪和其他一些传感器同样。

至于如何使用它,如下所示:

<强> 1。添加Play服务依赖性

build.gradle(module:app)中,添加:

compile 'com.google.android.gms:play-services:10.0.1'

<强> 2。添加权限

在您的清单中,添加以下权限:

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

第3。在MainCctivity的onCreate中连接API

(确保已将公共GoogleApiClient mApiClient声明为成员变量)

mApiClient = new GoogleApiClient.Builder(this)
        .addApi(ActivityRecognition.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();

mApiClient.connect();

<强> 2。 onConnected:

(将其粘贴在您的MainActivity中)

@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类 创建一个新类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 删除回调并断开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();
    }

来源:How to Recognize User Activity with Activity Recognition