Android中Intent,service,broadcast應用淺析(一)
典型的Android應用程式由兩部分構成,一個是在前台運作的Activity和View,一個就是在背景運作的Intent 和Service對象,還有一種是是廣播接收器,BroadCastReceiver,我們通常啟動一個service(服務)對象或者發送一個廣播,都是由Intent 來啟動的.
首先來看下怎麼用Intent來啟動一個服務:
寫了一個小例子,在首頁面上有兩個按鈕,一個點選是啟動服務,一個點選是取消服務,看了界面,再看一下界面,在看一下源代碼的截圖.
<a target="_blank" href="http://blog.51cto.com/attachment/201107/113827791.png"></a>
關于服務需要說明的是:服務中隻有onCreate,onStart,和onStop方法,當第一次啟動服務的時候調用的是onCreate,onStart方法,停止服務時調用onStop方法,完了之後再啟動服務就隻需要調用onStart方法了.
public class Activity01 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button startService = (Button) findViewById(R.id.startBtn);
startService.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(Activity01.this,
BackgroundService.class));
}
});
Button stopService = (Button) findViewById(R.id.stopBtn);
stopService.setOnClickListener(new OnClickListener() {
stopService(new Intent(Activity01.this, BackgroundService.class));
}
}
我現在寫的這個小服務的功能是滿足時間條件後重新整理狀态欄,具體的說,就是啟動服務之後開始計算時間,當時間過了一定的時間點之後就重新整理狀态欄,因為之前要在程式中做這一塊,就寫了這樣的一個小例子.
先看代碼中onCreate方法,聲明了一個通知欄管理的對象,然後用了一個handler,這個handler收到message之後靜态變量seconds加一,然後更新狀态欄。
@Override
public void onCreate() {
super.onCreate();
notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
seconds++;
updateNotification(seconds);
break;
}
super.handleMessage(msg);
};
timer = new Timer(false);
但是handler是怎樣收到message的呢?這是我們在onStart中建立了一個timer的對象,可以看代碼,onStart方法中向通知欄發送一個消息,說明已經啟動服務,然後調用run方法,每個一秒鐘向handler發送一個消息.handler接收到消息後執行的代碼已經在onCreate中說過
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
displayNotificationMessage("starting Background Service");
timer.schedule(new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}, 1000, 1000);
當我去停止服務的時候,向通知欄中發動一個消息,說明停止服務,然後重新置标記服務已經開始了多長時間的變量為0,同時用timer.cancel()方法來停止timer的運作。
public void onDestroy() {
super.onDestroy();
displayNotificationMessage("stopping Background Service");
seconds = 0;
timer.cancel();
}
向狀态欄發送資訊的方法,需要說明的就是這個PendingIntent,剛開始的時候很不了解這個東西,後來終于搞明白了,Intent是發送出去一個任務,我們向狀态欄中發送一個消息,狀态欄下拉點選的時候我們要讓他有什麼樣的反應,就是需要用PendingIntent來定義,相當于PendingIntent定義的是這個操作被觸發的時候需要的訓示操作,通俗了解,Intent相當于你跟别人發送一個直接指令,說讓别人直接做什麼.PendingIntent相當于你給别人一個指令,指令中告訴他當有緊急事件發生的時候做什麼,總之有點難了解了.
private void displayNotificationMessage(String message) {
Notification notification = new Notification(R.drawable.icon, message,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Activity01.class), 0);
notification.setLatestEventInfo(this, "Background Service", message,
contentIntent);
notificationMgr.notify(R.id.app_notification_id, notification);
}
關于廣播和服務還有Intent更加詳細的說明還有後續.
本文轉自HDDevTeam 51CTO部落格,原文連結:http://blog.51cto.com/hddev/622627,如需轉載請自行聯系原作者