Google Play服务活动识别断开连接,无任何错误 [英] Google Play services activity recognition disconnects without any error

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

问题描述

我正在写一个记录屏幕开/关事件和用户活动的后台服务(开始)。



对于活动,我使用的是谷歌api客户端。该应用程序在Moto G手机上正常工作,即记录活动和屏幕,但活动识别在HTC一部手机上停止。



我已经做了很少的代码更新,但仍然有一个几分钟后活动识别停止的问题。如另一位成员所建议的,我还导出了 android-support-v4.jar android-support-v7-appcompat.jar 文件,但仍然存在问题。



手机的位置已打开,并且未处于省电模式。此外,我更新了我的SDK以及谷歌播放服务在手机上的最新的,但仍然我的api客户端几分钟后断开连接。以下是我使用的代码文件。



请帮我纠正这个。我正在使用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); //用于过滤
}


@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
}
// @覆盖
public void onConnectionSuspended(int arg0){
/ / TODO自动生成的方法stub
mGoogleApiClient.connect(); //我最近发现了,但仍然应用程序不起作用
}
@Override
public void onConnectionFailed(ConnectionResult result){
// TODO自动生成的方法stub

}
}

ActivityRecognitionService

  public class ActivityRecognitionService extends IntentService {

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

public ActivityRecognitionService(){
super(我的活动识别服务);
}

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

private String getType(int type){
if(type == DetectedActivity.UNKNOWN)
returnUnknown;
else if(type == DetectedActivity.IN_VEHICLE)
returnIn Vehicle;
else if(type == DetectedActivity.ON_BICYCLE)
返回On Bicycle;
else if(type == DetectedActivity.ON_FOOT)
returnOn foot;
else if(type == DetectedActivity.STILL)
returnStill;
else if(type == DetectedActivity.TILTING)
returnTilting;
else if(type == DetectedActivity.RUNNING)
returnRunning;
else if(type == DetectedActivity.WALKING)
returnWalking;
else
return;
}


解决方案

一个href =https://stackoverflow.com/a/32965481/5333782>这里,似乎没有办法。电话静止一段时间后,活动报告将停止。



如果您想要录制,即使手机是静止,我看到两种方式: p>

1)完全依赖于ActivityRecognition API并记录静止,直到Google服务检测到SIGNIFICANT_MOTION,并且ActivityRecognition开始发送新的更新;



2)编写自己的简单的StillActivityRecognitionService,当没有来自官方API的更新时,它将开始。该服务应该听加速度计传感器,解释传感器事件(偏离平均值,峰值等),并将其决定为仍然/不静止。


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".

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

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