问题描述
我无法遇到与自定义布局显示的通知.如果我使用以下(旧)代码:
notification = new Notification(); notification.icon = R.drawable.icon; notification.tickerText = currentTickerText; notification.when = System.currentTimeMillis(); notification.setLatestEventInfo(context, (CharSequence)currentTitle, (CharSequence)"", pendingIntent);
一切按预期工作,通知显示出来.但是,如果我使用以下(新)代码:
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;
通知永远不会显示.我没有给出错误,所以我真的不确定寻找问题的地方.以下是service_notification布局代码:
<?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>
此外,我在android 2.2仿真器上运行它,如果这会有所不同.
任何人都对可能是错误的任何想法或如何追踪问题的方法?我有点损失,因为我没有错误的消息来解决问题.谢谢!
推荐答案
状态栏通知需要以下所有内容:
- 状态栏的图标
- 标题和消息,除非您定义自定义通知布局
- 一个挂起,在选择通知时被解雇
您未指定图标,这意味着您的通知无效.
(但是,在框架方面对此情况没有适当的错误消息/异常而言,这是不好的)
编辑:刚刚测试过这一点,我可以从Android 2.2上的评论中确认"不可见"通知.它适用于2.3.3.似乎你偶然发现了超过一个错误已在此处修复.
有问题的线对我来说是如下:
contentView.setViewVisibility(R.id.pgStatus, View.GONE);
如果我评论出来,一切都有效.当我在XML布局中添加android:visibility="gone"到ProgressBar时,它也有效,这基本上存在效果.但是一旦我打电话给栏上的setViewVisibility(),一切都崩溃了.
所以我推荐从BugTracker注释中的解决方法,将ProgressBar包装到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.