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");
}
});
}
});
}
}
}
});
}
}
Question 1: How do I debug any Intent service? As mentioned in the comments you should be able to set a break point and attach the debugger to the service.
Question 2: I want to execute the service ...? This will depend on your requirement and how you want to define a session.
If a session is defined as when the user logs in/logs out then you can simply put the code simple start your in your login / log out logic
It gets a little more complicated if you define a session as when your app is in the fore ground/until it goes to the background/killed. I will try to provide as much info/cases as possible:
Starting the application is the easier part so, you can simply start your service in
onCreate
of your launcher activity or evenonCreate
ofApplication
if you sub class itClosing the app depends on what you define as
closing
. If you require the user to explicitly back out of the app then you do not need to take care of the case below:User can simply put the app in the background by pressing the home button in which case
onBackPressed
will not be called and its possible that the system cleans up your app in the background so you'll never get a chance to start your service. In this case system will callonSaveInstanceState
and give you a chance to start your service. (If you do not care a bout this case then you should be fine). One edge case here is thatonSavedInstance
state is also called on screen rotation so you'll need some logic to distinguish between when the app is going to the background vs. screen rotationOne thing on the side:
onHandleIntent
is called on the background thread so you can just use the synchronous versions offind
save
andunpin
this will help you avoid all the nesting.Hopefully this helps.