Service和Activity之间没有响应的UI界面

时间:2012-02-13 03:03:06

标签: java android service interface android-activity

我有一个服务扩展类,负责在后台运行。 好吧,在我的情况下,每次服务收到更新时,它必须将消息传递给正在运行的活动,以便活动UI也会更新。

所以,我使用的是TabActivity,例如:Main.java,在Main.java的一个标签中,有一个名为Contact.java的活动。

我正在关注Google的前台服务示例来制作服务,而在Main.java上我绑定了服务:

// Reference to the service
public static EmasService serviceBinder;

// Handles the connection between the service and activity
private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
    // Called when the connection is made.
        serviceBinder = ((EmasService.EmasBinder)service).getService();
        Log.v("Main", "onServiceConnected");
        // Toast.makeText(getApplicationContext(), "Service connected!", Toast.LENGTH_LONG).show();
    }

    public void onServiceDisconnected(ComponentName className) {
    // Received when the service unexpectedly disconnects.
        serviceBinder = null;
        Main.this.stopService(serviceIntent);
        Toast.makeText(getApplicationContext(), "Service disconnected!", Toast.LENGTH_LONG).show();
    }
};

我定义了Contact.java:

intent = new Intent().setClass(this, Contacts.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("").setIndicator(createTabView(this,"Contact",
                      res.getDrawable(R.drawable.contact_icon)))
                  .setContent(intent);
    tabHost.addTab(spec);
好的,现在要更新UI我使用一个名为ServiceListener.java的界面(新手问题不提它,我知道我应该使用广播意图):

public interface ServiceListener {
public void invocation(Message message);
}

然后在Contact.java上我在达到状态onResume:

时初始化了监听器
protected void bindInterface() {
    // call bind service instance to setListener this activity
    Main.serviceBinder.setListener(this);
}

并且在服务上它将被初始化:

public void setListener(ServiceListener sListener) {
    Log.v(TAG, "setListener");
    //if(listener == null) {
        //this.listener = null;
        this.listener = sListener;
    //}
}

然后,每次需要更新UI时,它都会在Vector上设置新队列:

lMessage.setData(bundle);
interfaceMessage.add(lMessage);

之后,线程会逐个将消息发送到接口:

while(listenerIsRun) {
            try {
                Thread.sleep(300);
                //Log.v(TAG, "InterfaceQueue: "+interfaceMessage.size());
                if(interfaceMessage.size() > 0) {
                    if(listener != null) {
                        //if(listener instanceof Contacts)
                        listener.invocation(interfaceMessage.get(0));// invoke the message
                        interfaceMessage.remove(0);// don't forget to remove invoked msg
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

并且在Contact.java上它会捕获这样的消息:

public void invocation(Message message) {
    contactHandler.sendMessage(message);
}

// TODO handler which handle ui message from service
private Handler contactHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
                // all updating process goes here...
            }
     };

好吧,我认为这种方式可以让它轻松工作,但实际上并非如此! 有时它会更新用户界面,但有时它只是没有响应,为什么?

请告诉我,如果我错过了某些内容或需要添加内容......

由于

0 个答案:

没有答案