Android的服务从来没有出现启动 - onStartCommand()不叫从来没有、不叫、Android、onStartCommand

2023-09-04 11:22:44 作者:长安一夜尽落花

我想创建一个将处理文件I / O在后台的服务。活动更新数据将结合到服务和呼叫服务的方法来执行的I / O。我使用的是Android的文档进行指导。

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.

似乎我的服务并不想创业,但是。在我的活动的onCreate()方法,我有:

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();

目前3号线LogCat中输出ComponentInfo对象,因此它会出现在创建服务;但是,SMS为空。此外,SurveyManagerService onStartCommand()方法似乎从来没有被称为:

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;
}

我从来没有看到任何启动服务输出LogCat中,也不敬酒出现。

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.

推荐答案

看来的onCreate()方法has退出之前,该服务可以真正开始。有一次,我删除了getSurveyManagerService()请求,一旦有点时间就这样过去了,我收到了System.out的消息,表明该服务已开始。

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.

编辑:我的解决办法是在我的活动创造我ServiceConnection的私有子类。然后,我推翻了onServiceConnected()方法来提供其数据的活动(填充的ListView,在这种情况下)。

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).

 
精彩推荐
图片推荐