国产成人精品a视频一区www_国产区视频在线观看_99色视频_欲色av_亚洲一区电影_亚洲综合视频一区

Android后臺程序應用技巧分享

來源:網絡

點擊:1444

A+ A-

所屬頻道:新聞中心

關鍵詞: Android,后臺程序,應用技巧

      Android手機操作系統是由谷歌推出的一款開源的基于Linux平臺的操作系統,深受廣大編程愛好者的喜愛。在Android系統中我們一直在接觸著前臺界面程序,其實在一開始接觸Android時就聽說了,程序就有有界面和無界面之分。

      Android后臺程序就是這類無界面的程序,它在后臺執行,沒有影響你的界面。比如短信監聽程序,執行在后臺,當有短信時才給你們提示,振動或聲音;比如鬧鐘,設定好時間后,在定時通知你;再比如mp3播放器,選擇好音樂后,在待在后臺唱著,當有電話來時,自動暫停,完后再繼續播放。

      其實分析下來,我們不難發現,Android后臺程序跟前臺程序是一樣的,也就是在執行我們指定的程序,只是留給我們兩個問題,1。因為沒有界面,我們會問,怎么啟動,怎么終止?2。因為沒有界面,這程序如何通知我們一些信息或狀態。

      前面的學習讓我們知道,一個Activity想Call另一個Activity時,只需要能過中介人Intent就可以了,同樣我們與服務處理類打交道也是通過Intent來實現,當然,界面類是繼承著Activity,而服務類則是繼承著Service類。

      啟動服務:

      1.// Implicitly start a Service

      2.startService(new Intent(MyService.MY_ACTION));

      3.// Explicitly start a Service

      4.startService(new Intent(this, MyService.class));

      停止服務:

      1.stopService(new Intent(this, MyService.class));

      同樣,跟Activity一樣的生命期中,系統也會自動跟據不同的狀態來調用繼承函數:

      1.@Override

      2.public void onCreate()

      3.public IBinder onBind(Intent intent)

      4.public void onStart(Intent intent, int startId)

      5.。。。

      在實際的開發中,我們一般都不會直接寫一個服務類,一般都會寫一個與Android后臺程序相配套的前臺程序,一般的程序總會有一些配置吧~~,然后這個界面中就可以很方便地來控制后臺程序的運作。

      我們來回答第二個問題,就是在服務中我們怎么發起一個通知給用戶,在Andorid中,提供了以下幾種方式:

      1. Toast

      這是一個無模式的小窗體,會將顯示的信息顯示在首頁面中:

      實現代碼是:

      1.Context context = getApplicationContext();

      2.String msg = “To the bride an groom!”;

      3.int duration = Toast.LENGTH_SHORT;

      4.Toast toast = Toast.makeText(context, msg, duration);

      5.int offsetX = 0;

      6.int offsetY = 0;

      7.toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);

      8.toast.show();

      當然,你也可以顯示更雜的,可以將一個控制直接當成一個Toast顯示出來,也可以自定義一個控件顯示出來,自定義控件的強大是大家都知道的~~

      2. Notifications

      這種方式是系統中比較通用的模式,通過這種方式你可以使系統:將一個圖標在狀態條上閃,讓機器震動,發出聲音等。

      實現代碼:

      1.String svcName = Context.NOTIFICATION_SERVICE;

      2.NotificationManager notificationManager;

      3.notificationManager = (NotificationManager)getSystemService(svcName);

      4.// Choose a drawable to display as the status bar icon

      5.int icon = R.drawable.icon;

      6.// Text to display in the status bar when the notification is launched

      7.String tickerText = “Notification”;

      8.// The extended status bar orders notification in time order

      9.long when = System.currentTimeMillis();

      10.Notification notification = new Notification(icon, tickerText, when);

      11.Context context = getApplicationContext();

      12.// Text to display in the extended status window

      13.String expandedText = “Extended status text”;

      14.// Title for the expanded status

      15.String expandedTitle = “Notification Title”;

      16.// Intent to launch an activity when the extended text is clicked

      17.Intent intent = new Intent(this, MyActivity.class);

      18.PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);

      19.notification.setLatestEventInfo(context, expandedTitle,expandedText,launchIntent);

      觸發方式:

      1.int notificationRef = 1;

      2.notificationManager.notify(notificationRef, notification);

      學會了Activity再寫個Android后臺程序也就不難了!!

      這里順便再提一下,在Android系統中也提供了多線程編程,我們知道不管是前臺還是后臺程序,都有生命期的,當程序不活動時,我們想繼續讓程序執行,這里我們需要用到線程了,在Android系統中使用線程,跟我們直接寫java線程程序非常想似:

      1.// This method is called on the main GUI thread.

      2.private void mainProcessing() {

      3.// 主程序中啟動線程。

      4.Thread thread = new Thread(null, doBackgroundThreadProcessing,

      “Background”);

      5.thread.start();

      6.}

      7.// Runnable that executes the background processing method.

      8.private Runnable doBackgroundThreadProcessing = new Runnable() {

      9.public void run() {

      10.//線程執行內容。。。

      11.}

      12.};

    (審核編輯: 智匯小新)

    聲明:除特別說明之外,新聞內容及圖片均來自網絡及各大主流媒體。版權歸原作者所有。如認為內容侵權,請聯系我們刪除。

    主站蜘蛛池模板: 中文在线一区二区 | 久久精品国产99国产精品 | 日韩精品在线一区 | 国产一级免费视频 | 国产精品99久久久久久久久 | 久久久久中文字幕 | 日本精品视频在线观看 | 九九九久久国产免费 | 伊人狠狠 | 国产传媒在线视频 | 成人国产精品 | 日韩欧美一区二区三区 | 日韩欧美中文 | 成年人免费看片 | 在线看黄色av | 不卡一区二区三区四区 | 成人av一区 | 国产精品一区二区久久精品爱微奶 | 久久一级 | 一级免费大片 | 在线观看免费视频亚洲 | 福利影视 | 欧美日韩中文字幕在线 | 久久久久久久精 | √新版天堂资源在线资源 | 日韩一级不卡 | 亚洲一级黄色片 | 欧美精品导航 | 欧美高清在线一区 | 精品色区| 福利三区 | 一区在线视频 | 秋霞成人 | 亚洲狠狠久久综合一区77777 | 日韩欧美中文字幕在线观看 | 黄色电影在线免费看 | www.久久| 欧美99 | 99久久久国产精品 | 欧美精品一区二区三区在线播放 | 欧美激情国产日韩精品一区18 |