android 一個簡單的服務例子

來源:互聯網
上載者:User

標籤:

  1 public class MessageService extends Service {  2   3     // 擷取訊息線程  4     private MessageThread messageThread = null;  5   6     // 點擊查看  7     private Intent messageIntent = null;  8     private PendingIntent messagePendingIntent = null;  9  10     // 通知欄訊息 11     private int messageNotificationID = 1000; 12     private Notification messageNotification = null; 13     private NotificationManager messageNotificatioManager = null; 14     private final IBinder binder = new MessageService.LocalBinder(); 15  16     @Override 17     public IBinder onBind(final Intent intent) { 18         return binder; 19     } 20  21     // 定義內容類別繼承Binder 22     class LocalBinder extends Binder { 23         // 返回本地服務 24         // 可以返回這個服務,然後<bold>activity可以通過服務調用服務的方法</bold>了。 25         MessageService getService() { 26             return MessageService.this; 27         } 28  29     } 30  31     @Override 32     public int onStartCommand(final Intent intent, final int flags, 33             final int startId) { 34         // 初始化 35         messageNotification = new Notification(); 36         messageNotification.icon = R.drawable.ic_launcher; 37         messageNotification.tickerText = "新訊息"; 38         messageNotification.defaults = Notification.DEFAULT_SOUND; 39         messageNotificatioManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 40  41         messageIntent = new Intent(this, MessageActivity.class); 42         messagePendingIntent = PendingIntent.getActivity(this, 0, 43                 messageIntent, 0); 44  45         // 開啟線程 46         messageThread = new MessageThread(); 47         messageThread.isRunning = true; 48         messageThread.start(); 49  50         return super.onStartCommand(intent, flags, startId); 51     } 52  53     /** 54      * 從伺服器端擷取訊息 55      *  56      */ 57     class MessageThread extends Thread { 58         // 運行狀態,下一步驟有大用 59         public boolean isRunning = true; 60  61         @Override 62         public void run() { 63             while (isRunning) { 64                 try { 65                     // 休息15s 66                     Thread.sleep(15 * 1000); 67                     // 擷取伺服器訊息 68                     String serverMessage = getServerMessage(); 69                     if (serverMessage != null && !"".equals(serverMessage)) { 70                         // 更新通知欄 71                         messageNotification.setLatestEventInfo( 72                                 MessageService.this, "新訊息", "奧巴馬宣布,本拉登兄弟掛了!" 73                                         + serverMessage, messagePendingIntent); 74                         messageNotificatioManager.notify(messageNotificationID, 75                                 messageNotification); 76                         // 每次通知完,通知ID遞增一下,避免訊息覆蓋掉 77                         messageNotificationID++; 78                     } 79                 } catch (InterruptedException e) { 80                     e.printStackTrace(); 81                 } 82             } 83         } 84     } 85  86     /** 87      * 這裡以此方法為伺服器Demo,僅作樣本 88      *  89      * @return 返回伺服器要推送的訊息,否則如果為空白 的話,不推送 90      */ 91     public String getServerMessage() { 92         return "YES!"; 93     } 94  95     @Override 96     public void onDestroy() { 97         System.exit(0); 98         // 或者,二選一,推薦使用System.exit(0),這樣進程退出的更乾淨 99         // messageThread.isRunning = false;100         super.onDestroy();101     }102 }

from:源自

 

android 一個簡單的服務例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.