實現Android的訊息通知欄
來源:互聯網
上載者:User
背景知識:可以用Activity和Service來開始訊息通知,兩者的區別在於一個是在前台觸發,一個是後台服務觸發。要使用訊息通知,必須要用到兩個類:NotificationManager和Notification,其他NotificationManager的初始化是用getSystemService方法,並且通過notify方法來向android系統發送訊息列通知和顯示。代碼://訊息通知欄//定義NotificationManagerString ns = Context.NOTIFICATION_SERVICE;NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);//定義通知欄展現的內容資訊int icon = R.drawable.icon;CharSequence tickerText = "我的通知欄標題";long when = System.currentTimeMillis();Notification notification = new Notification(icon, tickerText, when);//定義下拉通知欄時要展現的內容資訊Context context = getApplicationContext();CharSequence contentTitle = "我的通知欄標展開標題";CharSequence contentText = "我的通知欄展開詳細內容";Intent notificationIntent = new Intent(this, BootStartDemo.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent); //用mNotificationManager的notify方法通知使用者產生標題列訊息通知mNotificationManager.notify(1, notification);