Android 服务似乎从未启动 - onStartCommand() 未调用
我正在尝试创建一个在后台处理文件 I/O 的服务。更新数据的活动将绑定到服务并调用服务的方法来执行 I/O。我使用 Android 文档作为指导。
但是,我的服务似乎不想启动。在我的 Activity 的 onCreate() 方法中,我有:
Intent smsIntent = new Intent(this, SurveyManagerService.class);
String surveyFilename = getIntent().getStringExtra("surveyFilename");
System.out.println(startService(smsIntent)); //testing if it starts
SurveyManagerServiceConnection connection = new SurveyManagerServiceConnection();
sms = connection.getSurveyManagerService();
在第 3 行 LogCat 输出一个 ComponentInfo 对象,因此看起来服务已创建;但是,短信为空。此外,SurveyManagerService onStartCommand() 方法似乎从未被调用:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
System.out.println("starting service");
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
openSurveyFromIntent(intent);
return START_REDELIVER_INTENT;
}
我从未在 LogCat 中看到任何“启动服务”输出,也没有出现 Toast。
我的服务在我的清单中声明为:
<service android:name=".SurveyManagerService" android:exported="false" android:enabled="true">
</service>
我是否遗漏了一些明显的东西?让我知道我应该提供哪些其他信息来诊断问题。
I am trying to create a service that will handle file I/O in the background. Activities that update the data will bind to the service and call the service's methods to perform the I/O. I am using the Android documentation for guidance.
My service does not seem to want to start, however. In the onCreate() method of my Activity, I have:
Intent smsIntent = new Intent(this, SurveyManagerService.class);
String surveyFilename = getIntent().getStringExtra("surveyFilename");
System.out.println(startService(smsIntent)); //testing if it starts
SurveyManagerServiceConnection connection = new SurveyManagerServiceConnection();
sms = connection.getSurveyManagerService();
At line 3 LogCat outputs a ComponentInfo object, so it would appear that the service is created; however, sms is null. Furthermore, the SurveyManagerService onStartCommand() method never seems to be called:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
System.out.println("starting service");
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
openSurveyFromIntent(intent);
return START_REDELIVER_INTENT;
}
I never see any "starting service" output in LogCat, nor does the Toast appear.
My service is declared in my manifest as:
<service android:name=".SurveyManagerService" android:exported="false" android:enabled="true">
</service>
Am I missing something obvious? Let me know what other information I should provide to diagnose the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请务必在 AndroidManifest.xml 中声明您的服务
Be sure to declare your service in AndroidManifest.xml
也许您不小心将服务标签放在了应用程序标签之外?然后服务就会默默地失败。虽然日志中应该有“无法启动服务意图...”错误。
Maybe you've accidentally placed the service tag outside application tag? Then the service just fails silently. Although there should be a "Unable to start service Intent..." error in the log.
看来 onCreate() 方法必须退出 在服务实际启动之前。一旦我删除了 getSurveyManagerService() 请求,并且过了一段时间,我就收到了 System.out 消息,表明服务正在启动。
不幸的是,这会产生另一个问题:活动依赖于服务来获取其数据,因此我需要确定如何使活动等待服务启动。
编辑:我的解决方案是在我的 Activity 中创建 ServiceConnection 的私有子类。然后,我重写 onServiceConnected() 方法以向 Activity 提供其数据(在本例中填充 ListView)。
It seems the onCreate() method has to exit before the service can actually start. Once I removed the getSurveyManagerService() request, and once a bit of time had passed, I received the System.out messages indicating the service was starting.
Unfortunately that creates another problem: the Activity relies on the Service for its data, so I need to determine how to cause the Activity to wait for the service to start.
EDIT: My solution was to create a private subclass of my ServiceConnection in my Activity. I then overrode the onServiceConnected() method to provide the Activity with its data (populating a ListView, in this case).
就我而言,我重构了服务类和服务类。注意到它没有在清单中自动重构。所以我必须手动更新清单中服务的完全限定名称。
In my case i refactored the service class & noticed that it was not automatically refactored in manifest. So i had to manually update the fully qualified name of the service in manifest.