Android BinderService 過程

來源:互聯網
上載者:User

標籤:

步驟:建立伺服器端服務,暴露介面

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 mBinder20 21   22 //step:2 difine MyBinder and extends23   class MyBinder  extends Binder24   {25      
  //調用其他的服務 

       public BinderService getService()
       {
        returnBinderService.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 interface33     @Override34     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     @Override20     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             @Override28             public void onClick(View v) 29             {30             // step 5:call interface from server31             //bindService     parameter=>_intent32                 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=>conn39 private ServiceConnection conn=new ServiceConnection() 40 {41     42     @Override43     public void onServiceDisconnected(ComponentName name) {44         45         // TODO Auto-generated method stub46         47     }48     49     @Override50     public void onServiceConnected(ComponentName name, IBinder service) {51         52         //step 7:receive server method53         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);            }

 

Android BinderService 過程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.