华为荣耀v10v10用不了NotificationCompat.Builder

Android开发:关于notification兼容问题_百度知道
Android开发:关于notification兼容问题
关于notification。两种写法,一种v4的compat.build,一种最原始的notification。
两种写法在模拟器或htc上都能正常发送通知,可是小米手机都不行,有没有人试过小米的呢,小米上该如何写notification。
NotificationManager mNotificationMana...
我有更好的答案
小米手机是可以的呀,我刚运行了这段代码,你那报什么错误吗?
没有任何报错,只是通知栏上不出东西。所以不知道从哪下手处理了。你运行我发的代码没有问题吗?
没有问题的,可以正常显示。MI2,V5系统。你加一个设置试试,Bitmap largeIcon = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ic_launcher);builder.setLargeIcon(largeIcon);试试看。再加上这一句:builder.setTicker(&title & + &content&);
我用了另外的小米也是可以的。怀疑这个做测试机的小米系统是不是有问题了,我刷个机再试试看。谢谢你
采纳率:60%
为您推荐:
其他类似问题
您可能关注的内容
android开发的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。4039人阅读
Android适配(4)
首先是发送普通通知的一段代码。
public void send(String string) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("标题");// 设置通知栏标题
mBuilder.setSmallIcon(R.drawable.ic_launcher);// 设置通知小ICON
mBuilder.setContentText(string + "点击查看。");
mBuilder.setTicker(string); // 通知首次出现在通知栏,带上升动画效果的
mBuilder.setWhen(System.currentTimeMillis());// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
Notification notification = mBuilder.build();//API 16
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
在Android 5.0以下版本,icon可以正常显示,但是在5.0以上版本中,icon显示为白块。
随后百度这个问题找到一个网址:
大体意思是在5.0中,对于设计风格有了统一的要求。
引用原文:简单的说就是5.0后Android官方建议不要为通知的图标添加任何额外的透明度,渐变色,不要企图用颜色将通知图标与其他应用,比如系统应用,应用的通知图标只能是在透明的背景上有白色的图案。至于原因,文档并没有细说,只是提到5.0系统将会在底层处理图标,想知怎么处理的可以参考Android
SDK API level 21后的Notificaiton源码,里面写的较详细。可以参考:
知道了问题的解决方法后,做了修改。如下(终极版):
public void send(String string) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,intent, 0);
mBuilder.setContentIntent(pendingIntent);// 设置通知栏点击意图
mBuilder.setContentTitle("标题");// 设置通知栏标题
if (Build.VERSION.SDK_INT &= Build.VERSION_CODES.LOLLIPOP) {//悬挂式Notification,5.0后显示
mBuilder.setContentText(string + "点击查看。").setFullScreenIntent(pendingIntent, true);
mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
mBuilder.setSmallIcon(R.drawable.jingbao1);// 设置通知小ICON(5.0必须采用白色透明图片)
mBuilder.setSmallIcon(R.drawable.ic_launcher);// 设置通知小ICON
mBuilder.setContentText(string );
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.jing_6));// 设置通知大ICON
mBuilder.setTicker(string + "有警报!"); // 通知首次出现在通知栏,带上升动画效果的
mBuilder.setWhen(System.currentTimeMillis());// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
mBuilder.setPriority(NotificationCompat.PRIORITY_MAX); // 设置该通知优先级
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);//在任何情况下都显示,不受锁屏影响。
mBuilder.setAutoCancel(true);// 设置这个标志当用户单击面板就可以让通知将自动取消
mBuilder.setOngoing(false);// ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用NotificationCompat.DEFAULT_ALL属性,可以组合
mBuilder.setVibrate(new long[] { 0, 100, 500, 100 });//振动效果需要振动权限
mBuilder.setSound(Uri.parse("android.resource://" + getPackageName()//声音
+ "/" + R.raw.notification_alarm));
mBuilder.setDefaults(NotificationCompat.DEFAULT_LIGHTS);//闪灯
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//Notification notification = mBuilder.getNotification();//API 11
Notification notification = mBuilder.build();//API 16
mNotificationManager.notify(1, notification);
以上列举了常用的通知设置介绍。
如果你按照上面的进行了修改,结果成功,那么恭喜你!,如果你发现没有效果,那么只能说点背(我就是)。
问题出在了哪里?
其实代码方面没有任何问题,只是现在国产手机都有自己的定制化UI,在我的手上有两款手机,一款是华为的EMUI,一款是乐视的
EUI。这两个对于通知都有自己的管理,基本的管理有优先级、是否允许、是否悬浮显示通知。如下图:
所以在上面的代码中,设置通知优先级、悬浮、显示的权限(不受锁屏限制)等这些设置会无效。
这些的实现需要用户自己在设置里进行管理设置。
甚至在乐视的手机上,设置的小图,大图都会失效。最后发现乐视的EUI会对常用的app(qq,微信等)进行适配在UI中提供小图。
(例如美团在华为的显示正常,但是乐视就。。)
最后再说说小图的尺寸:
mdpi @ 24.00dp = 24.00px
hdpi @ 24.00dp = 36.00px
xhdpi @ 24.00dp = 48.00px
xxhdpi @ 24.00dp = 72.00px
阅读:5367
文章:29篇
阅读:68106
阅读:9114Android Developers
&&&↳
android.support.v4.app.NotificationCompat.Builder
Class Overview
Builder class for
Allows easier control over
all the flags, as well as help constructing the typical notification layouts.
On platform versions that don't offer expanded notifications, methods that depend on
expanded notifications have no effect.
For example, action buttons won't appear on platforms prior to Android 4.1. Action
buttons depend on expanded notifications, which are only available in Android 4.1
and later.
For this reason, you should always ensure that UI controls in a notification are also
available in an
in your app, and you should always start that
when users click the notification. To do this, use the
Public Constructors
( context)
Constructor.
Public Methods
(int icon,
Add an action to this notification.
Combine all of the options that have been set and return a new
This method is deprecated.
(boolean autoCancel)
Setting this flag will make it so the notification is automatically
canceled when the user clicks it in the panel.
Supply a custom RemoteViews to use instead of the standard one.
Set the large text at the right-hand side of the notification.
to send when the notification is clicked.
Set the text (second row) of the notification, in a standard notification.
Set the title (first row) of the notification, in a standard notification.
(int defaults)
Set the default notification options that will be used.
to send when the notification is cleared by the user
directly from the notification panel.
( intent, boolean highPriority)
An intent to launch instead of posting the notification to the status bar.
Set the large icon that is shown in the ticker and notification.
(int argb, int onMs, int offMs)
Set the argb value that you would like the LED on the device to blnk, as well as the
(int number)
Set the large number at the right-hand side of the notification.
(boolean ongoing)
Set whether this is an ongoing notification.
(boolean onlyAlertOnce)
Set this flag if you would only like the sound, vibrate
and ticker to be played if the notification is not already showing.
Set the relative priority for this notification.
(int max, int progress, boolean indeterminate)
Set the progress this notification represents, which may be
represented as a .
(int icon, int level)
A variant of
that takes an additional
level parameter for when the icon is a .
(int icon)
Set the small icon to use in the notification layouts.
Set the sound to play.
( sound, int streamType)
Set the sound to play.
Add a rich notification style to be applied at build time.
Set the third line of text in the platform notification template.
( tickerText,
Set the text that is displayed in the status bar when the notification first
arrives, and also a RemoteViews object that may be displayed instead on some
( tickerText)
Set the text that is displayed in the status bar when the notification first
(boolean b)
field as a stopwatch.
(long[] pattern)
Set the vibration pattern to use.
(long when)
Set the time that the event occurred.
Inherited Methods
From class
Creates and returns a copy of this Object.
Compares this instance with the specified object and indicates if they
are equal.
Invoked when the garbage collector has detected that this instance is no longer reachable.
Returns the unique instance of
that represents this
object's class.
Returns an integer hash code for this object.
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
Returns a string containing a concise, human-readable description of this
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
Public Constructors
NotificationCompat.Builder
( context)
Public Methods
(int icon,
getNotification
This method is deprecated.
setAutoCancel
(boolean autoCancel)
setContent
setContentInfo
setContentIntent
setContentText
setContentTitle
setDefaults
(int defaults)
setDeleteIntent
setFullScreenIntent
( intent, boolean highPriority)
setLargeIcon
(int argb, int onMs, int offMs)
(int number)
setOngoing
(boolean ongoing)
setOnlyAlertOnce
(boolean onlyAlertOnce)
setPriority
setProgress
(int max, int progress, boolean indeterminate)
setSmallIcon
(int icon, int level)
setSmallIcon
(int icon)
( sound, int streamType)
setSubText
( tickerText,
( tickerText)
setUsesChronometer
(boolean b)
setVibrate
(long[] pattern)
(long when)最近需要实现一个自定义Notification的功能。网上找了找代码,解决方案就是通过RemoteViews来实现。但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个总结,希望大家能少走点弯路。
RemoteViews 自定义View
这是最基础的知识点,虽然做过自定义通知的应该都清楚,但我觉得还是有必要带一下。它主要被用于AppWidget和Notification,它描述一个在其它进程中显示的View。以下是例子代码。从中我们可以看到RemoteViews提供了一些方法来改变它的子View的值,如设置TextView的文字等。
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.view_notification_type_0);
remoteViews.setTextViewText(R.id.title_tv, title);
remoteViews.setTextViewText(R.id.content_tv, content);
remoteViews.setTextViewText(R.id.time_tv, getTime());
remoteViews.setImageViewResource(R.id.icon_iv, R.drawable.logo);
remoteViews.setInt(R.id.close_iv, "setColorFilter", getIconColor());
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(NOTICE_ID_KEY, NOTICE_ID_TYPE_0);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
int requestCode = (int) SystemClock.uptimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.notice_view_type_0, pendingIntent);
int requestCode1 = (int) SystemClock.uptimeMillis();
Intent intent1 = new Intent(ACTION_CLOSE_NOTICE);
intent1.putExtra(NOTICE_ID_KEY, NOTICE_ID_TYPE_0);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, requestCode1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.close_iv, pendingIntent1);
这里有几点需要注意的。
这个方法被用来调用子View中需要一个Int型参数的方法。如下面这句代码,调用了id为close_iv的setColorFilter方法,参数为getIconColor()的返回值。
设置不同区域的点击PendingIntent
默认的Notification只能通过setContentIntent设置整体的点击事件。不过通过RemoteViews我们可以设置不同地方不同的点击事件,当然这里的事件指的是PendingIntent。如下,设置了点击R.id.notice_view_type_0打开一个Activity,而点击R.id.close_iv会发出一个广播,可以通过这个广播的广播接收器来做一些事情,如这里是关闭当前的Notification。另外还可以打开一个Service。
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.notice_view_type_0, pendingIntent);
int requestCode1 = (int) SystemClock.uptimeMillis();
Intent intent1 = new Intent(ACTION_CLOSE_NOTICE);
intent1.putExtra(NOTICE_ID_KEY, NOTICE_ID_TYPE_0);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, requestCode1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.close_iv, pendingIntent1);
设置通知的自定义View
以上我们得到了自定义的RemoteViews。通过下面这段代码就能生成自定义View的Notification,注意这里使用了setContent()方法。这是网上自定义Notification都会使用的方法。
Notification notification = new NotificationCompat.Builder(context).setContent(remoteViews).build();
但是它会有一个问题。
通过setContent()方法获得的Notification是定高的。如果View的高度比默认高度要大的话,就有一部分显示不出来。如下图
默认情况下通知高度为64dp,当然Rom不同可能会有些区别。一般文字在小于两行的情况下都是可以显示。
那么如何做到wrap_content。需要使用一些黑科技。如下:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
if(android.os.Build.VERSION.SDK_INT &= 16) {
notification = builder.build();
notification.bigContentView = remoteViews;
notification.contentView = remoteViews;
为了理解以上代码,我们需要明确一个我们很容易忽略的问题,那就是通知是可以展开和收起的。请看以下两张图片。同样是网易云音乐的通知,图一比图二要大一些。其实图一展示的是网易云音乐通知的展开状态,使用两个手指上滑就可以缩起,也就是图二。
在上面的代码中我们分别设置了bigContentView 这是展开的自定义视图,而contentView则是收起时的视图。
注意bigContentView是在sdk16时引入的,所以需要判断一下。如果小于sdk16则只能定高了。
注意bigContentView 的最大高度是256dp
注意bigContentView和contentView的设置不能调转顺序,亲测这样会让contentView不显示。
另外需要注意某些Rom可能不支持展开收起通知,在设置了BigContentView之后就只显示展开的视图,而默认情况下只展示收起视图。如魅族的FlyMe,其它Rom并没有测试,如果读者知道可以分享一下。
背景色适配
不同Rom的通知背景色是不同的,所以在UI上需要注意。 主要分为两种情况。
背景色为有透明度的黑色,如MiUi、FlyMe。
背景色为白色,如原生的5.0之后的Rom、华为部分Rom。
主要有两种方案。
固定背景色
也就是设置一个固定的背景色,文字和icon颜色都可以固定。如下图。这有一个缺点,我们在图中也看到了,那就是某些Rom的Notification会有一个左右的padding,如MIUI的就特别明显,如果固定背景色就会很难看。所以这种方法虽然简答,但是不建议使用。
透明背景色
另一种方法就是让背景透明。那么文字和icon的颜色怎么办呢?很简单,跟随系统的Notification中文字的样式。如下设置了TextView的style为默认通知中info的样式。其它相关Style包括TextAppearance.StatusBar.EventContent.Line2、TextAppearance.StatusBar.EventContent.Info等。
需要注意的一点是Android5.0之后使用了不同的Style名表示通知样式。 我们需要创建一个layout-v21文件夹,并新建一个在5.0之后使用的自定义通知样式。如下同样是设置TextView的style为Info的样式,但我们使用的是style是@android:style/TextAppearance.Material.Notification.Info。
另外如果自定义view中有Icon,那么Icon的颜色也需要适应背景,可以选择一个灰色,如#999999,原生安卓黑色和白色的文字内容颜色都为该值。
或者根据不同的背景色设置不同的颜色,通过上面提到的setInt方法。ImageView的setColorFilter方法可以设置图案颜色为某种纯色。但是目前我还没有找到很好的方法获取默认通知的背景色,如果读者找到了望告知。
以上即为我在自定义Notification中遇到的一些问题以及解决方案。目前还有两点有待进一步补充和完善。
获取默认通知背景色,或者使图标颜色与背景色适配的方案。
不支持Notification展开收起的Rom,目前知道的仅有FlyMe。
示例代码地址
阅读(...) 评论()11726人阅读
Android 遇见的问题(3)
今天做Notification的时候不显示,很是郁闷。android 4.2 酷派手机什么也没提示,华为p9 android 6.0提示log如下: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Icon.getType()' on a null object referencejava.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.drawable.Icon.getType()' on a null object reference
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.processSmallRightIcon(Notification.java:3337)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.applyStandardTemplate(Notification.java:3004)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.applyStandardTemplate(Notification.java:2980)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.makeContentView(Notification.java:3202)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.buildUnstyled(Notification.java:3384)
08-23 10:13:14.676 E/Sync( 4180):at android.app.Notification$Builder.build(Notification.java:3709)
。。。。。。log上可以看出是空指针异常。在android 4.3源码 Notification.Builder中没有发现processSmallRightIcon()方法,应该是android6.0源码中有的方法。看这个方法名应该和smallIcon有关系。代码如下:NotificationManager notificationManager =
(NotificationManager)(mContext.getSystemService(Context.NOTIFICATION_SERVICE));
Notification.Builder builder= new Notification.Builder(mContext);
builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),R.drawable.icon_notification));
builder.setContentTitle(mContext.getText(R.string.glass_update));
String strProgress = mContext.getResources().getString(R.string.update_download_progress)+progress+"%";
builder.setContentText(strProgress);
Notification notification = builder.build();
notificationManager.notify(1, notification)加上设置smallIcon的代码:builder.setSmallIcon(R.drawable.icon_notification);这样测试就没有问题了,Notification可以正常显示。
文章:15篇
阅读:15991
文章:11篇
阅读:190053}

我要回帖

更多关于 华为荣耀v10 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信