如何彻底解决Androidstudio中运行通告栏时报Developer warning for package "com.example.my"Failed to post no

如何彻底解决Androidstudio中运行通告栏时报Developer warning for package "com.example.my"Failed to post no,第1张

概述如何彻底解决Androidstudio中运行通告时报Developerwarningforpackage"com.example.my"Failedtopostnotificationonchannel'default'Seelogformoredetails错误问题   ————安德风QQ165210274

    如何彻底解决AndroIDstudio中运行通告栏时报Developer warning for package "com.example.my"Failed to post notification on channel 'default' See log for more details

错误问题

 

 

                         ————安德风QQ1652102745

 

一、在模拟器中运行通知栏(Notification)报错效果演示:

 

 

PS:声明一下我的MainActivity程序代码是没有任何问题的前提下,出现这种报错状况

 

 

二、解决问题后正常运行效果:

①效果1(Android8.0 API26以下版本效果图)NotificationCompat.Builder类来实现通告栏功能

 

②效果2(Android8.0 API26及以上版本效果图)NotificationCompat.Builder类来实现通告栏功能

 

 

三、解决方案:

1、分析原因

出现这种错误 主要原因是我们一般常用NotificationCompat.Builder类来实现通告栏功能

NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");

在Android8.0 (API26)以前手机上运行通知栏要么全部屏蔽通知栏(防止骚扰),要么全部正常运行通知栏。我们用NotificationCompat.Builder类实现是完全没有问题的。

自从Android8.0(API26)出现时,为了让用户更好的体验,于是新增通道功能(也就是NotificationChannel类),通过通道功能(也就是NotificationChannel类)能够让用户有选择的权利,用户可以设置屏蔽无关重要的垃圾通告,用户也可以设置开启选择重要的通告。

 NotificationChannel mChannel = new NotificationChannel(ID, "123", importance);//建立通知栏通道类(需要有ID,重要属性)

 

也就是说我们在Android8.0(API26)及以上的版本实现通告栏功能用NotificationCompat.Builder类,是无法正常运行的就会导致我们出现报错现象出现:

Developer warning for package "com.example.my"Failed to post notification on channel 'default' See log for more details

错误问题

 

2、解决方法:

 

 

①如果你的模拟器运行系统版本是Android8.0以下的就用NotificationCompat.Builder类来实现通告栏功能

 

布局设计源代码:https://www.cnblogs.com/adf520/p/12601872.HTML

2-1、MainActivity.java功能实现源代码

 

  1 package com.example.my;  2   3 import androIDx.annotation.RequiresAPI;  4 import androIDx.appcompat.app.AppCompatActivity;  5 import androIDx.core.app.NotificationCompat;  6   7 import androID.app.Notification;  8 import androID.app.NotificationChannel;  9 import androID.app.notificationmanager; 10 import androID.content.Context; 11 import androID.content.Contextwrapper; 12 import androID.content.Intent; 13 import androID.content.pm.PackageManager; 14 import androID.graphics.Bitmap; 15 import androID.graphics.BitmapFactory; 16 import androID.graphics.color; 17 import androID.os.Build; 18 import androID.os.Bundle; 19 import androID.provIDer.Settings; 20 import androID.text.Layout; 21 import androID.vIEw.LayoutInflater; 22 import androID.vIEw.VIEw; 23 import androID.Widget.button; 24 import androID.Widget.CheckBox; 25 import androID.Widget.TextVIEw; 26 import androID.Widget.Toast; 27  28 public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { 29 CheckBox cb1,cb2,cb3; 30 button pay; 31 int count=0; 32     @RequiresAPI(API = Build.VERSION_CODES.O) 33     @OverrIDe 34     protected voID onCreate(Bundle savedInstanceState) { 35         super.onCreate(savedInstanceState); 36         setContentVIEw(R.layout.activity_main); 37          38         cb1=findVIEwByID(R.ID.cb1); 39         cb2=findVIEwByID(R.ID.cb2); 40         cb3=findVIEwByID(R.ID.cb3); 41         pay=findVIEwByID(R.ID.pay); 42         pay.setonClickListener(this); 43  44  45     } 46  47     @RequiresAPI(API = Build.VERSION_CODES.O) 48     @OverrIDe 49     public voID onClick(VIEw v) { 50         if (cb1.isChecked()) 51             count+=45; 52         if (cb2.isChecked()) 53             count+=60; 54         if (cb3.isChecked()) 55             count+=55; 56  57  58 //        final notificationmanager manager = (notificationmanager)getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务) 59 //        String ID = "channel_1"; //自定义设置通道ID属性 60 //        String description = "123";//自定义设置通道描述属性 61 //        int importance = notificationmanager.importANCE_HIGH;//通知栏管理重要提示消息声音设定 62 //        /** 63 //         * Oreo不用Priority了,用importance 64 //         * importANCE_NONE 关闭通知 65 //         * importANCE_MIN 开启通知,不会d出,但没有提示音,状态栏中无显示 66 //         * importANCE_LOW 开启通知,不会d出,不发出提示音,状态栏中显示 67 //         * importANCE_DEFAulT 开启通知,不会d出,发出提示音,状态栏中显示 68 //         * importANCE_HIGH 开启通知,会d出,发出提示音,状态栏中显示 69 //         */ 70 //        NotificationChannel mChannel = new NotificationChannel(ID, "123", importance);//建立通知栏通道类(需要有ID,重要属性) 71 ////        mChannel.setDescription(description); // 配置通知渠道的属性 72 ////        mChannel.enablelights(true);// 设置通知出现时的闪灯(如果 androID 设备支持的话) 73 ////        mChannel.setlightcolor(color.RED);//设置闪灯颜色为红色 74 ////        mChannel.enableVibration(true);   // 设置通知出现时的震动(如果 androID 设备支持的话) 75 ////        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 76 //       manager.createNotificationChannel(mChannel);////最后在notificationmanager中创建该通知渠道 77 //        Notification notification = new Notification.Builder(this, ID)//创建Notification对象。 78 //                .setContentTitle("付款通知")  //设置通知标题 79 //                .setSmallicon(R.drawable.q)//设置通知小图标 80 //                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q))//设置通知大图标 81 //                .setContentText("您已付款"+count+"元")//设置通知内容 82 //                .setautoCancel(true)//设置自动删除通知 83 //                .build();//运行 84 // 85 //        manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知 86 //        count=0;//清空金额 87  88 // 89 //     LayoutInflater inflater=getLayoutInflater(); 90 //     VIEw layout=inflater.inflate(R.layout.activity_main2,null); 91 //     TextVIEw tv= layout.findVIEwByID(R.ID.tv); 92 //     tv.setText("您支付了"+count+"元"); 93 //     Toast toast=new Toast(MainActivity.this); 94 //     toast.setVIEw(layout); 95 //     toast.setDuration(Toast.LENGTH_SHORT); 96 //     toast.show(); 97 //     count=0; 98  99         100 101         //第一步:创建通知构造器NotificationCompat.Builder对象。102         NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");103         //第二步:调用NotificationCompat.Builder对象的方法设置通知相关内容。104         builder.setSmallicon(R.drawable.q);//设置通知小图标105         builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q));//设置通知大图标106         builder.setContentTitle("付款通知");//设置通知标题107         builder.setContentText("您已付款"+count+"元");//设置通知内容108         builder.setautoCancel(true);//设置自动删除通知109         Notification notification=builder.build();//:创建Notification对象。110         notificationmanager manager= (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)111 //        manager.notify(1,notification); //通知栏保留单条通知112         manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知113         count=0;//清空金额114 116         117     }118 }

 

 

 

②如果你的模拟器运行系统版本是Android8.0及以上的就用  NotificationChannel类来实现通告栏功能

2-1、MainActivity.java功能实现源代码

  1 package com.example.my;  2   3 import androIDx.annotation.RequiresAPI;  4 import androIDx.appcompat.app.AppCompatActivity;  5 import androIDx.core.app.NotificationCompat;  6   7 import androID.app.Notification;  8 import androID.app.NotificationChannel;  9 import androID.app.notificationmanager; 10 import androID.content.Context; 11 import androID.content.Contextwrapper; 12 import androID.content.Intent; 13 import androID.content.pm.PackageManager; 14 import androID.graphics.Bitmap; 15 import androID.graphics.BitmapFactory; 16 import androID.graphics.color; 17 import androID.os.Build; 18 import androID.os.Bundle; 19 import androID.provIDer.Settings; 20 import androID.text.Layout; 21 import androID.vIEw.LayoutInflater; 22 import androID.vIEw.VIEw; 23 import androID.Widget.button; 24 import androID.Widget.CheckBox; 25 import androID.Widget.TextVIEw; 26 import androID.Widget.Toast; 27  28 public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { 29 CheckBox cb1,cb2,cb3; 30 button pay; 31 int count=0; 32     @RequiresAPI(API = Build.VERSION_CODES.O) 33     @OverrIDe 34     protected voID onCreate(Bundle savedInstanceState) { 35         super.onCreate(savedInstanceState); 36         setContentVIEw(R.layout.activity_main); 37  38         cb1=findVIEwByID(R.ID.cb1); 39         cb2=findVIEwByID(R.ID.cb2); 40         cb3=findVIEwByID(R.ID.cb3); 41         pay=findVIEwByID(R.ID.pay); 42         pay.setonClickListener(this); 43  44  45     } 46  47     @RequiresAPI(API = Build.VERSION_CODES.O) 48     @OverrIDe 49     public voID onClick(VIEw v) { 50         if (cb1.isChecked()) 51             count+=45; 52         if (cb2.isChecked()) 53             count+=60; 54         if (cb3.isChecked()) 55             count+=55; 56  57  58         final notificationmanager manager = (notificationmanager)getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务) 59         String ID = "channel_1"; //自定义设置通道ID属性 60         String description = "123";//自定义设置通道描述属性 61         int importance = notificationmanager.importANCE_HIGH;//通知栏管理重要提示消息声音设定 62         /** 63          * Oreo不用Priority了,用importance 64          * importANCE_NONE 关闭通知 65          * importANCE_MIN 开启通知,不会d出,但没有提示音,状态栏中无显示 66          * importANCE_LOW 开启通知,不会d出,不发出提示音,状态栏中显示 67          * importANCE_DEFAulT 开启通知,不会d出,发出提示音,状态栏中显示 68          * importANCE_HIGH 开启通知,会d出,发出提示音,状态栏中显示 69          */ 70         NotificationChannel mChannel = new NotificationChannel(ID, "123", importance);//建立通知栏通道类(需要有ID,重要属性) 71 //        mChannel.setDescription(description); // 配置通知渠道的属性 72 //        mChannel.enablelights(true);// 设置通知出现时的闪灯(如果 androID 设备支持的话) 73 //        mChannel.setlightcolor(color.RED);//设置闪灯颜色为红色 74 //        mChannel.enableVibration(true);   // 设置通知出现时的震动(如果 androID 设备支持的话) 75 //        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); 76        manager.createNotificationChannel(mChannel);////最后在notificationmanager中创建该通知渠道 77         Notification notification = new Notification.Builder(this, ID)//创建Notification对象。 78                 .setContentTitle("付款通知")  //设置通知标题 79                 .setSmallicon(R.drawable.q)//设置通知小图标 80                 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q))//设置通知大图标 81                 .setContentText("您已付款"+count+"元")//设置通知内容 82                 .setautoCancel(true)//设置自动删除通知 83                 .build();//运行 84  85         manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知 86         count=0;//清空金额 87  88 // 89 //     LayoutInflater inflater=getLayoutInflater(); 90 //     VIEw layout=inflater.inflate(R.layout.activity_main2,null); 91 //     TextVIEw tv= layout.findVIEwByID(R.ID.tv); 92 //     tv.setText("您支付了"+count+"元"); 93 //     Toast toast=new Toast(MainActivity.this); 94 //     toast.setVIEw(layout); 95 //     toast.setDuration(Toast.LENGTH_SHORT); 96 //     toast.show(); 97 //     count=0; 98  99 100 101 //        //第一步:创建通知构造器NotificationCompat.Builder对象。102 //        NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),"default");103 //        //第二步:调用NotificationCompat.Builder对象的方法设置通知相关内容。104 //        builder.setSmallicon(R.drawable.q);//设置通知小图标105 //        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.q));//设置通知大图标106 //        builder.setContentTitle("付款通知");//设置通知标题107 //        builder.setContentText("您已付款"+count+"元");//设置通知内容108 //        builder.setautoCancel(true);//设置自动删除通知109 //        Notification notification=builder.build();//:创建Notification对象。110 //        notificationmanager manager= (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);//通知栏管理器(得到系统服务)111 ////        manager.notify(1,notification); //通知栏保留单条通知112 //        manager.notify((int) System.currentTimeMillis(),notification); //通知栏保留多条通知113 //        count=0;//清空金额114 115 116 117     }118 }

 

 

 

以上代码仅作为参考,希望能给你带来帮助,我是安德风,感谢大家的关注与支持,有问题欢迎在下方留言;看到后一一答复。

 

 

总结

以上是内存溢出为你收集整理的如何彻底解决Androidstudio中运行通告栏时报Developer warning for package "com.example.my"Failed to post no全部内容,希望文章能够帮你解决如何彻底解决Androidstudio中运行通告栏时报Developer warning for package "com.example.my"Failed to post no所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)