Reputation: 5830
I have an broadcast receiver for DetectedActivities using the ActivityRecognition. I noticed that it did connect, but I was getting no data. This is on a Samsung Note 2. On other phones I tried it always works. This is the code to connect to google 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");
}
}
This is the code to request motions:
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();
}
}
Where "getActivityDetectionPendingIntent" is:
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);
}
What is weird, that I then restarted the phone. Entered the app and after the restart, I just jiggled the phone a bit and started getting locations. Is there a possibility for the broadcast receiver to be forced closed somehow? or the recognition activity to be broken? Did anybody else got this issue? And is there any way to check the status of my broadcast receiver? I have no logs, cause I have no issues, in the logs. It simply builds the api client, and then request the motions with this:
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
mGoogleApiClient,
Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
getActivityDetectionPendingIntent()
).setResultCallback(this);
Upvotes: 2
Views: 522
Reputation: 13549
I was investigating this myself. From the documentation:
To conserve battery, activity reporting may stop when the device is 'STILL' for an extended period of time. It will resume once the device moves again. This only happens on devices that support the Sensor.TYPE_SIGNIFICANT_MOTION hardware.
Beginning in API 21, activities may be received less frequently than the detectionIntervalMillis parameter if the device is in power save mode and the screen is off.
So to save battery the ActivityRecognition
will go to sleep (and stop firing events) when it can.
Upvotes: 3