我正在尝试创建一个将在后台处理文件 I/O 的服务。更新数据的 Activity 将绑定(bind)到服务并调用服务的方法来执行 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。
我的服务在我的 list 中声明为:
<service android:name=".SurveyManagerService" android:exported="false" android:enabled="true">
</service>
我是否遗漏了一些明显的东西?让我知道我应该提供哪些其他信息来诊断问题。
最佳答案
请务必在 AndroidManifest.xml 中声明您的服务
<application>
...
<service android:name=".path.to.service.MyService"/>
</application>
关于Android 服务似乎从未启动 - onStartCommand() 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7689891/