1

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 "";
    }
NaviRamyle
  • 3,967
  • 1
  • 31
  • 49
And_Dev
  • 113
  • 2
  • 12

1 Answers1

1

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

Community
  • 1
  • 1
mibrl12
  • 454
  • 5
  • 21
  • yup that's what i found from my research too :) – And_Dev Oct 06 '15 at 13:58
  • but i just found another phone LG that has this same issue, i mean user is using the phone but still activity sensing stopped after some time, do you have any suggestions? – And_Dev Oct 07 '15 at 16:42
  • if sensing stops it is ok, because it is a feature of this ActivityRecognition API. the problem would be if it will never start sensing again. I guess after you walk for a while ActivityRecognition wakes up and start sensing again. so as I've written in my answer - two ways: trust this Service or write your own auxiliary, which will may wake up the phone using AlarmManager with the required delays, sense accelerometers for 1-5 seconds, calculate deviation from the mean and send it's decision "still" or "unknown" to a broadcast receiver and so on – mibrl12 Oct 07 '15 at 18:54
  • but it works perfect on my other test phones..it makes sense to me that sensing stops when a user is still for a long period of time, but once user is active again then there should be a log of activity. This is what i observed on HTC. Any suggestions? BTW my app is blank activity that starts many services like location sensing etc. every thing else except for activity is working fine – And_Dev Oct 07 '15 at 18:58
  • maybe add some logs so you will be able if it enters 'onConnectionFailed'. also I would add 'mGoogleApiClient.disconnect()' in it – mibrl12 Oct 07 '15 at 19:05
  • you mean: public void onConnectionFailed(ConnectionResult result) { mGoogleApiClient.disconnect(); } Also, if I disconnects, re connection will be automatic? – And_Dev Oct 07 '15 at 19:11
  • Try to add two methods to your mGoogleApiClient : `mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(ActivityRecognition.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect();` – mibrl12 Oct 07 '15 at 19:12
  • i am doing it already i guess, see above. or am i missing any thing? – And_Dev Oct 07 '15 at 19:14
  • no more ideas, but if it help I will correct my answer. add two calls in `onCreate` in `MyActivity` to `mGoogleApiClient` as I wrote above – mibrl12 Oct 07 '15 at 19:14