2012-08-15 108 views
3

我已經按照本教程將一項活動與我的服務綁定在一起。 http://developer.android.com/guide/components/bound-services.html通過綁定服務從服務與活動進行溝通

我能夠調用服務的功能,但如果我想例如,改變我的一些textviews或禁用一些在服務完成,因爲工作的切換按鈕(和服務)的。會有一個簡單的方法來做到這一點?

+0

我可能正在考慮在主要活動內部創建一個靜態類並調用各種函數。這會有效嗎? – jimmyC 2012-08-15 19:16:17

回答

1

您可以使用消息在活動和服務之間發送信息。這是發送簡單數據的簡單方法,但如果您需要非常頻繁地發送數據或發送複雜數據,可能不是最佳選擇。這是一些代碼,我有我的應用程序與服務中的一種和活性,其通信的示例:

代碼的活動:

//this is where you set what you want to happen 
class IncomingHandler extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
      //this switch reads the information in the message (usually just 
      //an integer) and will do something depending on which integer is sent 
      case 1: do_something(); 
      case 2: do_something_2(); //etc. 
      default: 
       super.handleMessage(msg); 
     } 
    } 
} 

final Messenger myMessenger = new Messenger(new IncomingHandler()); 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, 
      IBinder service) { 
     myService = new Messenger(service); 
     myCallbackText = (TextView)findViewById(R.id.tv01); //This is a text view which will display status information as needed 
     myCallbackText.setText("Attached."); 

     try { 
      Message msg = Message.obtain(null, 
        1); 
      msg.replyTo = mMessenger; //here we send an instance of our messenger implementation as the replyTo address 
      mService.send(msg); 

      msg = Message.obtain(null, 
        3, this.hashCode(), 0); 
      mService.send(msg); //send a message with the value "3" 
     } catch (RemoteException e) { 
      //nothing you can do if the server isn't active 
     } 

     Toast.makeText(Service_testActivity.this, R.string.remote_service_connected, 
       Toast.LENGTH_SHORT).show();//confirmation that the connection happened successfully 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     // This is called when the connection with the service has been 
     // unexpectedly disconnected -- that is, its process crashed. 
     mService = null; 
     mCallbackText = (TextView)findViewById(R.id.tv01);//same textview as before 
     mCallbackText.setText("Disconnected."); 

     Toast.makeText(Service_testActivity.this, R.string.remote_service_disconnected, 
       Toast.LENGTH_SHORT).show(); 
    } 
}; 



代碼在服務: 在服務中,您需要讓代碼(與活動中的代碼非常相似)接收消息並將msg.replyTo字段保存爲Messenger對象。有一個例子某處,這將讓你做一個對象,然後使用像這樣的IncomingHandler:

ArrayList<Messenger> mClients = new ArrayList<Messenger>(); 
class IncomingHandler extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
      case MSG_REGISTER_CLIENT: 
       mClients.add(msg.replyTo); 
       break; 
      case MSG_UNREGISTER_CLIENT: 
       mClients.remove(msg.replyTo); 
       break; 
      default: 
       super.handleMessage(msg); 
     } 
    } 
} 

這可以讓你的服務來跟蹤多個客戶端的同時,併發送消息到指定的客戶。要發送消息,只需使用這樣的事情:

mClients.get(1).send(Message.obtain(null, 3, new Random().nextInt(), 0)); 
//sends a message to the first client saved in the list