简体   繁体   English

Android活动识别问题

[英]Android activity recognition issue

I am developing android native applications. 我正在开发android原生应用程序。 My requirement is to get the current activity of the device like running,in_vehicle,still and so on. 我的要求是获取设备的当前活动,如running,in_vehicle,still等。 I used ActivityRecognitionAPI and set the pendingintent to receive the activity changes through IntentService. 我使用ActivityRecognitionAPI并将pendingintent设置为通过IntentService接收活动更改。 I gave 5 seconds interval for each updated. 我给每个更新的时间间隔为5秒。 Its failed to provide the activity changes in certain period of time and again started providing activity. 它未能在一定时间内提供活动变化,并再次开始提供活动。 After that i preferred Awareness SnapshotAPI to get the activity state. 之后我更喜欢Awareness SnapshotAPI来获取活动状态。 Its also same result, failed to provide the activity regularly. 它也同样的结果,未能定期提供活动。 Both APIs sometimes providing and many times not. 这两种API有时提供,有时不提供。 I used GooglePlayServices 10.2.0 version for my developemnt. 我使用GooglePlayServices 10.2.0版本进行开发。 Anyone tell what is the reason for these things and solution to get regular activity updates.. 任何人都告诉这些事情的原因和解决方案,以获得定期的活动更新..

Thanks in advance. 提前致谢。

I am trying to show my app user's activity like he is walking, running and so on. 我正在尝试显示我的应用用户的活动,比如他正在走路,跑步等等。 This is my requirement. 这是我的要求。

Way 1: 方式1:

//Google API client using ActivityRecognition.API //使用ActivityRecognition.API的Google API客户端

Intent intent = new Intent(mContext, RequestActivityService.class);
PendingIntent callbackIntent = PendingIntent.getService(mContext, 111,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,5000, callbackIntent);

public class RequestActivityService extends IntentService {
public RequestActivityService() {
    super("RequestActivityService");
}

@Override
protected void onHandleIntent(Intent intent) {

        if (ActivityRecognitionResult.hasResult(intent)) {

            ActivityRecognitionResult result = ActivityRecognitionResult
                    .extractResult(intent);

            DetectedActivity mostProbableActivity = result
                    .getMostProbableActivity();

            int confidence = mostProbableActivity.getConfidence();

            int activityType = mostProbableActivity.getType();
            String activityName = getNameFromType(activityType);

        }
}

** I tried this way to get activity updates. **我尝试这种方式来获取活动更新。 It started providing the activity info, but Intent service not called sometimes and again started firing the intent. 它开始提供活动信息,但有时不调用Intent服务,并再次启动触发意图。 So I am not able to show the correct activity information. 所以我无法显示正确的活动信息。

Way 2: 方式2:

//Google API client using Awareness.API //使用Awareness.API的Google API客户端

Awareness.SnapshotApi.getDetectedActivity(googleApiClient).

                setResultCallback(new ResultCallback<DetectedActivityResult>() {
                    @Override
                    public void onResult(@NonNull DetectedActivityResult detectedActivityResult) {
                        if (!detectedActivityResult.getStatus().isSuccess()) {
                            Log.i("Could not get the activity","");
                            return;
                        }
                        ActivityRecognitionResult ar = detectedActivityResult.getActivityRecognitionResult();
                        DetectedActivity probableActivity = ar.getMostProbableActivity();
                        String activityType = RequestActivityService.getNameFromType(probableActivity.getType());
                        int activityConfidence = probableActivity.getConfidence();
                    }
                });

** I tried this another way, using this Api we can call this method recursively and get the activity information frequently. **我尝试了另一种方式,使用这个Api我们可以递归调用此方法并经常获取活动信息。 But Its also sometime providing and sometimes detectedActivityResult.getStatus() not success. 但它也有时提供并且有时检测到的ActivityResult.getStatus()没有成功。 If I am trying to get the status code and status message, status code returns 15 and status message will be null. 如果我试图获取状态代码和状态消息,状态代码将返回15并且状态消息将为null。

Both these ways are failed to give regular updates of activity and more over I used GooglePlayService 10.2.0 version. 这两种方式都无法定期更新活动,而且我使用的是GooglePlayService 10.2.0版本。 I have tested the GoogleSamples of ActivityRecognition from Github. 我已经从Github测试了ActivityScognition的GoogleSamples。 That is also same result. 这也是同样的结果。 I don't know what do to achieve my requirement. 我不知道如何达到我的要求。 Hope now u can understand my requirement and things which I faced. 希望你现在能理解我的要求和我所面对的事情。

Call this method again after getting response: 得到回复后再次调用此方法:

I would suggest to set detectionIntervalMillis to 0. That will lead to get data ASAP.( be aware of battery consumption ) 我建议将detectionIntervalMillis设置为0.这将导致尽快获取数据。(注意电池消耗)

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,o, callbackIntent);

ActivityRecognitionAPI from Google doesn't give updates regularly. Google的ActivityRecognitionAPI不会定期提供更新。 From what I have noticed, if there is a change in the confidence level of the activity or the activity changes altogether, then it'll send an update. 根据我的注意,如果活动的置信度或活动完全改变,那么它将发送更新。 Even then, it'll take time to ensure the data read isn't just noise of some kind. 即便如此,确保数据读取不仅仅是某种噪声还需要时间。

I'm not sure why you need regular updates, but I would suggest changing the logic to accept the last given activity as the current activity, and only act upon the changes (ie, STILL -> IN_VEHICLE ). 我不确定为什么你需要定期更新,但我建议改变逻辑以接受最后给定的活动作为当前活动,并且只对变化采取行动(即, STILL - > IN_VEHICLE )。

如果长时间没有变化,它将不会发送更新,直到发生变化...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM