首页 > 代码库 > android 一个简单的服务例子

android 一个简单的服务例子

  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 一个简单的服务例子