Android入門:綁定本地服務

來源:互聯網
上載者:User

一、綁定服務介紹
前面文章中講過一般的通過startService開啟的服務,當訪問者關閉時,服務仍然存在;但是如果存在這樣一種情況:訪問者需要與服務進行通訊,則我們需要將訪問者與服務進行綁定;如果使用Context.bindService()方法啟動服務,則在服務未建立時,系統會調用服務的onCreate()方法,接著調用onBind()方法,這時就訪問者與服務已經綁定;如果多次調用bindService()多次綁定服務,則不會有副作用(導致多次綁定);綁定服務的運行流程如所示:


二、Bind Service 核心代碼

綁定與解除綁定定服務

(1)Context.bindService(Intent service,ServiceConnection conn,BIND_AUTO_CREATE);//綁定服務(2)Context.unbindService(ServiceConnection conn);
//解除綁定服務

ServiceConnection

ServiceConnection為一個介面,用於綁定和解除綁定定IBinder,因此需要建立一個類實現它;class XxxServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//service為在onBind返回的IBinder
//綁定Binder對象
}
@Override
public void onServiceDisconnected(ComponentName name) {
//解除綁定定Binder對象
}
}


Service類
class XxxService extends Service{private IBinder binder = new
XxxBinder();public IBinder onBind(Intent intent){return binder;}public int fun(int a){
//服務提供的方法,但是不能直接調用...}private class XxxBinder extends Binder implements IXxxBinder{
//面向介面編程public return fun1(int a){
//對外暴露的APIreturn fun(a);
//內部調用Service的方法}}}

三、BindService案例


情境:服務提供了加法運算的介面,Activity需要調用服務提供的加法運算方法,並返回結果;
效果說明:



MainActivity.java
package com.xiazdong.bindservice;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText op1,op2;private Button button;private TextView result;private IAddOp binder ;//使用介面IAddOpprivate ServiceConnection conn = new AddOpServiceConnection();private OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View v) {int number = binder.addOpService(Integer.parseInt(op1.getText().toString()), Integer.parseInt(op1.getText().toString()));result.setText("result="+number+"");}};@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        op1 = (EditText)this.findViewById(R.id.op1);        op2 = (EditText)this.findViewById(R.id.op2);        result = (TextView)this.findViewById(R.id.result);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(listener);        Intent service = new Intent(this,AddOpService.class);        this.bindService(service, conn, BIND_AUTO_CREATE);    }private class AddOpServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//綁定服務時調用binder = (IAddOp)service;}@Overridepublic void onServiceDisconnected(ComponentName name) {//解除綁定定服務時調用binder = null;}}}

AddOpService.java

package com.xiazdong.bindservice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class AddOpService extends Service {private IBinder binder = new AddOpBinder();public int add(int a,int b){//服務提供的方法return a+b;}@Overridepublic IBinder onBind(Intent intent) {return binder;}private class AddOpBinder extends Binder implements IAddOp{public int addOpService(int a, int b) {//binder對外暴露一個API調用服務提供的方法return add(a,b);}}}
IAddOpService.java
package com.xiazdong.bindservice;public interface IAddOp {public int addOpService(int a,int b);}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.