If you need to be able to write a Service that can perform complicated communication with clients in remote processes (beyond simply the use of Context.startService
to send commands to it), then you can use the Messenger
class instead of writing full AIDL files.
Activity:
1 package org.mobiletrain.messenger; 2 3 import android.app.Activity; 4 5 public class MainActivity extends Activity { 6 7 private Button bind; 8 private Button send; 9 10 private Messenger messenger; 11 private Messenger sendMessenger; 12 13 Handler handler = new Handler() { 14 @Override 15 public void handleMessage(Message msg) { 16 System.out.println("acitivty handler"); 17 super.handleMessage(msg); 18 } 19 }; 20 21 ServiceConnection conn = new ServiceConnection() { 22 @Override 23 public void onServiceConnected(ComponentName name, IBinder binder) { 24 messenger = new Messenger(binder); 25 } 26 27 @Override 28 public void onServiceDisconnected(ComponentName arg0) { 29 30 } 31 }; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 38 bind = (Button) findViewById(R.id.bindButton); 39 send = (Button) findViewById(R.id.sendButton); 40 41 bind.setOnClickListener(new OnClickListener() { 42 @Override 43 public void onClick(View v) { 44 Intent intent = new Intent(); 45 intent.setClass(MainActivity.this, MessengerService.class); 46 bindService(intent, conn, Context.BIND_AUTO_CREATE); 47 } 48 }); 49 50 send.setOnClickListener(new OnClickListener() { 51 @Override 52 public void onClick(View v) { 53 sendMessenger = new Messenger(handler); 54 Message msg = Message.obtain(null, 0); 55 msg.replyTo = sendMessenger; 56 try { 57 messenger.send(msg); 58 } catch (RemoteException e) { 59 e.printStackTrace(); 60 } 61 } 62 }); 63 } 64 65 }
布局文件:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context=".MainActivity" > 6 7 <Button 8 android:id="@+id/bindButton" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_alignParentTop="true" 12 android:layout_centerHorizontal="true" 13 android:layout_marginTop="149dp" 14 android:text="bind" /> 15 16 <Button 17 android:id="@+id/sendButton" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_below="@id/bindButton" 21 android:layout_alignLeft="@id/bindButton" 22 android:text="send" 23 /> 24 25 </RelativeLayout>
Service:
1 package org.mobiletrain.messenger; 2 3 import android.app.Service; 4 5 public class MessengerService extends Service { 6 7 Messenger replyMessenger = null; 8 Messenger messenger = null; 9 10 @Override 11 public IBinder onBind(Intent arg0) { 12 messenger = new Messenger(handler); 13 return messenger.getBinder(); 14 } 15 16 Handler handler = new Handler() { 17 @Override 18 public void handleMessage(Message msg) { 19 if (msg.what == 0) { 20 replyMessenger = msg.replyTo; 21 new MyThread().start(); 22 } 23 super.handleMessage(msg); 24 } 25 26 }; 27 28 class MyThread extends Thread { 29 public void run() { 30 System.out.println("Thread start"); 31 try { 32 Thread.sleep(5 * 1000); 33 } catch (InterruptedException e) { 34 e.printStackTrace(); 35 } 36 System.out.println("Thread finish"); 37 Message msg = Message.obtain(null, 0); 38 try { 39 replyMessenger.send(msg); 40 } catch (RemoteException e) { 41 e.printStackTrace(); 42 } 43 } 44 } 45 }
注册Service:
1 <service android:name="org.mobiletrain.messenger.MessengerService"></service>