Android的自定义通知不显示 [英] Android Custom Notification doesn't Display

查看:167
本文介绍了Android的自定义通知不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法得到一个通知,其中一个自定义布局来显示。如果我用下面的(旧)code:

 的通知=新的通知();
notification.icon = R.drawable.icon;
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis的();
notification.setLatestEventInfo(背景下,(CharSequence的)currentTitle,(CharSequence的),pendingIntent);
 

一切按预期工作的通知显示出来。但是,如果我用下面的(新)code:

 内容查看=新RemoteViews(context.getPackageName(),R.layout.service_notification);
contentView.setImageViewResource(R.id.image,R.drawable.icon);
contentView.setTextViewText(R.id.title,(CharSequence中)currentTitle);
contentView.setTextViewText(R.id.text,(CharSequence的));
contentView.setViewVisibility(R.id.pgStatus,View.GONE);

通知=新通知();
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis的();

notification.contentView =内容查看;
notification.contentIntent = pendingIntent;
 

的通知永远不会​​显示。我没有给出任何错误,所以我真的不知道到哪里寻找问题。这里是service_notification布局code:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /布局
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=10dp>
    < ImageView的机器人:ID =@ + ID /图像
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_alignParentLeft =真
        机器人:layout_marginRight =10dp/>
    < TextView的机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_toRightOf =@ ID /图像
        机器人:文本=这是冠军。
        风格=@风格/ NotificationTitle/>
    < TextView的机器人:ID =@ + ID /文
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_toRightOf =@ ID /图像
        机器人:layout_below =@ ID /标题
        机器人:文本=这是状态
        风格=@风格/ NotificationText/>
    <进度
        机器人:ID =@ + ID / pgStatus
        风格=机器人:ATTR / progressBarStyleHorizo​​ntal
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID /文
        机器人:layout_toRightOf =@ + ID /图片/>
< / RelativeLayout的>
 

另外,我在Android 2.2模拟器中运行这个,如果有差别。

任何人有什么可能是错误的任何想法或如何我能去追查问题?我有点茫然,因为我没有一条错误信息工作过的。谢谢!

解决方案
  

一个状态栏通知,要求所有如下:

     
      
  • 状态栏图标
  •   
  • 标题和消息,除非你定义一个自定义通知布局
  •   
  • 系统PendingIntent,当选择该通知被解雇
  •   

的<一个href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateANotification">Status栏通知 - 创建通知

您没有指定一个图标,这意味着您的通知是无效的。

(但是,是的,这是不好的框架端没有正确的错误消息/例外这种情况下)


编辑:刚刚测试此通过,我可以证实,从上Android 2.2的注释中的隐形的通知。它适用于2.3.3,好像你在一个错误迷迷糊糊这已被固定在其间。

有问题的线以下对我来说:

  contentView.setViewVisibility(R.id.pgStatus,View.GONE);
 

如果我评论说出来,一切正常。它也可以当我添加安卓知名度=水涨船高的进度在XML布局,具有基本相同的效果。但只要我称之为 setViewVisibility()上吧,一切会崩解。

所以,我建议你从错误追踪意见的解决办法,包裹进度成的LinearLayout和改变的可见性来代替。

I'm having trouble getting a notification with a custom layout to display. If I use the following (old) code:

notification = new Notification();
notification.icon = R.drawable.icon;
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis();
notification.setLatestEventInfo(context, (CharSequence)currentTitle, (CharSequence)"", pendingIntent);

Everything works as expected and the notification shows up. However if I use the following (new) code:

contentView = new RemoteViews(context.getPackageName(), R.layout.service_notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.title, (CharSequence)currentTitle);
contentView.setTextViewText(R.id.text, (CharSequence)"");
contentView.setViewVisibility(R.id.pgStatus, View.GONE);

notification = new Notification();
notification.tickerText = currentTickerText;
notification.when = System.currentTimeMillis();

notification.contentView = contentView;
notification.contentIntent = pendingIntent;

The notification is never shown. I'm not given an error either so I'm not really sure where to look for an issue. Here is the service_notification layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:text="This is the title."
        style="@style/NotificationTitle" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        android:text="This is the status"
        style="@style/NotificationText" />
    <ProgressBar
        android:id="@+id/pgStatus"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_toRightOf="@+id/image" />
</RelativeLayout>

Also, I'm running this on the Android 2.2 emulator if that makes a difference.

Anyone have any ideas of what could be wrong or how I could go about tracking down the issue? I'm kind of at a loss since I don't have an error message to work off of. Thanks!

解决方案

A status bar notification requires all of the following:

  • An icon for the status bar
  • A title and message, unless you define a custom notification layout
  • A PendingIntent, to be fired when the notification is selected

from Status Bar Notifications - Creating Notifications

You don't specify an icon, which means your notification is not valid.

(But yes, it's bad on the framework side that there is no proper error message/exception for this case)


Edit: Just tested this through and I can confirm the "invisible" notification from the comment on Android 2.2. It works on 2.3.3., seems like you stumbled over a bug that has been fixed in the meantime.

The problematic line is the following for me:

contentView.setViewVisibility(R.id.pgStatus, View.GONE);

If I comment that out, everything works. It also works when I add android:visibility="gone" to the ProgressBar in the XML layout, which has basically the same effect. But as soon as I call setViewVisibility() on the bar, everything breaks apart.

So I'd recommend the workaround from the bugtracker comments, wrap the ProgressBar into a LinearLayout and change the visibility of that instead.

这篇关于Android的自定义通知不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆