我有一个使用ActivityRecognition的DetectedActivities广播接收器。我注意到它确实连接了,但我没有得到任何数据。这是在三星Note 2上。在其他手机上我试过了,它总是有效的。这是连接到谷歌api的代码:
public synchronized void buildGoogleApiClient() {
if(PSLocationCenter.getInstance().pref.getAutoPilot(context) || (PSTripDBFactory.getInstance(context).getActiveTrip()!= null)){
Log.i("","autopilot, will activate");
if(mGoogleApiClient == null || !mGoogleApiClient.isConnected()) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(ActivityRecognition.API)
.build();
Log.i("", "autopilot build google api client");
mGoogleApiClient.connect();
Log.i("", "autopilot register receiver");
LocalBroadcastManager.getInstance(context).registerReceiver(mBroadcastReceiver,
new IntentFilter(Constants.BROADCAST_ACTION));
}else{
Log.i("", "autopilot is already connected");
if(!PSLocationCenter.getInstance().pref.getMotionDetection(context)){
requestActivityUpdatesButtonHandler();
}
if(PSTripDBFactory.getInstance(context).getActiveTrip()== null) {
PSLocationCenter.getInstance().requestLocationUpdatesFromMotion();
}
}
}else{
if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
removeActivityUpdatesButtonHandler();
}
Log.i("","autopilot, will NOOOT activate");
}
}
这是请求动作的代码:
public void requestActivityUpdatesButtonHandler() {
Log.i("", "autopilot request activity update");
Utils.appendLog("AUTOPILOT requestActivityUpdatesButtonHandler", false);
if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
Log.i("","AUTOPILOT requestActivityUpdatesButtonHandler GOOGLE API CLIENT CONNECTED");
Utils.appendLog("AUTOPILOT requestActivityUpdatesButtonHandler GOOGLE API CLIENT CONNECTED", false);
PSLocationCenter.getInstance().pref.setMotionDetection(context, true);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
mGoogleApiClient,
Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
getActivityDetectionPendingIntent()
).setResultCallback(this);
}else if(mGoogleApiClient != null && mGoogleApiClient.isConnecting()){
Log.i("","AUTOPILOT requestActivityUpdatesButtonHandler GOOGLE API CLIENT CONNECTING");
Utils.appendLog("AUTOPILOT requestActivityUpdatesButtonHandler GOOGLE API CLIENT CONNECTING", false);
Handler han = new Handler();
han.postDelayed(new Runnable() {
@Override
public void run() {
requestActivityUpdatesButtonHandler();
}
},500);
}else{
Log.i("","AUTOPILOT requestActivityUpdatesButtonHandler GOOGLE API CLIENT NOT CONNECTED");
Utils.appendLog("AUTOPILOT requestActivityUpdatesButtonHandler GOOGLE API CLIENT NOT CONNECTED", false);
buildGoogleApiClient();
}
}
其中"getActivityDetectionPendingIntent"为:
private PendingIntent getActivityDetectionPendingIntent() {
Log.i("", "autopilot get activity detection pending intent");
// Reuse the PendingIntent if we already have it.
if (mActivityDetectionPendingIntent != null) {
return mActivityDetectionPendingIntent;
}
Intent intent = new Intent(context, DetectedActivitiesIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent backListener when calling
// requestActivityUpdates() and removeActivityUpdates().
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
奇怪的是,我重新启动了手机。进入应用程序,重新启动后,我只是轻轻摇晃了一下手机,开始获取位置。是否有可能以某种方式强制关闭广播接收器?还是识别活动被打破?其他人有这个问题吗?有什么方法可以检查我的广播接收器的状态吗?我没有日志,因为日志中没有任何问题。它只需构建api客户端,然后用以下命令请求动作:
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
mGoogleApiClient,
Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
getActivityDetectionPendingIntent()
).setResultCallback(this);
我自己也在调查这个问题。来自文件:
为了节省电池,当设备处于长时间"静止"。一旦设备恢复再次移动。这种情况仅发生在支持Sensor.TYPE_SIGNIFICANT_MOTION硬件。
从API 21开始,接收活动的频率可能低于detectionIntervalMillis参数(如果设备处于节能状态)模式,屏幕关闭。
因此,为了节省电池,ActivityRecognition
将在可能的情况下进入睡眠状态(并停止发射事件)。