Android BindService使用案例講解
最近學(xué)習(xí)了一下Android里面的Service的應(yīng)用,在BindService部分小卡了一下,主要是開始沒有徹底理解為什么要這么實(shí)現(xiàn)。
BindService和Started Service都是Service,有什么地方不一樣呢:
- 1. Started Service中使用StartService()方法來進(jìn)行方法的調(diào)用,調(diào)用者和服務(wù)之間沒有聯(lián)系,即使調(diào)用者退出了,服務(wù)依然在進(jìn)行【onCreate()- >onStartCommand()->startService()->onDestroy()】,注意其中沒有onStart(),主要是被onStartCommand()方法給取代了,onStart方法不推薦使用了。
- 2. BindService中使用bindService()方法來綁定服務(wù),調(diào)用者和綁定者綁在一起,調(diào)用者一旦退出服務(wù)也就終止了【onCreate()->onBind()->onUnbind()->onDestroy()】。
調(diào)用者Activity:
package 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;
public class MainActivity extends Activity {
private Button startBtn;
private Button stopBtn;
private boolean flag;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flag = false;
//設(shè)置
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
public void 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;
}
}
};
private void bindService(){
Intent intent = new Intent(MainActivity.this,BindService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
private void unBind(){
if(flag == true){
unbindService(conn);
flag = false;
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder binder = (MyBinder)service;
BindService bindService = binder.getService();
bindService.MyMethod();
flag = true;
}
};
}
服務(wù)BindService
package 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;
public class BindService extends Service {
private static final String TAG = "BindService";
public void MyMethod(){
Log.i(TAG, "BindService-->MyMethod()");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return myBinder;
}
public class MyBinder extends Binder{
public BindService getService(){
return BindService.this;
}
}
private MyBinder myBinder = new MyBinder();
}
由于Android 中的Service使用了onBind 的方法去綁定服務(wù),返回一個Ibinder對象進(jìn)行操作,而我們要獲取具體的Service方法的內(nèi)容的時候,我們需要Ibinder對象返回具體的Service對象才能操作,所以說具體的Service對象必須首先實(shí)現(xiàn)Binder對象,這個樣子的話我們才能利用bindService的方法對Service進(jìn)行綁定,獲取Binder對象之后獲取具體的Service對象,然后才獲取Service中的方法等等。所以我們需要注意的是bindService的方式去綁定服務(wù)獲取的必定是實(shí)現(xiàn)了Binder的對象,所以這是我們必須使用Binder的方式去獲取Service的方式而不是直接使用Service的類,這個是Android內(nèi)部實(shí)現(xiàn)所約束的。
方法過程如下:
Intent intent = new Intent(MainActivity.this,BindService.class)->新建了BindService對象->新建了MyBinder對象
->bindService(intent, conn, Context.BIND_AUTO_CREATE);->onBind()函數(shù) -----傳遞MyBinder對象------->onServiceConnected()
--> 通過傳遞的Binder對象獲取剛剛和Binder對象對應(yīng)的BindService 對象 -->調(diào)用Service中定義的方法。
這個其中必須通過Binder對象,因?yàn)槭峭ㄟ^Binder對象來傳遞的,通過Binder對象獲取Service對象,然后獲取所需的服務(wù),所以Service必須實(shí)現(xiàn)Binder,以便傳遞和使用。
到此這篇關(guān)于Android BindService使用案例講解的文章就介紹到這了,更多相關(guān)Android BindService使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你五分鐘實(shí)現(xiàn)Android超漂亮的刻度輪播控件實(shí)例教程
說到輪播圖,想必大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于如何利用五分鐘快速實(shí)現(xiàn)一款超漂亮的Android刻度輪播控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-09-09
Android仿新浪微博發(fā)布微博界面設(shè)計(jì)(5)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博發(fā)布微博界面設(shè)計(jì)方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能
這篇文章主要為大家詳細(xì)介紹了SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android編程實(shí)現(xiàn)仿美團(tuán)或淘寶的多級分類菜單效果示例【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿美團(tuán)或淘寶的多級分類菜單效果,結(jié)合實(shí)例形式分析了Android多級菜單的實(shí)現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01
Android調(diào)用OpenCV2.4.10實(shí)現(xiàn)二維碼區(qū)域定位
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用OpenCV 2.4.10實(shí)現(xiàn)二維碼區(qū)域定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android頂部標(biāo)題欄的布局設(shè)計(jì)
大家好,本篇文章主要講的是Android頂部標(biāo)題欄的布局設(shè)計(jì),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Android實(shí)現(xiàn)檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Android SurfaceView基礎(chǔ)用法詳解
這篇文章主要介紹了Android SurfaceView基礎(chǔ)用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

