Android后臺(tái)程序應(yīng)用技巧分享
Android手機(jī)操作系統(tǒng)是由谷歌推出的一款開源的基于Linux平臺(tái)的操作系統(tǒng),深受廣大編程愛好者的喜愛。在Android系統(tǒng)中我們一直在接觸著前臺(tái)界面程序,其實(shí)在一開始接觸Android時(shí)就聽說了,程序就有有界面和無界面之分。#t#
Android后臺(tái)程序就是這類無界面的程序,它在后臺(tái)執(zhí)行,沒有影響你的界面。比如短信監(jiān)聽程序,執(zhí)行在后臺(tái),當(dāng)有短信時(shí)才給你們提示,振動(dòng)或聲音;比如鬧鐘,設(shè)定好時(shí)間后,在定時(shí)通知你;再比如mp3播放器,選擇好音樂后,在待在后臺(tái)唱著,當(dāng)有電話來時(shí),自動(dòng)暫停,完后再繼續(xù)播放。
其實(shí)分析下來,我們不難發(fā)現(xiàn),Android后臺(tái)程序跟前臺(tái)程序是一樣的,也就是在執(zhí)行我們指定的程序,只是留給我們兩個(gè)問題,1.因?yàn)闆]有界面,我們會(huì)問,怎么啟動(dòng),怎么終止?2.因?yàn)闆]有界面,這程序如何通知我們一些信息或狀態(tài)。
前面的學(xué)習(xí)讓我們知道,一個(gè)Activity想Call另一個(gè)Activity時(shí),只需要能過中介人Intent就可以了,同樣我們與服務(wù)處理類打交道也是通過Intent來實(shí)現(xiàn),當(dāng)然,界面類是繼承著Activity,而服務(wù)類則是繼承著Service類。
啟動(dòng)服務(wù):
- // Implicitly start a Service
- startService(new Intent(MyService.MY_ACTION));
- // Explicitly start a Service
- startService(new Intent(this, MyService.class));
停止服務(wù):
- stopService(new Intent(this, MyService.class));
同樣,跟Activity一樣的生命期中,系統(tǒng)也會(huì)自動(dòng)跟據(jù)不同的狀態(tài)來調(diào)用繼承函數(shù):
- @Override
- public void onCreate()
- public IBinder onBind(Intent intent)
- public void onStart(Intent intent, int startId)
- 。。。
在實(shí)際的開發(fā)中,我們一般都不會(huì)直接寫一個(gè)服務(wù)類,一般都會(huì)寫一個(gè)與Android后臺(tái)程序相配套的前臺(tái)程序,一般的程序總會(huì)有一些配置吧~~,然后這個(gè)界面中就可以很方便地來控制后臺(tái)程序的運(yùn)作。
我們來回答第二個(gè)問題,就是在服務(wù)中我們怎么發(fā)起一個(gè)通知給用戶,在Andorid中,提供了以下幾種方式:
1. Toast
這是一個(gè)無模式的小窗體,會(huì)將顯示的信息顯示在首頁面中:
實(shí)現(xiàn)代碼是:
- Context context = getApplicationContext();
- String msg = “To the bride an groom!”;
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(context, msg, duration);
- int offsetX = 0;
- int offsetY = 0;
- toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);
- toast.show();
當(dāng)然,你也可以顯示更雜的,可以將一個(gè)控制直接當(dāng)成一個(gè)Toast顯示出來,也可以自定義一個(gè)控件顯示出來,自定義控件的強(qiáng)大是大家都知道的~~
2. Notifications
這種方式是系統(tǒng)中比較通用的模式,通過這種方式你可以使系統(tǒng):將一個(gè)圖標(biāo)在狀態(tài)條上閃,讓機(jī)器震動(dòng),發(fā)出聲音等。
實(shí)現(xiàn)代碼:
- String svcName = Context.NOTIFICATION_SERVICE;
- NotificationManager notificationManager;
- notificationManager = (NotificationManager)getSystemService(svcName);
- // Choose a drawable to display as the status bar icon
- int icon = R.drawable.icon;
- // Text to display in the status bar when the notification is launched
- String tickerText = “Notification”;
- // The extended status bar orders notification in time order
- long when = System.currentTimeMillis();
- Notification notification = new Notification(icon, tickerText, when);
- Context context = getApplicationContext();
- // Text to display in the extended status window
- String expandedText = “Extended status text”;
- // Title for the expanded status
- String expandedTitle = “Notification Title”;
- // Intent to launch an activity when the extended text is clicked
- Intent intent = new Intent(this, MyActivity.class);
- PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
- notification.setLatestEventInfo(context, expandedTitle,expandedText,launchIntent);
觸發(fā)方式:
- int notificationRef = 1;
- notificationManager.notify(notificationRef, notification);
學(xué)會(huì)了Activity再寫個(gè)Android后臺(tái)程序也就不難了??!
這里順便再提一下,在Android系統(tǒng)中也提供了多線程編程,我們知道不管是前臺(tái)還是后臺(tái)程序,都有生命期的,當(dāng)程序不活動(dòng)時(shí),我們想繼續(xù)讓程序執(zhí)行,這里我們需要用到線程了,在Android系統(tǒng)中使用線程,跟我們直接寫java線程程序非常想似:
- // This method is called on the main GUI thread.
- private void mainProcessing() {
- // 主程序中啟動(dòng)線程.
- Thread thread = new Thread(null, doBackgroundThreadProcessing,
“Background”);- thread.start();
- }
- // Runnable that executes the background processing method.
- private Runnable doBackgroundThreadProcessing = new Runnable() {
- public void run() {
- //線程執(zhí)行內(nèi)容。。。
- }
- };
Android后臺(tái)程序的相關(guān)應(yīng)用就為大家介紹的這里。