亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

Android中Notification用法實例總結

2020-04-11 11:22:29
字體:
來源:轉載
供稿:網友

本文實例總結了 Android中Notification用法。分享給大家供大家參考,具體如下:

我們在用手機的時候,如果來了短信,而我們沒有點擊查看的話,是不是在手機的最上邊的狀態欄里有一個短信的小圖標提示???你是不是也想實現這種功能呢?今天的Notification就是解決這個問題的。

我們也知道Android系統也是在不斷升級的,有關Notification的用法也就有很多種,有的方法已經被android拋棄了,現在我實現了三種不同的方法,并適應不同的android版本?,F在我就把代碼公布出來,我喜歡把解釋寫在代碼中,在這里我就不多說了,先看效果圖:

再看代碼,主要的代碼如下:

package net.loonggg.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.RemoteViews; public class MainActivity extends Activity {   private static final int NOTIFICATION_FLAG = 1;   @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);   }   public void notificationMethod(View view) {     // 在Android進行通知處理,首先需要重系統哪里獲得通知管理器NotificationManager,它是一個系統Service。     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);     switch (view.getId()) {     // 默認通知     case R.id.btn1:       // 創建一個PendingIntent,和Intent類似,不同的是由于不是馬上調用,需要在下拉狀態條出發的activity,所以采用的是PendingIntent,即點擊Notification跳轉啟動到哪個Activity       PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,           new Intent(this, MainActivity.class), 0);       // 下面需兼容Android 2.x版本是的處理方式       // Notification notify1 = new Notification(R.drawable.message,       // "TickerText:" + "您有新短消息,請注意查收!", System.currentTimeMillis());       Notification notify1 = new Notification();       notify1.icon = R.drawable.message;       notify1.tickerText = "TickerText:您有新短消息,請注意查收!";       notify1.when = System.currentTimeMillis();       notify1.setLatestEventInfo(this, "Notification Title",           "This is the notification message", pendingIntent);       notify1.number = 1;       notify1.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明當通知被用戶點擊時,通知將被清除。       // 通過通知管理器來發起通知。如果id不同,則每click,在statu那里增加一個提示       manager.notify(NOTIFICATION_FLAG, notify1);       break;     // 默認通知 API11及之后可用     case R.id.btn2:       PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,           new Intent(this, MainActivity.class), 0);       // 通過Notification.Builder來創建通知,注意API Level       // API11之后才支持       Notification notify2 = new Notification.Builder(this)           .setSmallIcon(R.drawable.message) // 設置狀態欄中的小圖片,尺寸一般建議在24×24,這個圖片同樣也是在下拉狀態欄中所顯示,如果在那里需要更換更大的圖片,可以使用setLargeIcon(Bitmap            // icon)           .setTicker("TickerText:" + "您有新短消息,請注意查收!")// 設置在status           // bar上顯示的提示文字           .setContentTitle("Notification Title")// 設置在下拉status           // bar后Activity,本例子中的NotififyMessage的TextView中顯示的標題           .setContentText("This is the notification message")// TextView中顯示的詳細內容           .setContentIntent(pendingIntent2) // 關聯PendingIntent           .setNumber(1) // 在TextView的右方顯示的數字,可放大圖片看,在最右側。這個number同時也起到一個序列號的左右,如果多個觸發多個通知(同一ID),可以指定顯示哪一個。           .getNotification(); // 需要注意build()是在API level       // 16及之后增加的,在API11中可以使用getNotificatin()來代替       notify2.flags |= Notification.FLAG_AUTO_CANCEL;       manager.notify(NOTIFICATION_FLAG, notify2);       break;     // 默認通知 API16及之后可用     case R.id.btn3:       PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,           new Intent(this, MainActivity.class), 0);       // 通過Notification.Builder來創建通知,注意API Level       // API16之后才支持       Notification notify3 = new Notification.Builder(this)           .setSmallIcon(R.drawable.message)           .setTicker("TickerText:" + "您有新短消息,請注意查收!")           .setContentTitle("Notification Title")           .setContentText("This is the notification message")           .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API           // level16及之后增加的,API11可以使用getNotificatin()來替代       notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明當通知被用戶點擊時,通知將被清除。       manager.notify(NOTIFICATION_FLAG, notify3);// 步驟4:通過通知管理器來發起通知。如果id不同,則每click,在status哪里增加一個提示       break;     // 自定義通知     case R.id.btn4:       // Notification myNotify = new Notification(R.drawable.message,       // "自定義通知:您有新短信息了,請注意查收!", System.currentTimeMillis());       Notification myNotify = new Notification();       myNotify.icon = R.drawable.message;       myNotify.tickerText = "TickerText:您有新短消息,請注意查收!";       myNotify.when = System.currentTimeMillis();       myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除       RemoteViews rv = new RemoteViews(getPackageName(),           R.layout.my_notification);       rv.setTextViewText(R.id.text_content, "hello wrold!");       myNotify.contentView = rv;       Intent intent = new Intent(Intent.ACTION_MAIN);       PendingIntent contentIntent = PendingIntent.getActivity(this, 1,           intent, 1);       myNotify.contentIntent = contentIntent;       manager.notify(NOTIFICATION_FLAG, myNotify);       break;     case R.id.btn5:       // 清除id為NOTIFICATION_FLAG的通知       manager.cancel(NOTIFICATION_FLAG);       // 清除所有的通知       // manager.cancelAll();       break;     default:       break;     }   } }

再看主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical"   tools:context=".MainActivity" >   <Button     android:id="@+id/btn1"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:onClick="notificationMethod"     android:text="默認通知(已被拋棄,但是通用)" />   <Button     android:id="@+id/btn2"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:onClick="notificationMethod"     android:text="默認通知(API11之后可用)" />   <Button     android:id="@+id/btn3"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:onClick="notificationMethod"     android:text="默認通知(API16之后可用)" />   <Button     android:id="@+id/btn4"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:onClick="notificationMethod"     android:text="自定義通知" />   <Button     android:id="@+id/btn5"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:onClick="notificationMethod"     android:text="清除通知" /> </LinearLayout>

還有一個是:自定義通知的布局文件my_notification.xml,代碼如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:background="#ffffff"   android:orientation="vertical" >   <TextView     android:id="@+id/text_content"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:textSize="20sp" /> </LinearLayout>

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲精品国产福利| 亚洲国产成人精品女人久久久| 久久人体大胆视频| 久久99久国产精品黄毛片入口| 亚洲午夜国产成人av电影男同| 国产精品女人网站| 亚洲一区av在线播放| 国产一区二区三区直播精品电影| 国产精品日韩欧美| 中文字幕精品视频| 亚洲国产精品视频在线观看| 日韩免费观看视频| 久久国产精品99国产精| 亚洲精品97久久| 久久久最新网址| 国产成人avxxxxx在线看| 久久久久久国产精品三级玉女聊斋| 日韩精品免费一线在线观看| 欧美日韩国产精品一区二区不卡中文| 久热在线中文字幕色999舞| 亚洲精美色品网站| 久久久精品亚洲| 欧美大胆在线视频| 精品国产999| 国产亚洲精品激情久久| 日韩亚洲国产中文字幕| 亚洲欧美国产日韩天堂区| 国产免费一区二区三区在线观看| 欧美精品video| 国产精品96久久久久久又黄又硬| 亚洲最大在线视频| 亚洲欧美日韩一区二区三区在线| 欧美一区深夜视频| 成人黄色免费网站在线观看| 日韩精品有码在线观看| 色多多国产成人永久免费网站| 国产在线观看一区二区三区| 成人444kkkk在线观看| 成人激情视频小说免费下载| 久久精品国产亚洲| 国产亚洲欧洲在线| 国产一区二区三区丝袜| 国产精品视频久久久| 成人午夜在线视频一区| 国产男女猛烈无遮挡91| 国产精品高清在线| 国产精品第1页| 亚洲人成在线电影| 久久久精品亚洲| 国产精品日韩在线| 欧美性高跟鞋xxxxhd| 91po在线观看91精品国产性色| 久久6免费高清热精品| 国产精品久久久91| 高清日韩电视剧大全免费播放在线观看| 欧美精品电影免费在线观看| 国产视频一区在线| 成人h猎奇视频网站| 国产亚洲aⅴaaaaaa毛片| 亚洲欧洲中文天堂| 欧美视频不卡中文| 成人性生交大片免费看小说| 亚洲天堂网站在线观看视频| 国产视频久久网| 亚洲欧美日本另类| 成人a视频在线观看| 国产精品久久久久999| 在线观看国产精品淫| 黑人欧美xxxx| 亚洲国产古装精品网站| 久久久www成人免费精品张筱雨| 伊人伊成久久人综合网站| 国产精品久久久久久久久粉嫩av| 91精品国产高清久久久久久91| 国产精品偷伦免费视频观看的| 国产精品88a∨| 欧美亚洲国产视频| 亚洲国产97在线精品一区| 久久99亚洲热视| 国产视频亚洲精品| 国产精品久久久久久久久久久新郎| 欧美国产欧美亚洲国产日韩mv天天看完整| 国产69久久精品成人| 欧美极品少妇xxxxⅹ裸体艺术| 国产精品视频久久| 欧洲亚洲免费视频| 亚洲成在人线av| 欧美xxxx综合视频| 欧美在线视频免费观看| 日韩中文字幕视频在线| 久久亚洲精品中文字幕冲田杏梨| 国外成人在线播放| 国产精品入口免费视| 九九久久久久久久久激情| 久久婷婷国产麻豆91天堂| 欧美午夜视频一区二区| 国产欧美亚洲视频| 欧美成人国产va精品日本一级| 欧美高清视频在线播放| 亚洲直播在线一区| 正在播放国产一区| 日韩电影中文 亚洲精品乱码| 亚洲成人网av| 国产美女扒开尿口久久久| 亚洲伊人久久大香线蕉av| 日韩有码片在线观看| 日韩av网址在线观看| 亚洲一区二区三区乱码aⅴ| 日韩美女主播视频| 亚洲欧洲在线播放| 欧美福利小视频| 国产精品jizz在线观看麻豆| 成人中文字幕在线观看| 国产精品女人网站| 亚洲黄色片网站| 国模私拍视频一区| 国产噜噜噜噜噜久久久久久久久| 精品亚洲一区二区三区在线观看| 成人在线播放av| 国产一区二区三区在线看| 久久久精品亚洲| 日韩av免费看网站| 欧美中文字幕在线播放| 懂色aⅴ精品一区二区三区蜜月| 国产一区二区成人| 成人精品一区二区三区| 91欧美激情另类亚洲| 日韩av大片免费看| 国产精品爱啪在线线免费观看| 亚洲最大激情中文字幕| 91av视频导航| 国产mv免费观看入口亚洲| 亚洲激情免费观看| www国产精品com| 91久久久久久国产精品| 97在线看免费观看视频在线观看| 国产精品美女久久| 国产亚洲视频中文字幕视频| 91chinesevideo永久地址| 亚洲精品久久久一区二区三区| 亚洲精品99久久久久中文字幕| 综合欧美国产视频二区| 91在线免费观看网站| 国产91精品最新在线播放| 欧美最顶级丰满的aⅴ艳星| 久久久精品2019中文字幕神马| 成人在线视频网站| 中文字幕精品在线视频| 成人免费福利在线| 国产日本欧美在线观看| 777午夜精品福利在线观看| 久久久国产91| 久久免费精品日本久久中文字幕| 国产一区二区日韩精品欧美精品| 欧美午夜精品久久久久久浪潮| 成人免费激情视频| 国产在线98福利播放视频| 91丨九色丨国产在线| 欧美巨大黑人极品精男| 色综合天天综合网国产成人网| 亚洲free性xxxx护士白浆| 91久久在线视频| 国产精品久久二区| 日韩精品一二三四区|