大略地看了一下android.app下的Service類,覺得它與Activity非常相似,只是要注意幾個地方:
1.生命週期,Service的從onCreate()->onStart(int,Bundle)->onDestroy()顯得更為簡單。但是它的onStart是帶參數的,第一個ID可用來標識這個service,第二個參數顯示是用來傳遞資料的了。比較Activity,傳遞資料的Bundle是在onCreate就帶進入的。
2.Service的啟動由Context.startService開始,其實Activity或者Service都是Context的衍生類別。結束於Context.stopService()或者它自己的stopSelf()。
3.Service還有一個與Activity不一樣的是它可以由另一個Context去綁定一個已存在的Service。就是這個方法Context.bindService(),被綁定的Service要求是已經onCreate了但可以沒有onStart。在Service類中有個抽象方法getBinder()可以得到這個IBinder對象。關於這方面的細節,以後再看,這裡只做個記錄罷。
4.與Service有關的還有一個安全的問題,可以在AndroidManifest.xml中用<uses-permission>標籤來聲明一個Service的存取權限,關於Android的安全問題也留待以後再解決吧。
我一直相信一種水到渠成的學習方法,先從最簡單的東西入手,就不會覺得學習很枯燥了。
下面來做個例子。
修改AndroidManifest.xml檔案,增加一個Activity和一個Service:
<activity class=".HelloTwoD" android:label="hello_two_d">
</activity>
<service class=".HelloTwoDService" />
HelloTwoD.java的代碼比較簡單,如下:
public class HelloTwoD extends Activity implements OnClickListener
...{
public HelloTwoD()
...{
super();
}
public void onCreate(Bundle icicle) ...{
super.onCreate(icicle);
setTheme(android.R.style.Theme_Dark);
setContentView(R.layout.maind);
Button btn = (Button)findViewById(R.id.btnTest);
btn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) ...{
// 用一個顯式的Intent來啟動服務
Intent i = new Intent();
i.setClass(this, HelloTwoDService.class);
//帶上我的名字
Bundle b= new Bundle();
b.putString("name", "sharetop");
this.startService(i,b);
}
}
當然要啟動這個HelloTwoD,也需要在我最初的那個HelloTwo中加一點代碼(我就不羅嗦了)。再看看那個HelloTwoDService是如何?的:
public class HelloTwoDService extends Service ...{
public Timer timer;
public final String TAG="HelloTwoDService_TAG";
public void onCreate() ...{
super.onCreate();
Log.d(TAG,"onCreate");
timer = new Timer(true);
}
@Override
public IBinder getBinder() ...{
// TODO Auto-generated method stub
return null;
}
public void onStart(int startId, Bundle arg)
...{
//看看startId是什麼內容
if(arg!=null)
Log.d(TAG,"onStart "+Integer.valueOf(startId).toString()+" from "+arg.getString("name"));
else
Log.d(TAG,"onStart with null Bundle");
timer.schedule(new TimerTask()...{
public void run()...{
//表示一下我的存在
Log.d(TAG,"say from a timer.");
//停掉自己這個服務
HelloTwoDService.this.stopSelf();
}
},5000);
}
public void onDestroy()
...{
Log.d(TAG,"onDestroy");
}
}
這裡我用一個定時器timer來延時5秒鐘顯示訊息,否則立即就顯示出來覺得不象一個後台服務了。用日誌輸出那個onStart中的startId看看,原來只是一個標識而已。
下面來個簡單的NotificationManager吧,看了看API文檔,覺得最簡單地恐怕就是那個NotificationManager.notifyWithText()了,修改上面的run方法如下:
timer.schedule(new TimerTask()...{
public void run()...{
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notifyWithText(1001, "わたしはSHARETOPです。", NotificationManager.LENGTH_LONG, null);
HelloTwoDService.this.stopSelf();
}
},5000);
再試試看效果。太簡單了,Notification主要是用於後台服務用來通知前台,所以,Android提供了三類不同的通知方式,notifyWithText可以簡單地顯示一個字串,而notifyWithView稍複雜點,可以有一個view來構造這個顯示資訊框,而最靈活的就是那個notify(int id, Notification notification)了,參數notification是類Notification的執行個體。
修改一下剛才的那個run方法,如下:
timer.schedule(new TimerTask()...{
public void run()...{
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification nf = new Notification(R.drawable.icon,"這是資訊的詳細描述",null,"資訊的標題",null);
manager.notify(0,nf);
HelloTwoDService.this.stopSelf();
}
},5000);
這裡建立一個Notification的執行個體nf,建構函式的第一個參數是那個顯示在狀態列(也就是Android手機上面的那一條顯示訊號強度、電池電量等資訊的位置)的表徵圖。後面可以有
一個標題和點擊以後的詳細資料,這是字串形式,還可以有一個Intent用來表示點擊後可以發生一個跳轉行為。
OK,今天到此為止。下次該體驗各種View了。