【发布时间】:2013-06-26 09:24:39
【问题描述】:
我在从另一个 Android 应用 (API 17) 启动服务时遇到问题。 但是,如果我确实从 shell 运行 'am',则服务可以正常启动。
# am startservice com.xxx.yyy/.SyncService
Starting service: Intent { act=android.intent.action.MAIN cat=
[android.intent.category.LAUNCHER] cmp=com.xxx.yyy/.SyncService }
(service starts fine at this point)
# am to-intent-uri com.xxx.yyy/.SyncService
intent:#Intent;action=android.intent.action.MAIN;
category=android.intent.category.LAUNCHER;
component=com.xxx.yyy/.SyncService;end
所以,当我在代码中执行相同操作时,看起来我没有遗漏任何意图:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setComponent(new ComponentName("com.xxx.yyy", ".SyncService"));
ComponentName c = ctx.startService(i);
if (c == null) { Log.e(TAG, "failed to start with "+i); }
我得到的是(当时服务没有运行):
E/tag( 4026): failed to start with Intent {
act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER]
cmp=com.xxx.yyy/.SyncService }
我在服务上没有意图过滤器,我不想设置一个,我真的想了解我在通过其组件名称启动它时做错了什么,或者可能会造成什么这是不可能的。
【问题讨论】:
-
@Richa 此页面有使用 Intent.setClassName() 的示例,但它并没有真正回答我的具体问题...
-
另外,检查 logcat 是否有任何有意义的错误(不要过滤 logcat,因为您可能会错过一些有价值/重要的东西)