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

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

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

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

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)建點擊通知時發(fā)送的廣播
    Intent intent = new Intent(context, NotificationMonitorService.class);
    intent.setAction("android.service.notification.NotificationListenerService");
    PendingIntent pi = PendingIntent.getService(context,0,intent,0);
    //創(chuàng)建刪除通知時發(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)
        //設置通知左側的小圖標
        .setSmallIcon(R.drawable.ic_notification)
        //設置通知標題
        .setContentTitle("Simple notification")
        //設置通知內容
        .setContentText("Demo for simple notification!")
        //設置點擊通知后自動刪除通知
        .setAutoCancel(true)
        //設置顯示通知時間
        .setShowWhen(true)
        //設置通知右側的大圖標
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
        //設置點擊通知時的響應事件
        .setContentIntent(pi)
        //設置刪除通知時的響應事件
        .setDeleteIntent(deletePendingIntent);
    //發(fā)送通知
    nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build());
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

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

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

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

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

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

    Android中Notification通知用法詳解

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

    Android 可拖動的seekbar自定義進度值

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

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

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

    Android app會crash的原因及解決方法

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

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

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

    Android畫板開發(fā)之橡皮擦功能

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

    Android中使用AsyncTask做下載進度條實例代碼

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

    JetpackCompose Navigation導航實現(xiàn)流程

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

最新評論