Android BinderService 过程

步骤:建立服务器端服务,暴露接口

1.BinderService

 1  /**
 2  * @Title BinderService.java
 3  * @package     cn.boxai.binderservice
 4  * @since  
 5  * @version     1.0.0 
 6  * @author      Vic Lee
 7  * @date        Aug 14, 2016-3:50:04 PM
 8  */
 9 package cn.boxai.binderservice;
10 
11 import android.app.Service;
12 import android.content.Intent;
13 import android.os.Binder;
14 import android.os.IBinder;
15 
16 public class BinderService extends Service {
17   
18 //step:1 difind mBinder 
19 IBinder mBinder=new MyBinder(); //step:3 implements mBinder
20 
21   
22 //step:2 difine MyBinder and extends
23   class MyBinder  extends Binder
24   {
25      
  //调用其他的服务 

       public BinderService getService()
       {
        return BinderService.this;
       }

 
26      public String helloWorld(String name) 
27      {
28         return "You name is :"+name;
29     } 
30   }
31
    //添加其他的服务
    public void helloservice()

    {
      for (int i = 0; i < 100; i++)
      {
        try {
          Thread.sleep(1000);
          Log.i("helloservice", i+"");
          } catch (InterruptedException e)

            {
              e.printStackTrace();
            }
      }
    }


  

  

32   //step:4 retrue mBinder Exposed interface
33     @Override
34     public IBinder onBind(Intent intent) {
35 
36         return mBinder;
37 
38     }
39 
40     
41 }

 

2.客户端

 1 package cn.boxai.binderservice;
 2 
 3 import cn.boxai.binderservice.BinderService.MyBinder;
 4 import android.os.Bundle;
 5 import android.os.IBinder;
 6 import android.app.Activity;
 7 import android.content.ComponentName;
 8 import android.content.Intent;
 9 import android.content.ServiceConnection;
10 import android.util.Log;
11 import android.view.Menu;
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 BinderServiceActivity extends Activity {
18  Button mBinderServiceButton;
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         mBinderServiceButton=(Button) findViewById(R.id.btn_BinderService);
24         mBinderServiceButton.setOnClickListener(new  OnClickListener() 
25         {
26             
27             @Override
28             public void onClick(View v) 
29             {
30             // step 5:call interface from server
31             //bindService     parameter=>_intent
32                 Intent _intent=new  Intent(BinderServiceActivity.this,BinderService.class);
33                 bindService(_intent, conn, BinderService.BIND_AUTO_CREATE);
34             }
35         });
36         
37     }
38     //step 6:bindService     parameter=>conn
39 private ServiceConnection conn=new ServiceConnection() 
40 {
41     
42     @Override
43     public void onServiceDisconnected(ComponentName name) {
44         
45         // TODO Auto-generated method stub
46         
47     }
48     
49     @Override
50     public void onServiceConnected(ComponentName name, IBinder service) {
51         
52         //step 7:receive server method
53         String bindername=((MyBinder)service).helloWorld("LD");
54         Toast.makeText(BinderServiceActivity.this, bindername, 3000).show();
55         Log.i("BinderService", "BinderService==>"+bindername);

      ((MyBinder)service).getService().helloservice();//绑定其他服务
56 } 57 }; 58 59 60 61 62 }

 

取消绑定

客户端

   Button mUnBinderServiceButton;

    //取消绑定
        mUnBinderServiceButton=(Button) findViewById(R.id.btn_UnBinderService);
        mUnBinderServiceButton.setOnClickListener(new  OnClickListener() 
        {
            
            @Override
            public void onClick(View v) 
            {
             unbindService(conn);
                
            }
        });

服务端端

    @Override
    public boolean onUnbind(Intent intent) {
        
        // TODO Auto-generated method stub
        return super.onUnbind(intent);
        
    }

 

posted @ 2016-08-14 16:39  每天进步一点点!  阅读(2161)  评论(0编辑  收藏  举报