Android中BindService方式使用的理解

來源:互聯網
上載者:User

標籤:

Android中BindService方式使用的理解 - 唯一小神 - 部落格園

最近學習了一下Android裡面的Service的應用,在BindService部分小卡了一下,主要是開始沒有徹底理解為什麼要這麼實現。

BindService和Started Service都是Service,有什麼地方不一樣呢:

1. Started Service中使用StartService()方法來進行方法的調用,調用者和服務之間沒有聯絡,即使調用者退出了,服務依然在進行【onCreate()-? >onStartCommand()->startService()->onDestroy()】,注意其中沒有onStart(),主要是被onStartCommand()方法給取代了,onStart方法不推薦使用了。

2. BindService中使用bindService()方法來綁定服務,調用者和綁定者綁在一起,調用者一旦退出服務也就終止了【onCreate()->onBind()->onUnbind()->onDestroy()】。

調用者Activity:

MainAcitvitypackage com.zys.service;

import com.zys.service.BindService.MyBinder;

import android.R.bool;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

publicclass MainActivity extends Activity {
private Button startBtn;
private Button stopBtn;
privateboolean flag;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flag =false;
//設定
startBtn = (Button)this.findViewById(R.id.startBtn);
stopBtn = (Button)this.findViewById(R.id.stopBtn);
startBtn.setOnClickListener(listener);
stopBtn.setOnClickListener(listener);
}

private OnClickListener listener =new OnClickListener() {

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startBtn:
bindService();
break;
case R.id.stopBtn:
unBind();
break;
default:
break;
}
}


};

privatevoid bindService(){
Intent intent =new Intent(MainActivity.this,BindService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

privatevoid unBind(){
if(flag ==true){
unbindService(conn);
flag =false;
}
}

private ServiceConnection conn =new ServiceConnection() {

@Override
publicvoid onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub

}

@Override
publicvoid onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder binder = (MyBinder)service;
BindService bindService = binder.getService();
bindService.MyMethod();
flag =true;
}
};

}

服務BindService

BindServicepackage com.zys.service;

import java.io.FileDescriptor;

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

publicclass BindService extends Service {

privatestaticfinal String TAG ="BindService";

publicvoid MyMethod(){
Log.i(TAG, "BindService-->MyMethod()");
}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return myBinder;
}

publicclass MyBinder extends Binder{

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

private MyBinder myBinder =new MyBinder();
}

?????由於Android 中的Service使用了onBind 的方法去綁定服務,返回一個Ibinder對象進行操作,而我們要擷取具體的Service方法的內容的時候,我們需要Ibinder對象返回具體的Service對象才能操作,所以說具體的Service對象必須首先實現Binder對象,這個樣子的話我們才能利用bindService的方法對Service進行綁定,擷取Binder對象之後擷取具體的Service對象,然後才擷取Service中的方法等等。所以我們需要注意的是bindService的方式去綁定服務擷取的必定是實現了Binder的對象,所以這是我們必須使用Binder的方式去擷取Service的方式而不是直接使用Service的類,這個是Android內部實現所約束的。

方法過程如下:

Intent intent = new Intent(MainActivity.this,BindService.class)->建立了BindService對象->建立了MyBinder對象

->bindService(intent, conn, Context.BIND_AUTO_CREATE);->onBind()函數? -----傳遞MyBinder對象------->onServiceConnected()

?--> 通過傳遞的Binder對象擷取剛剛和Binder對象對應的BindService 對象  -->調用Service中定義的方法。

??? 這個其中必須通過Binder對象,因為是通過Binder對象來傳遞的,通過Binder對象擷取Service對象,然後擷取所需的服務,所以Service必須實現Binder,以便傳遞和使用。

Android中BindService方式使用的理解

聯繫我們

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