欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程之通知欄的用法小結(jié)

 更新時(shí)間:2017年01月30日 10:12:40   作者:books1958  
這篇文章主要介紹了Android編程之通知欄的用法,結(jié)合實(shí)例形式總結(jié)分析了Android通知欄的相關(guān)操作技巧,包括發(fā)送、刪除通知、自定義布局等操作實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例總結(jié)了Android編程中通知欄的用法。分享給大家供大家參考,具體如下:

很久沒(méi)有使用Android的通知功能了,今天把兩年前的代碼搬出來(lái)一看,發(fā)現(xiàn)很多方法都廢棄了,代碼中各種刪除線(xiàn)看的十分不爽。于是乎,打開(kāi)Google,查看官方文檔,學(xué)習(xí)最新的發(fā)送通知欄消息的方法。

本文中的代碼均參照谷歌官方文檔編寫(xiě):

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

1.首先,獲取系統(tǒng)的通知服務(wù):

復(fù)制代碼 代碼如下:
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

2.發(fā)送一個(gè)最簡(jiǎn)單的通知

public void simpleNotice(View view) {
    //此Builder為android.support.v4.app.NotificationCompat.Builder中的,下同。
    Builder mBuilder = new Builder(this);
    //系統(tǒng)收到通知時(shí),通知欄上面顯示的文字。
    mBuilder.setTicker("天津,晴,2~15度,微風(fēng)");
    //顯示在通知欄上的小圖標(biāo)
    mBuilder.setSmallIcon(R.drawable.consult_answer);
    //通知標(biāo)題
    mBuilder.setContentTitle("天氣預(yù)報(bào)");
    //通知內(nèi)容
    mBuilder.setContentText("天津,晴,2~15度,微風(fēng)");
    //設(shè)置大圖標(biāo),即通知條上左側(cè)的圖片(如果只設(shè)置了小圖標(biāo),則此處會(huì)顯示小圖標(biāo))
    mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.share_sina));
    //顯示在小圖標(biāo)左側(cè)的數(shù)字
    mBuilder.setNumber(6);
    //設(shè)置為不可清除模式
    mBuilder.setOngoing(true);
    //顯示通知,id必須不重復(fù),否則新的通知會(huì)覆蓋舊的通知(利用這一特性,可以對(duì)通知進(jìn)行更新)
    nm.notify(1, mBuilder.build());
}

3.刪除一個(gè)通知。參數(shù)即為通知的id

nm.cancel(1);

4.發(fā)送一個(gè)通知,點(diǎn)擊通知后跳轉(zhuǎn)到一個(gè)Activity,從這個(gè)Activity返回后,進(jìn)入程序內(nèi)的某一個(gè)頁(yè)面(一般為主頁(yè))

//點(diǎn)擊通知進(jìn)入一個(gè)Activity,點(diǎn)擊返回時(shí)進(jìn)入指定頁(yè)面。
public void resultActivityBackApp(View view) {
    Builder mBuilder = new Builder(this);
    mBuilder.setTicker("通知標(biāo)題2");
    mBuilder.setSmallIcon(R.drawable.ic_launcher);
    mBuilder.setContentTitle("通知標(biāo)題2");
    mBuilder.setContentText("點(diǎn)擊通知進(jìn)入一個(gè)Activity,點(diǎn)擊返回時(shí)進(jìn)入指定頁(yè)面。");
    //設(shè)置點(diǎn)擊一次后消失(如果沒(méi)有點(diǎn)擊事件,則該方法無(wú)效。)
    mBuilder.setAutoCancel(true);
    //點(diǎn)擊通知之后需要跳轉(zhuǎn)的頁(yè)面
    Intent resultIntent = new Intent(this, ResultActivityBackApp.class);
    //使用TaskStackBuilder為“通知頁(yè)面”設(shè)置返回關(guān)系
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    //為點(diǎn)擊通知后打開(kāi)的頁(yè)面設(shè)定 返回 頁(yè)面。(在manifest中指定)
    stackBuilder.addParentStack(ResultActivityBackApp.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent pIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pIntent);
    // mId allows you to update the notification later on.
    nm.notify(2, mBuilder.build());
}

同時(shí),需要在manifest中為點(diǎn)擊通知后打開(kāi)的Activity指定父Activity.

<activity
  android:name=".ResultActivityBackApp"
  android:parentActivityName=".MainActivity">
  <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".MainActivity" />
</activity>

(其中,activity的屬性parentActivityName為API 16中的屬性,meta-data中的代碼為兼容API 16以下。因此,對(duì)于大多數(shù)程序,這兩個(gè)地方都得寫(xiě)。)

5.和上述4類(lèi)似,只是在打開(kāi)的Activity中返回時(shí)回到home頁(yè)

//點(diǎn)擊通知進(jìn)入一個(gè)Activity,點(diǎn)擊返回時(shí)回到桌面
public void resultActivityBackHome(View view) {
    Builder mBuilder = new Builder(this);
    mBuilder.setTicker("通知標(biāo)題3");
    mBuilder.setSmallIcon(R.drawable.ic_launcher);
    mBuilder.setContentTitle("通知標(biāo)題3");
    mBuilder.setContentText("點(diǎn)擊通知進(jìn)入一個(gè)Activity,點(diǎn)擊返回時(shí)回到桌面");
    //設(shè)置點(diǎn)擊一次后消失(如果沒(méi)有點(diǎn)擊事件,則該方法無(wú)效。)
    mBuilder.setAutoCancel(true);
    Intent notifyIntent = new Intent(this, ResultActivityBackHome.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pIntent);
    nm.notify(3, mBuilder.build());
}

6.帶進(jìn)度條的通知

public void progressNotice(View view) {
    final Builder mBuilder = new Builder(this);
    mBuilder.setTicker("通知標(biāo)題4");
    mBuilder.setContentTitle("Picture Download")
        .setContentText("Download in progress")
        .setSmallIcon(R.drawable.ic_launcher);
    // Start a lengthy operation in a background thread
    new Thread(new Runnable() {
      @Override
      public void run() {
        int progress;
        for (progress = 0; progress <= 100; progress++) {
          // Sets the progress indicator to a max value, the current completion percentage,
          // and "determinate" state
          mBuilder.setProgress(100, progress, false);
          //不明確進(jìn)度的進(jìn)度條
//          mBuilder.setProgress(0, 0, true);
          nm.notify(4, mBuilder.build());
          // 模擬延時(shí)
          try {
            Thread.sleep(200);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        // When the loop is finished, updates the notification
        mBuilder.setContentText("Download complete");
        // Removes the progress bar
        mBuilder.setProgress(0, 0, false);
        nm.notify(4, mBuilder.build());
      }
    }
    ).start();
}

7.擴(kuò)展布局的通知。按住通知條下滑,可以查看更詳細(xì)的內(nèi)容

public void expandLayoutNotice(View view) {
    Builder mBuilder = new Builder(this);
    mBuilder.setTicker("通知標(biāo)題5");
    mBuilder.setSmallIcon(R.drawable.ic_launcher);
    mBuilder.setContentTitle("通知標(biāo)題5");
    mBuilder.setContentText("按住通知下拉可顯示擴(kuò)展布局");
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    String[] events = new String[]{"Beijing", "Tianjin", "Shanghai", "Guangzhou"};
    // 設(shè)置擴(kuò)展布局的標(biāo)題
    inboxStyle.setBigContentTitle("Event tracker details:");
    for (String s : events) {
      inboxStyle.addLine(s);
    }
    mBuilder.setStyle(inboxStyle);
    nm.notify(5, mBuilder.build());
}

8.自定義布局的通知欄。(根據(jù)谷歌的官方文檔不推薦這么做,因?yàn)槭褂眠@種方式時(shí),對(duì)不同屏幕進(jìn)行適配需要考慮的因素太多。而且,通知欄應(yīng)該展示的就是最簡(jiǎn)明扼要的信息,對(duì)于大多數(shù)程序默認(rèn)的布局已經(jīng)足夠了。)

//自定義布局的通知
public void customLayoutNotice(View view) {
    Builder mBuilder = new Builder(this);
    mBuilder.setTicker("通知標(biāo)題6");
    mBuilder.setTicker("通知標(biāo)題6");
    mBuilder.setSmallIcon(R.drawable.ic_launcher);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_layout_notice);
    mBuilder.setContent(remoteViews);
    //為RemoteViews上的按鈕設(shè)置文字
    remoteViews.setCharSequence(R.id.custom_layout_button1, "setText", "Button1");
    remoteViews.setCharSequence(R.id.custom_layout_button2, "setText", "Button2");
    //為RemoteViews上的按鈕設(shè)置點(diǎn)擊事件
    Intent intent1 = new Intent(this, CustomLayoutResultActivity.class);
    intent1.putExtra("content", "From button1 click!");
    PendingIntent pIntentButton1 = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.custom_layout_button1, pIntentButton1);
    Intent intent2 = new Intent(this, CustomLayoutResultActivity.class);
    intent2.putExtra("content", "From button2 click!");
    PendingIntent pIntentButton2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.custom_layout_button2, pIntentButton2);
    nm.notify(6, mBuilder.build());
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android動(dòng)態(tài)使用VectorDrawable過(guò)程詳解

    Android動(dòng)態(tài)使用VectorDrawable過(guò)程詳解

    這篇文章主要介紹了Android動(dòng)態(tài)使用VectorDrawable過(guò)程,2014年6月26日的I/O 2014開(kāi)發(fā)者大會(huì)上谷歌正式推出了Android L,它帶來(lái)了全新的設(shè)計(jì)語(yǔ)言Material Design,新的API也提供了這個(gè)類(lèi)VectorDrawable
    2023-02-02
  • Android開(kāi)發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽(tīng)方法總結(jié)

    Android開(kāi)發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽(tīng)方法總結(jié)

    這篇文章主要介紹了Android開(kāi)發(fā)之Button事件實(shí)現(xiàn)與監(jiān)聽(tīng)方法,結(jié)合實(shí)例形式總結(jié)分析了Android開(kāi)發(fā)中Button事件的兩種實(shí)現(xiàn)方法以及針對(duì)Button控件的幾種常用監(jiān)聽(tīng)方法,需要的朋友可以參考下
    2016-01-01
  • android studio的安裝(史上最詳細(xì))

    android studio的安裝(史上最詳細(xì))

    這篇文章主要介紹了android studio的安裝(史上最詳細(xì)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解

    Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android Dialog對(duì)話(huà)框用法實(shí)例詳解

    Android Dialog對(duì)話(huà)框用法實(shí)例詳解

    這篇文章主要介紹了Android Dialog對(duì)話(huà)框用法,結(jié)合實(shí)例形式分析了Android使用Dialog對(duì)話(huà)框過(guò)程中所涉及的創(chuàng)建、保存、回復(fù)等操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • Android實(shí)現(xiàn)圖片預(yù)覽與保存功能

    Android實(shí)現(xiàn)圖片預(yù)覽與保存功能

    在App開(kāi)發(fā)中,通常為了省流提高加載速度提升用戶(hù)體驗(yàn)我們通常在列表中或新聞中的插圖都是以縮略圖壓縮過(guò)的圖片來(lái)進(jìn)行展示,當(dāng)用戶(hù)點(diǎn)擊圖片時(shí)我們?cè)偃ゼ虞d真正像素的大圖讓用戶(hù)預(yù)覽。本文將利用Flutter實(shí)現(xiàn)這一功能,需要的可以參考一下
    2022-04-04
  • Android自定義View實(shí)現(xiàn)數(shù)字密碼鎖

    Android自定義View實(shí)現(xiàn)數(shù)字密碼鎖

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)數(shù)字密碼鎖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android中ADB命令用法大結(jié)局

    Android中ADB命令用法大結(jié)局

    adb全名android debug bridge 安卓調(diào)試橋,下面這篇文章主要給大家介紹了關(guān)于Android中ADB命令用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Android中ListView的item點(diǎn)擊沒(méi)有反應(yīng)的解決方法

    Android中ListView的item點(diǎn)擊沒(méi)有反應(yīng)的解決方法

    這篇文章主要介紹了Android中ListView的item點(diǎn)擊沒(méi)有反應(yīng)的相關(guān)資料,需要的朋友可以參考下
    2017-10-10
  • JNI方法實(shí)現(xiàn)圖片壓縮(壓縮率極高)

    JNI方法實(shí)現(xiàn)圖片壓縮(壓縮率極高)

    這篇文章主要給大家介紹了一種JNI方法實(shí)現(xiàn)圖片壓縮(壓縮率極高)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評(píng)論