Android的服务没有设置初始化变量 [英] Android Service not initializing variables from settings
问题描述
我正在与我的应用程序的问题,我似乎无法弄清楚。
I am running into a problem with my application that I cannot seem to figure out.
应用程序具有启动手机启动时,或者如果它已经停止,当一切完成之前的主要活动是运行启动的服务。
The application has a service that starts when the phone starts, or if it has been stopped, starts when the main activity is run before everything else is done.
在活动的onCreate我有:
In the activity's onCreate I have:
serviceIntent.setClass(this, Service.class);
bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
startService(serviceIntent);
WriteDisplay();
public void WriteDisplay(){
TextView t = (TextView)findViewById(R.id.tv1);
String strdisplay = Service.getDisplay();
t.setText(strdisplay);
}
在服务我有:
@Override
public void onCreate(){
super.onCreate();
GetSettings();
}
public static String getDisplay() {
return display;
}
的getSettings填充一个从settings.xml文件中返回getDisplay()的显示的值
GetSettings populates the value of display that is returned in getDisplay() from a settings.xml file.
然而,当我加载在活动的任何code将其简单地失败,并返回只是空值。
However when I load up any code in the activity it simply fails and returns nothing but null values.
我有调用DrawCanvas屏幕上的刷新按钮();再次,如果我打刷新按钮,它能够正确地抓住所有的变量和处理code。
I have a refresh button on the screen that invokes DrawCanvas(); again, and if I hit the refresh button it is able to grab all the variables properly and processes the code.
我不明白,为什么变量没有装上活动的第一次运行,但正确加载,当我打的刷新按钮。
I don't understand why the variables are not loaded on the first run of the activity, but loads properly when I hit the refresh button.
推荐答案
绑定到一个服务是一个异步操作。你必须等待,直到绑定完成后,然后调用服务方法。看看 ServiceConnection.onServiceConnected()
方法。它有一个的IBinder
参数。当这个方法被调用时,你可以投装订器的实际类型,并调用它的方法。
Binding to a service is an asynchronous operation. You have to wait until the binding is finished and then call service methods. Take a look at ServiceConnection.onServiceConnected()
method. It has an IBinder
argument. When this method is called you can cast binder to its actual type and call its methods.
下面是一个指导开发势必服务:的http://开发商。 android.com/guide/topics/fundamentals/bound-services.html
Here's a guide to developing bound services: http://developer.android.com/guide/topics/fundamentals/bound-services.html
您应仔细阅读并以类似的方式实现你的服务。
You should read it carefully and implement your service in a similar way.
这篇关于Android的服务没有设置初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!