Google 播放服务activity 识别断开连接没有任何错误

Google Play services activity recognition disconnects without any error

我正在编写后台服务(由 activity 启动)记录屏幕 on/off 事件和用户的 activity。

对于 activity,我正在使用 google api 客户端。该应用程序在 Moto G phones 上正常工作,即记录 activity 和屏幕,但 activity 识别在 HTC one phone.

上停止

我对代码做了一些更新,但仍然存在 activity 识别在几分钟后停止的问题。根据另一位成员的建议,我还导出了 android-support-v4.jarandroid-support-v7- appcompat.jar 个文件,但问题仍然存在。

phone的位置是开着的,不是省电模式。此外,我将我的 SDK 以及 phone 上的 google 播放服务更新为最新版本,但我的 api 客户端仍然在几分钟后断开连接。以下是我使用的代码文件。

请帮我更正这个问题。我正在使用日食。

我的活动:

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 "";
    }

我刚刚回答,看来是没有办法了。 Activity 报告将在 phone 为 "still" 一段时间后停止。

如果phone是"still"也想录音,我看有两种方式:

1) 完全依靠Activity识别API并记录"still"直到SIGNIFICANT_MOTION会被Google服务检测到[=26] =]识别开始向您发送新更新;

2) 编写您自己的简单 StillActivityRecognitionService,它会在 "official" API 没有更新时启动。该服务应监听加速度计传感器,解释传感器事件(偏离平均值、峰值等)并发送它的决定 "still"/"not still".