2012年6月8日 星期五

[Android] [JAVA] bindService

bindService可將Activity與Service連接
當連線成功後,會自動呼叫執行這個connection內的onServiceConnected(ComponentName className, IBinder service) function,在這function會接收到由service內的onBinde()所丟出來的Ibinder物件,利用這IBinder物件取得Service物件,就可以直接操作Service內各個public 的method

[Activity]
1). 宣告MyService的變數,用此讀取MyService的資料

2). 宣告ServiceConnection的變數,並撰寫onServiceConnected及onServiceDisconnected的method

3). 使用bindService方法連結Activity及Service

[Service]
1). 宣告MyBinder的變數

2). 定義MyBinder的類別來取得MyService的物件實例

3). 使用onBind的方法綁定Service,返回一個ibinder的對象進行操作

4). 撰寫剩下的onRebind、onUnbind

//=============================================================
[Main.xml]
package com.servicetest2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
 private Intent intent;
 private MyService myService;
 private boolean flag;
 private ServiceConnection serviceconnection = new ServiceConnection() {

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   Toast.makeText(Main.this, "Bind Service", Toast.LENGTH_SHORT).show();
   myService = ((MyService.MyBinder) service).getService();
   flag = true;
   Log.d("Main","Bind");
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {
   Toast.makeText(Main.this, "Bind Service Failed", Toast.LENGTH_SHORT).show();
   myService = null;
  }

 };

 public void onClick(View view) {
  switch (view.getId()) {
  case R.id.btnStartService:
   startService(intent);
   break;
  case R.id.btnStopService:
   stopService(intent);
   break;
  case R.id.btnBindService:
   bindService(intent, serviceconnection, Context.BIND_AUTO_CREATE);
   break;
  case R.id.btnUnbindService:
   // 當有bind,才會unbind
   if (flag == true) {
    unbindService(serviceconnection);
    flag = false;
    Log.d("switch","if");
   }
   Log.d("switch","break");
   break;
  }
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  flag = false;

  // 取得資源
  Button btnStartService = (Button) findViewById(R.id.btnStartService);
  Button btnBindService = (Button) findViewById(R.id.btnBindService);
  Button btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
  Button btnStopService = (Button) findViewById(R.id.btnStopService);

  // 設置按下按鈕的物件實例
  btnStartService.setOnClickListener(this);
  btnBindService.setOnClickListener(this);
  btnUnbindService.setOnClickListener(this);
  btnStopService.setOnClickListener(this);

  intent = new Intent(this, MyService.class);

 }
}


//-------------------------------------------------------------------------------------------------------------
[MyService.xml]
package com.servicetest2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service{

 public MyBinder myBinder=new MyBinder();
 
 @Override
 public IBinder onBind(Intent intent) {
  Log.d("MyService","onBind");
  return myBinder;
 }
 
 @Override
 public void onRebind(Intent intent){
  Log.d("MyService","onRebind");
  super.onRebind(intent);
 }
 
 @Override
 public boolean onUnbind(Intent intent){
  Log.d("MyService","onUnbind");
  return super.onUnbind(intent);
 }
 
 @Override
 public void onCreate(){
  Log.d("MyService","onCreate");
  super.onCreate();  
 }
 
 @Override
 public void onDestroy(){
  Log.d("MyService","onDestory");
  super.onDestroy();  
 }

 @Override
 public void onStart(Intent intent, int startId){
  Log.d("MyService","onStart");
  super.onStart(intent, startId);  
 }
 
 public class MyBinder extends Binder{
  MyService getService(){
   return MyService.this;
   
  }
  
 }

}

沒有留言:

張貼留言