xml:
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="com.zzw.testmessenger.MainActivity" > 6 7 <Button 8 android:id="@+id/bind" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_alignParentBottom="true" 12 android:layout_alignParentLeft="true" 13 android:text="绑定" /> 14 15 <Button 16 android:id="@+id/send" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:layout_alignParentBottom="true" 20 android:layout_alignParentRight="true" 21 android:text="发送数据" /> 22 23 <Button 24 android:id="@+id/start" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_alignParentBottom="true" 28 android:layout_centerHorizontal="true" 29 android:text="加法" /> 30 31 </RelativeLayout>
MainActivity:
1 package com.zzw.testmessenger; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.os.Message; 10 import android.os.Messenger; 11 import android.os.RemoteException; 12 import android.util.Log; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.widget.Button; 16 17 public class MainActivity extends Activity implements OnClickListener { 18 Button bind, send, start; 19 ServiceConnection sc; 20 Messenger sender; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 bind = (Button) findViewById(R.id.bind); 28 bind.setOnClickListener(this); 29 send = (Button) findViewById(R.id.send); 30 send.setOnClickListener(this); 31 start = (Button) findViewById(R.id.start); 32 start.setOnClickListener(this); 33 } 34 35 @Override 36 public void onClick(View v) { 37 switch (v.getId()) { 38 case R.id.bind: 39 bindService(); 40 break; 41 case R.id.send: 42 sendMessageToService(); 43 break; 44 case R.id.start: 45 useStartService(); 46 break; 47 } 48 } 49 50 private void bindService() { 51 sc = new ServiceConnection() { 52 @Override 53 public void onServiceDisconnected(ComponentName name) { 54 } 55 56 @Override 57 public void onServiceConnected(ComponentName name, IBinder service) { 58 sender = new Messenger(service); 59 } 60 }; 61 62 Intent intent = new Intent(MainActivity.this, MessengerSerivice.class); 63 bindService(intent, sc, BIND_AUTO_CREATE); 64 } 65 66 private void sendMessageToService() { 67 try { 68 Message msg = new Message(); 69 int a = (int) (Math.random() * 20); 70 int b = (int) (Math.random() * 20); 71 int s[] = { a, b }; 72 msg.obj = s; 73 sender.send(msg); 74 Log.d("sendMessageToService", "发送成功----->"); 75 } catch (RemoteException e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } 79 } 80 81 private void useStartService() { 82 Intent intent = new Intent(MainActivity.this, MessengerSerivice.class); 83 startService(intent); 84 } 85 86 @Override 87 protected void onDestroy() { 88 unbindService(sc); 89 Intent intent = new Intent(MainActivity.this, MessengerSerivice.class); 90 stopService(intent); 91 super.onDestroy(); 92 } 93 }
MessengerSerivice:
1 package com.zzw.testmessenger; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Handler; 6 import android.os.IBinder; 7 import android.os.Message; 8 import android.os.Messenger; 9 import android.util.Log; 10 11 public class MessengerSerivice extends Service { 12 private Messenger messenger; 13 Handler handler; 14 int a, b, sum; 15 16 @Override 17 public void onCreate() { 18 Log.d("MessengerSerivice", "onCreate"); 19 handler = new Handler() { 20 @Override 21 public void handleMessage(Message msg) { 22 super.handleMessage(msg); 23 int[] s = (int[]) msg.obj; 24 a = s[0]; 25 b = s[1]; 26 Log.d("收到的msg", "a=" + a + " b=" + b); 27 } 28 }; 29 messenger = new Messenger(handler); 30 super.onCreate(); 31 } 32 33 @Override 34 public int onStartCommand(Intent intent, int flags, int startId) { 35 Log.d("MessengerSerivice", "onStartCommand"); 36 sum = a + b; 37 Log.d("在onStartCommand中算出的和", sum + ""); 38 return super.onStartCommand(intent, flags, startId); 39 } 40 41 @Override 42 public IBinder onBind(Intent intent) { 43 Log.d("MessengerSerivice", "onBind"); 44 return messenger.getBinder(); 45 } 46 47 }