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

首頁 > 學院 > 開發設計 > 正文

Notification的基本使用

2019-11-09 14:44:25
字體:
來源:轉載
供稿:網友
        // 在Android進行通知處理,首先需要重系統哪里獲得通知管理器NotificationManager,它是一個系統Service。

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

以一個為例

  Notification notify3 = new Notification.Builder(this)                        .setSmallIcon(R.drawable.ic_launcher)  //icon的圖片設置                        .setTicker("TickerText:" + "您有新短消息,請注意查收!")//提醒的類容                        .setContentTitle("Notification Title")//提醒的標題                        .setContentText("This is the notification message")/提醒類容                        .setContentIntent(pendingIntent3).setNumber(1).build();

PendingIntent//跳轉界面  傳遞信息用的

示例

 intent1 = new Intent(getapplicationContext(),                        ContentActivity.class);                intent1.putExtra("content", "444");                intent1.putExtra("number", "444");                intent1.putExtra("date", "444");         PendingIntent        contentIntent = PendingIntent.getActivity(this, 1,                   intent1, PendingIntent.FLAG_CANCEL_CURRENT);

PendingIntent有一個getActivity方法,第一個參數是上下文,第二個參數 requestCode,第三個參數是 Intent,用來存儲信息,第四個參數是對參數的操作標識,常用的就是FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT。

關于第二個參數當requestCode值一樣時,后面的就會對之前的消息起作用,所以為了避免影響之前的消息,requestCode每次要設置不同的內容。

myNotify = new Notification();

myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除                myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈                myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失

  //DEFAULT_ALL     使用所有默認值,比如聲音,震動,閃屏等等  //DEFAULT_LIGHTS  使用默認閃光提示  //DEFAULT_SOUNDS  使用默認提示聲音   //DEFAULT_VIBRATE 使用默認手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />權限  myNotify.defaults = Notification.DEFAULT_LIGHTS; //閃光和震動需要添加權限
<!-- 閃光燈權限 --><uses-permission android:name="android.permission.FlashLIGHT"><!-- 振動器權限 --><uses-permission android:name="android.permission.VIBRATE"></uses-permission></uses-permission>
notifyBuilder.setPRogress(100, incr, false);這個設置滾動條

可以寫帶滾動條的提示

/** * 帶滾動條的提示 */public void Pro_Notification() {    manager_n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);     notifyBuilder = new NotificationCompat.Builder(this);    notifyBuilder.setContentTitle("Picture Download")            .setContentText("Download in progress")            .setTicker("TickerText:您有新下載消息,請注意查收!")            .setOngoing(true)            .setSmallIcon(R.drawable.ic_launcher);    // Start a lengthy Operation in a background thread    new Thread(            new Runnable() {                @Override                public void run() {                    int incr;                    // Do the "lengthy" operation 20 times                    for (incr = 0; incr <= 100; incr += 5) {                        // Sets the progress indicator to a max value, the                        // current completion percentage, and "determinate"                        // state                        notifyBuilder.setProgress(100, incr, false);                        // Displays the progress bar for the first time.                        manager_n.notify(0, notifyBuilder.build());                        // Sleeps the thread, simulating an operation                        // that takes time                        try {                            // Sleep for 5 seconds                            Thread.sleep( 1000);                        } catch (InterruptedException e) {                            Log.d("NOTIFICATION", "sleep failure");                        }                    }                    // When the loop is finished, updates the notification                    notifyBuilder.setContentText("Download complete")                            // Removes the progress bar                            .setProgress(0, 0, false);                    manager_n.notify(213, notifyBuilder.build());                }            }            // Starts the thread by calling the run() method in its Runnable    ).start();}

   manager.notify(NOTIFICATION_FLAG, notify2);中的NOTIFICATION_FLAG是int類型是提示信息的表示重復不會彈出顯示

貼出全部代碼有的

package com.yundong.pushmessage;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.support.v7.app.AppCompatActivity;import android.support.v7.app.NotificationCompat;import android.util.Log;import android.view.View;import android.widget.RemoteViews;public class MainActivity extends AppCompatActivity {    private static final int NOTIFICATION_FLAG = 1;    private static final int NOTIFICATION_FLAG_4 = 4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void notificationMethod(View view) {        Notification myNotify;        RemoteViews rv;        Intent intent1;        PendingIntent contentIntent;        // 在Android進行通知處理,首先需要重系統哪里獲得通知管理器NotificationManager,它是一個系統Service。        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        switch (view.getId()) {            // 默認通知            case R.id.btn1:                // Notification myNotify = new Notification(R.drawable.message,                // "自定義通知:您有新短信息了,請注意查收!", System.currentTimeMillis());                myNotify = new Notification();                myNotify.icon = R.drawable.ic_launcher;                myNotify.tickerText = "TickerText:您有新短消息,請注意查收!";                myNotify.when = System.currentTimeMillis();                myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除                myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈                myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失                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);               intent1 = new Intent(getApplicationContext(),                        ContentActivity.class);                intent1.putExtra("content", "111");                intent1.putExtra("number", "111");                intent1.putExtra("date", "111");               contentIntent = PendingIntent.getActivity(this, 1,                        intent1, PendingIntent.FLAG_CANCEL_CURRENT);                myNotify.contentIntent = contentIntent;                manager.notify(NOTIFICATION_FLAG, myNotify);                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.ic_launcher) // 設置狀態欄中的小圖片,尺寸一般建議在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.ic_launcher)                        .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());                 myNotify = new Notification();                myNotify.icon = R.drawable.ic_launcher;                myNotify.tickerText = "TickerText:您有新短消息,請注意查收!";                myNotify.when = System.currentTimeMillis();                myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能夠自動清除                myNotify.defaults = Notification.DEFAULT_ALL; // 使用默認設置,比如鈴聲、震動、閃燈                myNotify.flags = Notification.FLAG_AUTO_CANCEL; // 但用戶點擊消息后,消息自動在通知欄自動消失                 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);                intent1 = new Intent(getApplicationContext(),                        ContentActivity.class);                intent1.putExtra("content", "444");                intent1.putExtra("number", "444");                intent1.putExtra("date", "444");                 contentIntent = PendingIntent.getActivity(this, 1,                        intent1, PendingIntent.FLAG_CANCEL_CURRENT);                myNotify.contentIntent = contentIntent;                //第一個參數保證信息不一樣的                manager.notify(NOTIFICATION_FLAG_4, myNotify);                break;            case R.id.btn5:                // 清除id為NOTIFICATION_FLAG的通知//                manager.cancel(NOTIFICATION_FLAG);//                manager.cancel(NOTIFICATION_FLAG_4);                // 清除所有的通知                 manager.cancelAll();                break;            case R.id.btn6:                Pro_Notification();                break;            default:                /** * 有進度條的notification * @param view */                Pro_Notification();                break;        }    }    NotificationCompat.Builder notifyBuilder;    NotificationManager manager_n;    /**     * 帶滾動條的提示     */    public void Pro_Notification() {        manager_n = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);         notifyBuilder = new NotificationCompat.Builder(this);        notifyBuilder.setContentTitle("Picture Download")                .setContentText("Download in progress")                .setTicker("TickerText:您有新下載消息,請注意查收!")                .setOngoing(true)                .setSmallIcon(R.drawable.ic_launcher);        // Start a lengthy operation in a background thread        new Thread(                new Runnable() {                    @Override                    public void run() {                        int incr;                        // Do the "lengthy" operation 20 times                        for (incr = 0; incr <= 100; incr += 5) {                            // Sets the progress indicator to a max value, the                            // current completion percentage, and "determinate"                            // state                            notifyBuilder.setProgress(100, incr, false);                            // Displays the progress bar for the first time.                            manager_n.notify(0, notifyBuilder.build());                            // Sleeps the thread, simulating an operation                            // that takes time                            try {                                // Sleep for 5 seconds                                Thread.sleep( 1000);                            } catch (InterruptedException e) {                                Log.d("NOTIFICATION", "sleep failure");                            }                        }                        // When the loop is finished, updates the notification                        notifyBuilder.setContentText("Download complete")                                // Removes the progress bar                                .setProgress(0, 0, false);                        manager_n.notify(213, notifyBuilder.build());                    }                }                // Starts the thread by calling the run() method in its Runnable        ).start();    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
97国产在线观看| 欧美激情按摩在线| 7m第一福利500精品视频| 国产精品视频白浆免费视频| 中文字幕国内精品| 久久精品这里热有精品| 亚洲欧洲一区二区三区久久| 精品久久久久久中文字幕大豆网| 38少妇精品导航| 欧美精品在线极品| 久久九九全国免费精品观看| 精品偷拍一区二区三区在线看| 久久激情五月丁香伊人| 久久久欧美一区二区| 亚洲精品美女在线观看| 欧美日韩一区二区三区| 国产精品视频在线播放| 国产精品白嫩初高中害羞小美女| 亚洲国产欧美日韩精品| 久久久成人精品| 中文字幕av一区二区| 最新国产精品拍自在线播放| 亚洲欧美日本另类| 国产精品福利在线观看网址| 91精品久久久久久久久久入口| 国产成人一区二区三区| 亚洲美女精品久久| 日韩精品在线观看一区| 中文字幕综合一区| 欧美午夜视频在线观看| 日韩中文字幕第一页| 亚洲精品久久久久久久久久久久久| 欧美激情视频一区二区三区不卡| 久久九九免费视频| 伊是香蕉大人久久| 久久精品视频中文字幕| 国产福利视频一区| 国产一区二区久久精品| 亚洲国产另类 国产精品国产免费| 久久精品国产亚洲一区二区| 成人情趣片在线观看免费| 欧美激情小视频| 日本电影亚洲天堂| 国产精品揄拍500视频| 在线观看欧美成人| 久久精品电影一区二区| 国产日韩av高清| 粉嫩av一区二区三区免费野| 亚洲激情在线观看| 日韩中文综合网| 国模精品系列视频| 欧美日韩激情小视频| 自拍偷拍亚洲区| 欧美色视频日本高清在线观看| 中文字幕视频一区二区在线有码| 国自产精品手机在线观看视频| 91精品在线播放| 欧美激情网站在线观看| 国产精品女视频| 国产午夜精品理论片a级探花| 午夜精品理论片| 91高潮在线观看| 影音先锋欧美在线资源| 亚洲中国色老太| 成人午夜激情免费视频| 欧美日韩亚洲精品一区二区三区| 久久久久久久久久国产精品| 久久在线免费观看视频| 日韩av网站在线| 欧美日韩亚洲高清| 黄色一区二区在线| 亚洲欧美成人精品| 精品视频www| 91精品视频免费观看| 欧美国产中文字幕| 成人h视频在线观看播放| 中文字幕亚洲欧美| 久久久国产精品视频| 3344国产精品免费看| 日韩三级影视基地| 久久影院免费观看| 成人激情在线观看| 国产精品视频自在线| 成人在线精品视频| 国产日韩精品在线观看| 欧美大片欧美激情性色a∨久久| 欧美一区二区色| 亚洲经典中文字幕| 国内精品久久久久久| 久久久精品影院| 亚洲欧洲午夜一线一品| 美日韩丰满少妇在线观看| 日本高清不卡在线| 97碰碰碰免费色视频| 久久久精品国产一区二区| 日韩欧美一区二区三区| 亚洲专区国产精品| 久久综合久久美利坚合众国| 亚洲欧美国产精品久久久久久久| 欧美理论电影在线观看| 成人a免费视频| 亚洲精品小视频在线观看| 日韩精品在线视频美女| 综合国产在线视频| 狠狠躁夜夜躁人人爽天天天天97| 精品成人av一区| 精品久久久久久久久久ntr影视| 国产偷国产偷亚洲清高网站| 欧美精品制服第一页| 午夜精品一区二区三区在线视| 在线精品国产欧美| 亚洲精品自拍偷拍| 亚洲一区二区在线| 欧美www在线| 日韩电影中文 亚洲精品乱码| 欧美午夜激情小视频| 日韩av最新在线| 91精品国产91久久久久久| 欧美乱大交xxxxx另类电影| 亚洲精品成人久久电影| 国产精品福利网站| 欧美中文字幕在线观看| 国产精品一区二区久久精品| 亚洲第一网中文字幕| 51ⅴ精品国产91久久久久久| 国产欧美精品va在线观看| 色悠久久久久综合先锋影音下载| 久久九九热免费视频| 久久91亚洲精品中文字幕奶水| 亚洲美女又黄又爽在线观看| 最近中文字幕日韩精品| 亚洲一品av免费观看| 91精品久久久久久久久久入口| 成人精品在线观看| 日本精品久久中文字幕佐佐木| 国产高清视频一区三区| 欧美激情视频一区二区| 亚洲桃花岛网站| 日韩福利伦理影院免费| 久久亚洲综合国产精品99麻豆精品福利| 国产亚洲精品美女久久久| 亚洲美女福利视频网站| 久久久久久久一区二区| 精品视频久久久久久| 精品成人国产在线观看男人呻吟| 人人澡人人澡人人看欧美| 国产精品www网站| 亚洲欧美自拍一区| 日韩电视剧在线观看免费网站| 欧美另类极品videosbest最新版本| 亚洲无限乱码一二三四麻| 久久久久久久网站| 亚洲综合在线小说| 亚洲国产古装精品网站| 欧美午夜美女看片| 一区二区三区四区在线观看视频| 亚洲欧美激情四射在线日| 国产欧美日韩专区发布| 亚洲成人在线网| 在线播放日韩精品| 亚洲www在线观看| 自拍亚洲一区欧美另类| 欧美在线一级va免费观看| 国产精品999999|