我有一个使用parse.com作为其后端的android项目。为了减少api调用次数,我一直在将“不太重要”的数据固定到本地数据存储区,并尝试在每个用户会话中同步一次。我为此使用IntentService。我正在调用IntentService,如下所示。但是当IntentService被调用时,我看不到任何日志消息或调试断点。调试IntentService同步ParseObjects
问题1:如何调试任何意图服务?
问题2:我想为每个用户会话执行一次服务(每次用户打开和关闭应用程序时)。我不想将代码添加到活动的onPause方法中,因为我的应用程序有多个活动,因此会话中多次调用onPause。因此,我正在从用户可以退出应用程序之前的最后一个屏幕的活动onBackPressed调用该服务。这是否可靠?
意图调用代码:
@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代码
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");
}
});
}
});
}
}
}
});
}
}
添加一些'日志。在'onHandleIntent'的开头部署了'd' – pskink
你确定东西正在本地数据存储中?它可能是你的列表是空的,并且该语句永远不会被记录。您可以尝试在解析调用的旁边记录某些内容。 – Naveed
我将Log.d添加到onHandleIntent的乞求中,它也没有被打印出来。 – 55597