android service(本地) 知識點

service想必在android開發的過程中基本上都使用。需要一些後臺的任務處理,都是通過服務實現的。比如,通過在service中與服務器保持心跳連接。

今天將service的一些知識點總結一下,分一下幾個方面。

1) service的創建方式。

2) service和thread之間的關係。

3) 前臺service。


一、service創建方式。

service創建方式分爲兩種,(startService/stopService)和(bindService/unbindService)。

a. startService/stopService

通過這種方式啓動service以後,調用者和服務之間就沒關係了。所有通過這種方式啓動的話,不能實現service和調用者之間的通信。

startService 會經過onCreate onstart 方法,多次startService只會執行onstart方法。


b. bindService/unbindService

通過這種方式就能實現Activity和service之間的通信。當activity調用bindService時,就能獲取到IBander對象,在service裏邊實現IBander藉口,

在activity拿到IBander對象以後,就能調用service裏邊的方法了。通過這種方式,啓動service時,service和activity是一種"不求同生,但求同死"的關係。

MyService代碼

public class MyService extends Service {

	private MyBinder binder = new MyBinder();
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return binder;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		System.out.println("oncreate");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		System.out.println("onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("onDestroy");
		super.onDestroy();
	}
	
	public class MyBinder extends Binder{
		public void startDownload(){
			System.out.println("start download");
		}
	}
在MyService裏邊添加了內部類MyBinder,繼承Binder,是因爲Binder實現IBinder接口。並且在方法onbind()中返回MyBinder對象。


創建ServiceConnection對象代碼

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;
			binder.startDownload();
		}
	};

bindService代碼邏輯

Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, conn, BIND_AUTO_CREATE); // 當服務不在時,自動創建


通過bindService以後,就會執行Service中onBind()方法,在onServiceConnected中就能獲取到onBind()方法返回的IBander對象了。再通過強制類型轉換,

就能拿到MyBinder對象,因此我們就能調用服務裏邊的方法了,從而實現Activity和Service之間的通信。

通過bindService創建的服務,需要通過unbindService解除綁定,再進行銷燬。


c. 兩者之間的關係

通過startService以後,說明服務已經處於開啓狀態,然後再進行bindService的話,說明調用者和service之間已經產生關聯。如果想銷燬Service的話,單獨執行stopService

或者unbindService是不夠的。需要先調用stopService,將服務置爲停止狀態,再調用unbindService解除關係,這時,服務才能銷燬。


二、service和thread之間的關係

我們常說在service裏邊處理後臺任務,再線程裏邊處理一些耗時操作,防止界面出現ANR異常。那是不是意味着在線程裏邊乾的事,都可以放在service中呢。

其實不是,它們兩者之間就沒什麼關係,再說service運行在主線程,如果運行一些耗時邏輯,同樣出現ANR異常。OK。那我就解釋一下"在service裏邊做後臺處理邏輯"

含義吧。

1. service跟acitivty沒有關係,就算一個程序,所有的Activity都關閉了,只要進程不退出,service依然在運行。我們說的後臺是指,所有的activity都關了,程序也關了,但是進程還在,service就還在。所以,我們一些需要長期運行的邏輯就放在service中。


2. 既然service運行在主線程中,耗時操作還得創建子線程去處理。那爲啥還得使用service呢。還不如直接在activity中創建線程去處理不就結了。其實不然。

如果在activity中使用子線程去處理一些邏輯的話,當activity被回收,線程也會被回收,另外在其他的activity中很難使用在你的activity中線程處理邏輯,還有再次使用

activity中的線程處理邏輯時,很難恢復到上一次的狀態。但如果使用service就簡單多了,只要服務啓動,所以activity綁定到service獲得的是同一個IBander對象,就

算某一個activity被回收,也不會影響其他的activity。使用service能夠帶來很多的便利。


三、前臺service

將service設置成前臺service,主要有兩個方面的好處。

1. 設置成前臺service,不容易被系統回收。

2. 能夠在系統通知欄裏邊顯示一些信息。比如360、墨跡天氣。


設置的方法爲:

public void onCreate() {
<span style="white-space:pre">	</span>System.out.println("oncreate");
	super.onCreate();
		
	Notification notification = new Notification(R.drawable.ic_launcher,  
                "通知到來", System.currentTimeMillis());  
        Intent notificationIntent = new Intent(this, MainActivity.class);  
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  
                notificationIntent, 0);  
        notification.setLatestEventInfo(this, "通知標題", "這是通知內容",  
                pendingIntent);  
        startForeground(1, notification);  
}


設置以後,界面如圖:


只要服務啓動,在通知欄裏邊就能一直顯示。







發佈了42 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章