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