I have an android project that uses parse.com for its backend. To cut the number of api calls, I had been pinning "not so important" data to the local data store and trying to sync it once every user session. I am using a IntentService for this. I am calling the IntentService as shown below. But I don't see any Log messages or debugging breakpoints getting called when the IntentService gets called.
Question 1: How do I debug any Intent service?
Question 2: I want to execute the service once per user session(everytime the user opens and closes the app). I don't want to add the code to the onPause method of the activity because my app has multiple activities and so the onPause gets called multiple times in a session. Therefore I am calling the service from onBackPressed of the activity which is the last screen before the user can quit the app. Is this fool proof?
Intent Calling code :
@Override
public void onBackPressed() {
if(exitCount == 1)
{
exitCount=0;
Intent i= new Intent(this, SyncChoiceService.class);
this.startService(i);
super.onBackPressed();
}
else
{
Toast.makeText(getApplicationContext(), "Press Back again to quit.", Toast.LENGTH_SHORT).show();
exitCount++;
}
return;
}
IntentService Code
public class SyncChoiceService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public SyncChoiceService(String name) {
super("SyncChoiceService");
}
@Override
protected void onHandleIntent(Intent intent) {
//android.os.Debug.waitForDebugger();
// Adding this waitForDebugger doesn't make a difference
ParseQuery query = new ParseQuery("PostChoice");
query.fromPin();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> list, ParseException e) {
if(list!=null)
{
if(!list.isEmpty())
{
ParseObject.saveAllInBackground(list, new SaveCallback() {
@Override
public void done(ParseException e) {
ParseObject.unpinAllInBackground(list, new DeleteCallback() {
@Override
public void done(ParseException e) {
Log.i("Unpinned ","everything");
}
});
}
});
}
}
}
});
}
}