亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 8.0實(shí)現(xiàn)發(fā)送通知

 更新時(shí)間:2020年07月29日 11:01:15   作者:愛(ài)碼士_yan  
這篇文章主要為大家詳細(xì)介紹了Android 8.0實(shí)現(xiàn)發(fā)送通知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在Android8.0以后,針對(duì)Notification 通知api做了修改,新增了通知渠道(NotificationCannel)。下面就把demo的詳細(xì)代碼記錄下:

1.Application 為NotificationManager添加通知頻道

import android.app.Application;

import com.xinrui.ndkapp.notification.NotificationChannels;

public class NdkApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    NotificationChannels.createAllNotificationChannels(this);
  }
}

2.NotificationChannels 類

public class NotificationChannels {
  public final static String CRITICAL = "critical";
  public final static String IMPORTANCE = "importance";
  public final static String DEFAULT = "default";
  public final static String LOW = "low";
  public final static String MEDIA = "media";

  public static void createAllNotificationChannels(Context context) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if(nm == null) {
      return;
    }

    NotificationChannel mediaChannel = new NotificationChannel(
        MEDIA,
        context.getString(R.string.app_name),
        NotificationManager.IMPORTANCE_DEFAULT);
    mediaChannel.setSound(null,null);
    mediaChannel.setVibrationPattern(null);

    nm.createNotificationChannels(Arrays.asList(
        new NotificationChannel(
            CRITICAL,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_HIGH),
        new NotificationChannel(
            IMPORTANCE,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_DEFAULT),
        new NotificationChannel(
            DEFAULT,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_LOW),
        new NotificationChannel(
            LOW,
            context.getString(R.string.app_name),
            NotificationManager.IMPORTANCE_MIN),
        //custom notification channel
        mediaChannel
    ));
  }
}

3.發(fā)送通知

public void sendSimpleNotification(Context context, NotificationManager nm) {
    //創(chuàng)建點(diǎn)擊通知時(shí)發(fā)送的廣播
    Intent intent = new Intent(context, NotificationMonitorService.class);
    intent.setAction("android.service.notification.NotificationListenerService");
    PendingIntent pi = PendingIntent.getService(context,0,intent,0);
    //創(chuàng)建刪除通知時(shí)發(fā)送的廣播
    Intent deleteIntent = new Intent(context,NotificationMonitorService.class);
    deleteIntent.setAction(Intent.ACTION_DELETE);
    PendingIntent deletePendingIntent = PendingIntent.getService(context,0,deleteIntent,0);
    //創(chuàng)建通知
    Notification.Builder nb = new Notification.Builder(context, NotificationChannels.DEFAULT)
        //設(shè)置通知左側(cè)的小圖標(biāo)
        .setSmallIcon(R.drawable.ic_notification)
        //設(shè)置通知標(biāo)題
        .setContentTitle("Simple notification")
        //設(shè)置通知內(nèi)容
        .setContentText("Demo for simple notification!")
        //設(shè)置點(diǎn)擊通知后自動(dòng)刪除通知
        .setAutoCancel(true)
        //設(shè)置顯示通知時(shí)間
        .setShowWhen(true)
        //設(shè)置通知右側(cè)的大圖標(biāo)
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
        //設(shè)置點(diǎn)擊通知時(shí)的響應(yīng)事件
        .setContentIntent(pi)
        //設(shè)置刪除通知時(shí)的響應(yīng)事件
        .setDeleteIntent(deletePendingIntent);
    //發(fā)送通知
    nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build());
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 日志工具(log)的使用方法

    Android 日志工具(log)的使用方法

    這篇文章主要介紹了Android 日志工具的使用方法的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Android常用布局使用技巧示例講解

    Android常用布局使用技巧示例講解

    本文詳細(xì)介紹了Android常用布局,包括LinearLayout、RelativeLayout、FrameLayout、ConstraintLayout等,分析了它們的適用場(chǎng)景、優(yōu)缺點(diǎn)和使用技巧,幫助開(kāi)發(fā)者更好地選擇和使用合適的布局,提升UI設(shè)計(jì)和開(kāi)發(fā)效率
    2023-04-04
  • Android中Notification通知用法詳解

    Android中Notification通知用法詳解

    這篇文章主要為大家詳細(xì)介紹了Android中Notification通知用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android 可拖動(dòng)的seekbar自定義進(jìn)度值

    Android 可拖動(dòng)的seekbar自定義進(jìn)度值

    這篇文章主要介紹了Android 可拖動(dòng)的seekbar自定義進(jìn)度值的相關(guān)資料,有需要的朋友參考下
    2016-04-04
  • Android開(kāi)發(fā)Compose remember原理解析

    Android開(kāi)發(fā)Compose remember原理解析

    這篇文章主要為大家介紹了Android開(kāi)發(fā)Compose remember原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Android app會(huì)crash的原因及解決方法

    Android app會(huì)crash的原因及解決方法

    這篇文章主要介紹了Android app會(huì)crash的原因及解決方法,幫助大家更好的進(jìn)行Android開(kāi)發(fā),感興趣的朋友可以了解下
    2020-12-12
  • Android開(kāi)發(fā)ThreadPoolExecutor與自定義線程池詳解

    Android開(kāi)發(fā)ThreadPoolExecutor與自定義線程池詳解

    這篇文章主要為大家介紹了Android開(kāi)發(fā)ThreadPoolExecutor與自定義線程池詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android畫(huà)板開(kāi)發(fā)之橡皮擦功能

    Android畫(huà)板開(kāi)發(fā)之橡皮擦功能

    這篇文章主要為大家詳細(xì)介紹了Android畫(huà)板開(kāi)發(fā)之橡皮擦功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android中使用AsyncTask做下載進(jìn)度條實(shí)例代碼

    Android中使用AsyncTask做下載進(jìn)度條實(shí)例代碼

    這篇文章主要介紹了Android中使用AsyncTask做下載進(jìn)度條實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以參考下
    2017-01-01
  • JetpackCompose Navigation導(dǎo)航實(shí)現(xiàn)流程

    JetpackCompose Navigation導(dǎo)航實(shí)現(xiàn)流程

    Navigation是Jetpack用于Android導(dǎo)航的組件,作用是處理頁(yè)面跳轉(zhuǎn),以及頁(yè)面跳轉(zhuǎn)過(guò)程中的交互。使用Navigation,你就需要為每個(gè)頁(yè)面設(shè)定一條唯一路徑,它是一個(gè)String常量,形式是DeepLink的樣子,從一個(gè)頁(yè)面跳轉(zhuǎn)到另一個(gè)頁(yè)面,它通過(guò)輸入目的地的路徑進(jìn)行轉(zhuǎn)跳
    2023-01-01

最新評(píng)論