谷歌Play服务行为识别断开没有任何错误 [英] Google Play services activity recognition disconnects without any error

查看:193
本文介绍了谷歌Play服务行为识别断开没有任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,记录ON / OFF事件和用户的活动画面后台服务(通过活动启动)。

有关的活动中,我使用谷歌API客户端。该应用程序正常工作的摩托3G手机即记录活性和屏幕,但是,HTC行为识别停止一个电话。

我已经做了一些更新code,但仍有几分钟后,该活动识别停止的问题。正如另一位成员的建议,我还远销都在 Android的支持 - v4.jar Android的支持-V7-appcompat.jar 文件,但仍然问题是存在的。

的手机的位置上,这是不上省电模式。另外,我更新了我的SDK以及谷歌播放服务的手机到最新的一个,但几分钟后,还是我的API客户端断开连接。下面是我用code文件。

请帮我纠正。我使用的Eclipse。

MyActiviy:

 公共类MyActivity扩展活动实现GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {    私人的PendingIntent pIntent;
    GoogleApiClient mGoogleApiClient;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        mGoogleApiClient =新GoogleApiClient.Builder(this).addApi(ActivityRecognition.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
        mGoogleApiClient.connect();
        IntentFilter的过滤器=新的IntentFilter();
        filter.addAction(ACTIVITY_RECOGNITION); //进行过滤
      }
    @覆盖    公共无效onConnected(捆绑为arg0){
        意向意图=新意图(这一点,ActivityRecognitionService.class);
        pIntent = PendingIntent.getService(这一点,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient,0,pIntent); // 0
    }
    // @覆盖
    公共无效onConnectionSuspended(INT为arg0){
        // TODO自动生成方法存根
        mGoogleApiClient.connect(); //我发现这个最近,但仍然应用不工作
    }
    @覆盖
    公共无效onConnectionFailed(ConnectionResult结果){
        // TODO自动生成方法存根    }
}

ActivityRecognitionService

 公共类ActivityRecognitionService扩展IntentService {    私人字符串标记=appLogs ......;
    私人长期FNAME;    公共ActivityRecognitionService(){
        超(我的行为识别服务);
    }    @覆盖
    保护无效onHandleIntent(意向意图){
        如果(ActivityRecognitionResult.hasResult(意向)){
            ActivityRecognitionResult结果= ActivityRecognitionResult.extractResult(意向);
            Log.i(TAG,的getType(result.getMostProbableActivity()的getType())+的T+ result.getMostProbableActivity()getConfidence());
        }
    }    私人字符串的getType(整型){
        如果(类型== DetectedActivity.UNKNOWN)
            返回未知;
        否则,如果(类型== DetectedActivity.IN_VEHICLE)
            回到车辆;
        否则,如果(类型== DetectedActivity.ON_BICYCLE)
            回到自行车;
        否则,如果(类型== DetectedActivity.ON_FOOT)
            回到步行;
        否则,如果(类型== DetectedActivity.STILL)
            返回还是
        否则,如果(类型== DetectedActivity.TILTING)
            返回倾斜;
        否则,如果(类型== DetectedActivity.RUNNING)
            返回运行;
        否则,如果(类型== DetectedActivity.WALKING)
            返回行走;
        其他
            返回;
    }


解决方案

正如我刚才已经回答这里,似乎有周围也没有路。活动汇报后,手机将停止仍然有一段时间了。

如果你想记录即使手机仍然,我看到了两种方式:

1)完全依赖ActivityRecognition API和记录还是直到SIGNIFICANT_MOTION将谷歌服务及ActivityRecognition被检测到启动送你新的更新;

2)写自己的简单StillActivityRecognitionService,当没有来自官方的API更新其启动。这种服务应该听加速度传感器,除preT传感器事件(偏离均值,峰值等),并把它的决定还是/还不是。

I am writing a background service (started by an activity) that records Screen on/off events and user's activity.

For activity, I am using google api client. The app works correctly on Moto G phones i.e. records both activity and screen but activity recognition stops on HTC one phone.

I have done few updates to code but still there is an issue that activity recognition stops after few minutes. As suggested by another member, I also exported both the android-support-v4.jar and android-support-v7-appcompat.jar files but still the issue is there.

The phone's location is on and it is not on power saving mode. Also, I updated my SDK as well as google play services on phone to the latest one, but still my api client disconnects after few minutes. Below are the code files that I used.

Please help me to correct this. I am using eclipse.

MyActiviy:

public class MyActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private PendingIntent pIntent;
    GoogleApiClient mGoogleApiClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
        mGoogleApiClient.connect();
        IntentFilter filter = new IntentFilter();
        filter.addAction("ACTIVITY_RECOGNITION");//For filtering
      }


    @Override

    public void onConnected(Bundle arg0) {
        Intent intent = new Intent(this, ActivityRecognitionService.class);
        pIntent = PendingIntent.getService(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 0, pIntent);//0
    }
    //@Override 
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub  
        mGoogleApiClient.connect(); //I found this recently, but still app doesn't works
    } 
    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub

    }
}

ActivityRecognitionService

public class ActivityRecognitionService extends IntentService {

    private String TAG = "appLogs...";
    private long fName;

    public ActivityRecognitionService() {
        super("My Activity Recognition Service");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if(ActivityRecognitionResult.hasResult(intent)){
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            Log.i(TAG, getType(result.getMostProbableActivity().getType()) + "t" + result.getMostProbableActivity().getConfidence());
        }
    }

    private String getType(int type){
        if(type == DetectedActivity.UNKNOWN) 
            return "Unknown";
        else if(type == DetectedActivity.IN_VEHICLE)
            return "In Vehicle";
        else if(type == DetectedActivity.ON_BICYCLE)
            return "On Bicycle";
        else if(type == DetectedActivity.ON_FOOT)
            return "On Foot";
        else if(type == DetectedActivity.STILL)
            return "Still";
        else if(type == DetectedActivity.TILTING)
            return "Tilting";
        else if(type == DetectedActivity.RUNNING)
            return "Running";
        else if(type == DetectedActivity.WALKING)
            return "Walking";
        else
            return "";
    }

解决方案

As I have just answered here, it seems that there is no way around. Activity reporting will stop after phone is "still" for some time.

If you want to record even if the phone is "still", I see two ways:

1) rely entirely on the ActivityRecognition API and record "still" until a SIGNIFICANT_MOTION will be detected by Google Services and ActivityRecognition start send you new updates;

2) to write your own simple StillActivityRecognitionService, which starts when there is no updates from "official" API. This Service should listen to the accelerometer sensors, interpret sensor events (deviation from the mean, peak values etc.) and send it's decision "still"/"not still".

这篇关于谷歌Play服务行为识别断开没有任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆