【发布时间】:2015-06-13 04:06:14
【问题描述】:
我有一个使用 parse.com 作为其后端的 android 项目。为了减少 api 调用的数量,我一直将“不那么重要”的数据固定到本地数据存储中,并尝试在每个用户会话中同步一次。我为此使用了 IntentService。我正在调用 IntentService,如下所示。但是当调用 IntentService 时,我看不到任何日志消息或调试断点被调用。
问题 1:如何调试任何 Intent 服务?
问题 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的开头添加一些Log.d -
您确定这些东西正在进入本地数据存储区吗?您的列表可能为空并且该语句永远不会被记录。您可以尝试在解析调用之外记录一些内容。
-
我在onHandleIntent的请求中添加了Log.d,也没有打印出来。
-
对不起,我刚刚看到你已经添加了waitForDebugger
-
您的服务是否正常启动?尝试在实际启动服务时记录一些内容,然后查看是否曾经调用过 onHandleIntent。此外,这只是一个旁注,您确实在清单中声明了您的服务,对吗?
标签: android android-intent intentservice android-debug