Android中 service組件詳解
service組件跟activity組件及其類似,可以說service是沒有界面的activity,
當然service的生命周期和activity還是有一定的差別的。
service組件一般用在什么地方的,上面講了service組件沒有界面,不用跟用戶直接交互,
所以service組件一般運行在后臺。比如做一些不需要界面的數據處理等等。
開發(fā)service需要兩個步驟:
1,定義一個基礎service的子類。
2,在AndroidManifest.xml 文件中配置該service。
怎么啟動service呢,想想啟動activity是不是有兩種方法:
startActivity(intent),
startActivityForResult(intent)
那么啟動service也有兩種方法:
startService(intent),
bindService(Intent service,ServiceConnection conn,int flags),
兩者有什么區(qū)別可以先看下面的代碼:
public class BindService extends Service
{
private int count;
private boolean quit;
// 定義onBinder方法所返回的對象
private MyBinder binder = new MyBinder();
// 通過繼承Binder來實現(xiàn)IBinder類
public class MyBinder extends Binder
{
public int getCount()
{
// 獲取Service的運行狀態(tài):count
return count;
}
}
// 必須實現(xiàn)的方法
@Override
public IBinder onBind(Intent intent)
{
System.out.println("Service is Binded");
// 返回IBinder對象
return binder;
}
// Service被創(chuàng)建時回調該方法。
@Override
public void onCreate()
{
super.onCreate();
System.out.println("Service is Created");
// 啟動一條線程、動態(tài)地修改count狀態(tài)值
new Thread()
{
@Override
public void run()
{
while (!quit)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
}
count++;
}
}
}.start();
}
// Service被斷開連接時回調該方法
@Override
public boolean onUnbind(Intent intent)
{
System.out.println("Service is Unbinded");
return true;
}
// Service被關閉之前回調。
@Override
public void onDestroy()
{
super.onDestroy();
this.quit = true;
System.out.println("Service is Destroyed");
}
@Override
public void onRebind(Intent intent)
{
super.onRebind(intent);
this.quit = true;
System.out.println("Service is ReBinded");
}
}
上面的Service的作用是 簡單的開啟一個線程,每 1秒鐘 count++,這個count數據
通過 binder對象 傳遞給 訪問者。
待會再做詳解,先看下面的代碼怎么啟動Service,并得到 Service的 count數據
public class MainActivity extends Activity
{
Button startService_bnt , bindService_bnt;
// 保持所啟動的Service的IBinder對象
BindService.MyBinder binder;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService_bnt = (Button) findViewById(R.id.start_bnt);
bindService_bnt = (Button) findViewById(R.id.bind_bnt);
//創(chuàng)建啟動Service的Intent
Intent intent = new Intent(this,BindService.class);
startService_bnt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
//綁定指定Serivce
startService(intent);
}
});
bindService_bnt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
//綁定指定Serivce
bindService(intent , conn , Service.BIND_AUTO_CREATE);
Toast.makeText(MainActivity.this
, "Serivce的count值為:" + binder.getCount()
, 4000)
.show();
}
});
}
// 定義一個ServiceConnection對象
private ServiceConnection conn = new ServiceConnection()
{
// 當該Activity與Service連接成功時回調該方法
@Override
public void onServiceConnected(ComponentName name
, IBinder service)
{
System.out.println("--Service Connected--");
// 獲取Service的onBind方法所返回的MyBinder對象
binder = (BindService.MyBinder) service;
}
// 當該Activity與Service斷開連接時回調該方法
@Override
public void onServiceDisconnected(ComponentName name)
{
System.out.println("--Service Disconnected--");
}
};
}
上面activity定義了兩個按鈕,點擊兩個按鈕有兩種不同的方法啟動Service:
startService(intent), bindService(Intent service,ServiceConnection conn,int flags),
現(xiàn)在來講解一下兩種啟動方式的區(qū)別,并解釋上面的代碼。
startService(intent)啟動Service呢它不具有與訪問者交互的能力,就像activity 的 startActivity(),它不能從新啟動的activity拿到返回數據一樣
而bindService(Intent service,ServiceConnection conn,int flags),就不一樣了
訪問者能從啟動的Service 拿到數據,怎么拿到的呢,bindService的第二個參數 conn,該參數是一個 ServiceConnection 對象,當訪問者與Service連接成功 就會回調ServiceConnection 的 onServiceConnected() 方法 ,上面的程序就是在這個回調方法里面拿到 IBinder 對象的。
可以在看一下
// 定義一個ServiceConnection對象
private ServiceConnection conn = new ServiceConnection()
{
// 當該Activity與Service連接成功時回調該方法
@Override
public void onServiceConnected(ComponentName name
, IBinder service)
{
System.out.println("--Service Connected--");
// 獲取Service的onBind方法所返回的MyBinder對象
binder = (BindService.MyBinder) service;
}
// 當該Activity與Service斷開連接時回調該方法
@Override
public void onServiceDisconnected(ComponentName name)
{
System.out.println("--Service Disconnected--");
}
};
簡單點也就是說 訪問者通過 bindService 綁定到 Service,綁定成功后會回調ServiceConnection 中的 onServiceConnected()方法,這個方法里面有IBinder service 參數,這個參數就是 Service暴露給 訪問者的對象,訪問者拿到這個對象就可以訪問 Service的數據了
這就是 訪問者與Service數據交互的原理,是通過 IBinder 對象來傳遞的。
可能到這這里你還對 binder = (BindService.MyBinder) service;這句代碼不理解。
你肯能覺得 拿到的IBinder 對象不應該是上面Service代碼中onBind 方法返回的 binder 才是嘛,怎么 強轉成 BindService.MyBinder 對象了。
而且返回的 binder 也沒 count數據,訪問者怎么就能 binder.getCount() 得到數據呢。
@Override
public IBinder onBind(Intent intent)
{
System.out.println("Service is Binded");
// 返回IBinder對象
return binder;
}
別忘了 上面Service代碼里面還對 IBinder 對象進行處理
// 通過繼承Binder來實現(xiàn)IBinder類
public class MyBinder extends Binder
{
public int getCount()
{
// 獲取Service的運行狀態(tài):count
return count;
}
}
Binder 是 IBinder 的 實現(xiàn)類,MyBinder 繼承Binder 并在里面定義了個方法。
那么 拿到 IBinder 對象 就相當于 拿到 MyBinder 對象,就可以訪問 getCount方法了,這也是 為什么 binder = (BindService.MyBinder) service; 進行強轉,并且binder.getCount() 可以拿到 count 數據,因為 IBinder 里面并沒有業(yè)務實現(xiàn),是MyBinder 幫它實現(xiàn)了。
相關文章
Android ListView下拉刷新上拉自動加載更多DEMO示例
這篇文章主要介紹了Android ListView下拉刷新上拉自動加載更多DEMO示例的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Android自定義ViewGroup實現(xiàn)可滾動的橫向布局(2)
這篇文章主要介紹了Android自定義ViewGroup實現(xiàn)可滾動的橫向布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android實現(xiàn)九宮格(GridView中各項平分空間)的方法
這篇文章主要介紹了Android實現(xiàn)九宮格(GridView中各項平分空間)的方法,涉及Android針對GridView操作的相關技巧,需要的朋友可以參考下2015-06-06
Android中Glide加載到RelativeLayout背景圖方法示例
Glide框架大家應該都很熟悉,我們可以使用Glide加載網絡圖片、加載gif圖片,使用簡單。下面這篇文章主要給大家介紹了關于Android中Glide加載到RelativeLayout背景圖的相關資料,需要的朋友可以參考下。2017-12-12

