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

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

Notification的基本使用

2019-11-09 15:50:56
字體:
來源:轉載
供稿:網友
        // 在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
国产精品av在线| 日韩激情视频在线| 日韩免费电影在线观看| 在线看日韩av| 91亚洲精品视频| 精品国产91久久久久久老师| 国产精品欧美一区二区| 成人免费视频a| 久久视频中文字幕| 日韩免费看的电影电视剧大全| 国产精品视频精品视频| 色综合伊人色综合网| 亚洲美女av网站| 最近2019年中文视频免费在线观看| 国产精品第2页| 亚洲精品久久久久中文字幕二区| 欧洲s码亚洲m码精品一区| 欧美成人免费一级人片100| 国产亚洲精品综合一区91| 国产精品一区二区三区成人| 黄色一区二区在线观看| 日韩av在线导航| 92看片淫黄大片看国产片| 日本精品免费一区二区三区| 亚洲欧洲激情在线| 国产精品久久久久久久久影视| 97在线视频免费| 国产精品白嫩初高中害羞小美女| 国产成人精品av在线| 国产成人精品免高潮费视频| 亚洲精品wwwww| 亚洲va码欧洲m码| 777国产偷窥盗摄精品视频| 在线亚洲国产精品网| 精品国产欧美一区二区三区成人| 欧美黑人一级爽快片淫片高清| 亚洲色图激情小说| 欧美激情一级精品国产| 国产精品一区二区久久久久| 日韩电影在线观看永久视频免费网站| 欧美成人网在线| 欧美日产国产成人免费图片| 国产成人久久久精品一区| 91欧美精品午夜性色福利在线| 日韩成人黄色av| 狠狠爱在线视频一区| 久久国产精品视频| 久久综合网hezyo| 亚洲综合一区二区不卡| 最近中文字幕日韩精品| 亚洲一区二区三区在线视频| 国产精品情侣自拍| 久久久久久久一区二区| 久久亚洲私人国产精品va| 国产91在线高潮白浆在线观看| 久久99久久亚洲国产| 国产成人aa精品一区在线播放| 久久av红桃一区二区小说| 欧美大片在线看| 国产精品无码专区在线观看| 中文字幕久热精品视频在线| 国产日韩av高清| 久久综合88中文色鬼| 欧美与欧洲交xxxx免费观看| 91精品国产91久久久| 日韩av手机在线| 欧美性猛交xxx| 97色在线视频| 91在线直播亚洲| 欧美理论电影在线播放| 中文字幕精品国产| 日韩成人中文字幕在线观看| 都市激情亚洲色图| 精品亚洲国产成av人片传媒| 成人午夜激情免费视频| 欧美亚洲国产成人精品| 国产香蕉一区二区三区在线视频| 国产va免费精品高清在线观看| 中文字幕av一区二区三区谷原希美| 日韩av在线不卡| 亚洲激情在线观看视频免费| 欧美日韩中文在线| 欧美激情一区二区三区成人| 欧美在线视频免费观看| 色噜噜狠狠狠综合曰曰曰88av| 成人激情视频在线观看| 伊人激情综合网| 日韩精品免费在线观看| 在线播放国产一区中文字幕剧情欧美| 日本久久久a级免费| 国产精品久久久久久久久免费看| 久久999免费视频| 久久中文字幕在线视频| 成人欧美一区二区三区在线湿哒哒| 欧美日韩亚洲高清| 国产一区二区三区在线看| 日韩欧美在线一区| 97精品久久久中文字幕免费| 欧美国产日韩一区二区在线观看| 久久成人av网站| 欧美午夜精品久久久久久浪潮| 亚洲精品日韩欧美| 亚洲天堂av电影| 最新69国产成人精品视频免费| 在线成人中文字幕| 久久久噜噜噜久久| 久久夜精品香蕉| 啊v视频在线一区二区三区| 在线播放精品一区二区三区| 国产精品视频公开费视频| 亚洲精品久久久久中文字幕欢迎你| 国产精品亚洲综合天堂夜夜| 亚洲人成在线观看| 日韩精品在线影院| 亚洲免费视频在线观看| 久久中文字幕国产| 亚洲精品成人久久| 九九精品视频在线| 欧美性受xxxx白人性爽| 久久亚洲国产精品成人av秋霞| 国模精品视频一区二区| 国产一区二区三区网站| 一本色道久久88精品综合| 秋霞av国产精品一区| 亚洲精品v天堂中文字幕| 欧美成人激情在线| 96国产粉嫩美女| 97精品一区二区视频在线观看| 1769国产精品| 国产亚洲欧美日韩美女| 亚洲开心激情网| 亚洲天堂成人在线视频| 97视频网站入口| 久久视频免费观看| 亚洲va久久久噜噜噜| 成人xvideos免费视频| 欧美成人免费网| 欧美成人激情视频免费观看| 亚洲人成网站777色婷婷| 日韩精品极品在线观看| 午夜精品视频网站| 亚洲精品丝袜日韩| 成人av在线亚洲| 亚洲字幕一区二区| 一本一本久久a久久精品综合小说| 精品国产乱码久久久久久虫虫漫画| 怡红院精品视频| 亚洲精品成人久久电影| 国产成人+综合亚洲+天堂| 亚洲男人天堂2019| 91精品视频在线播放| 日韩在线播放av| 久久久国产精品一区| 一区二区三区动漫| www国产精品视频| 午夜免费久久久久| 日韩经典一区二区三区| 在线亚洲男人天堂| 久久精品国产清自在天天线| 在线丨暗呦小u女国产精品| 国产精品观看在线亚洲人成网| 国产在线视频欧美| 中文字幕日韩av综合精品| 日韩黄色av网站|