綁定Service須要調用html
傳入一個ServiceConnection 對象,該對象是一個接口,實例化時須要實現該接口,它的做用就是得到Service的IBinder對象,經過IBinder對象能夠實現與Service的通訊。android
Service的的代碼:app
1 package com.example.servicetest; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.Binder; 6 import android.os.IBinder; 7 import android.util.Log; 8 9 public class BindServiceTest extends Service { 10 private int count = 0; 11 private boolean quit = false; 12 private MyBinder mBinder = new MyBinder(); 13 14 public class MyBinder extends Binder { 15 public int count() { 16 return count; 17 } 18 } 19 20 @Override 21 public IBinder onBind(Intent arg0) { 22 Log.i("csx", "onBind"); 23 return mBinder; 24 } 25 26 @Override 27 public void onCreate() { 28 29 super.onCreate(); 30 Log.i("csx", "onCreate"); 31 new Thread() { 32 33 @Override 34 public void run() { 35 while (!quit) { 36 try { 37 Thread.sleep(1000); 38 } catch (InterruptedException e) { 39 40 e.printStackTrace(); 41 } 42 count++; 43 44 } 45 46 } 47 48 }.start(); 49 } 50 51 @Override 52 public int onStartCommand(Intent intent, int flags, int startId) { 53 Log.i("csx", "onStartCommand"); 54 return START_STICKY; 55 } 56 57 @Override 58 public boolean onUnbind(Intent intent) { 59 Log.i("csx", "onUnbind"); 60 return true; 61 } 62 63 @Override 64 public void onDestroy() { 65 super.onDestroy(); 66 this.quit = true; 67 Log.i("csx", "onDestroy"); 68 } 69 70 }
組件的代碼:ide
1 package com.example.servicetest; 2 3 import com.example.servicetest.BindServiceTest.MyBinder; 4 5 import android.content.ComponentName; 6 import android.content.Intent; 7 import android.content.ServiceConnection; 8 import android.os.Bundle; 9 import android.os.IBinder; 10 import android.support.v7.app.ActionBarActivity; 11 import android.util.Log; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.Button; 15 import android.widget.Toast; 16 17 public class MainActivity extends ActionBarActivity { 18 public static final String SERVICE_ACTION = "com.example.servicetest.BindServiceTest"; 19 Button bindButton, unbindButton, stateButton; 20 ServiceConnection conn; 21 BindServiceTest.MyBinder mBinder; 22 boolean isBind = false; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 bindButton = (Button) findViewById(R.id.button_bind); 29 unbindButton = (Button) findViewById(R.id.button_unbind); 30 stateButton = (Button) findViewById(R.id.button_get_state); 31 32 conn = new ServiceConnection() { 33 34 @Override 35 public void onServiceConnected(ComponentName name, IBinder service) { 36 Log.i("csx", "onServiceConnected"); 37 mBinder = (MyBinder) service; // 鏈接service以後將service中實現的Binder返回給本地聲明的Binder 38 39 } 40 41 @Override 42 public void onServiceDisconnected(ComponentName name) { 43 Log.i("csx", "onServiceDisconnected");// 意外斷開鏈接會調用該函數 44 45 } 46 47 }; 48 49 bindButton.setOnClickListener(new OnClickListener() { 50 51 @Override 52 public void onClick(View v) { 53 Intent service = new Intent(); 54 service.setAction(SERVICE_ACTION); 55 bindService(service, conn, BIND_AUTO_CREATE);// 綁定服務 56 isBind = true; 57 58 } 59 }); 60 61 unbindButton.setOnClickListener(new OnClickListener() { 62 63 @Override 64 public void onClick(View v) { 65 if (!isBind) { 66 Toast.makeText(MainActivity.this, "請先綁定服務", Toast.LENGTH_SHORT).show(); 67 return; 68 69 } 70 71 unbindService(conn);// 解除綁定服務 72 isBind = false; 73 74 } 75 }); 76 77 stateButton.setOnClickListener(new OnClickListener() { 78 79 @Override 80 public void onClick(View v) { 81 if (!isBind) { 82 Toast.makeText(MainActivity.this, "請先綁定服務", Toast.LENGTH_SHORT).show(); 83 return; 84 85 } 86 Toast.makeText(MainActivity.this, "" + mBinder.count(), Toast.LENGTH_SHORT).show();// 經過mBinder獲取service內部的數據 87 88 } 89 }); 90 91 } 92 }