开发者

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.

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

In the Service I have:

    @Override 
public void onCreate(){
    super.onCreate();
    GetSettings();

}
public static String getDisplay() {
    return display;
}

GetSettings populates the value of display that is returned in getDisplay() from a settings.xml file.

However when I load up any code in the activity it simply fails and returns nothing but null values.

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.


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜