Android开发通知图标问题
我的应用程序中的通知图标遇到了一个奇怪的问题。
我的应用程序通过蓝牙发送和接收数据。当应用程序启动时,它会创建应用程序的通知图标 (icon.png)。然后它会看到没有连接蓝牙设备并将图标更改为(warn.png)。问题是,当顶部状态栏显示 warn.png 时,在持续通知下拉菜单下,它会显示原始图标 (icon.png),并带有文本“未连接蓝牙设备”。当蓝牙设备连接时,状态栏图标会变回原始图标 (icon.png),但在持续通知下,它会显示警告图标,并显示消息“连接已建立”。
下面是我使用的代码:
private void notification_updates(String DISPLAY_TEXT, String ONGOING_TEXT, int ICON) {
Intent intent = new Intent(this,GUI.class);
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_SINGLE_TOP);
try
{
notification.setLatestEventInfo(Monitor.this, "App_Name",ONGOING_TEXT,
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
notification.icon = ICON;
notification.tickerText = DISPLAY_TEXT;
notification.flags = notification.FLAG_ONGOING_EVENT; //on going events
notification.flags += notification.FLAG_NO_CLEAR; //no clear.
mManager.notify(APP_ID, notification);
} catch(Exception e)
{
Log.e(TAG, "Failed to Notifiy the notification manager (create):\n" + e.toString());
}
}
I am running into a strange issue with notification icons in my application.
My application sends and receives data via bluetooth. When the application is started it creates a notification icon of the application (icon.png). It will then see there is no bluetooth device connected and change the icon to (warn.png). The issue is, when the status bar at the top shows warn.png, under the on-going notification drop down it has the origional icon (icon.png) with the text "No Bluetooth Device connected". When a bluetooth device does connect, the status bar icon changes back to the origional icon (icon.png) but under the on-going notification it has the warn icon with the message "Connection Established".
Below is the code I used:
private void notification_updates(String DISPLAY_TEXT, String ONGOING_TEXT, int ICON) {
Intent intent = new Intent(this,GUI.class);
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_SINGLE_TOP);
try
{
notification.setLatestEventInfo(Monitor.this, "App_Name",ONGOING_TEXT,
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
notification.icon = ICON;
notification.tickerText = DISPLAY_TEXT;
notification.flags = notification.FLAG_ONGOING_EVENT; //on going events
notification.flags += notification.FLAG_NO_CLEAR; //no clear.
mManager.notify(APP_ID, notification);
} catch(Exception e)
{
Log.e(TAG, "Failed to Notifiy the notification manager (create):\n" + e.toString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试创建一个全新的通知,并查看是否正确更新内容视图中的图标。
或者,如果这不适合您,请创建自定义内容视图: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView
然后,在 RemoteViews 对象上, setImageViewResource 更新图标, setTextViewText 更新文本,然后将 notification.contentView 设置为您的 RemoteViews 对象。我已经成功地正确更新了状态栏中的图标以及扩展任务栏中的图标/文本。
切线,我注意到你的代码有 notification.flags += notification.FLAG_NO_CLEAR。我相信因为这是一个位掩码你想要 |= 而不是 +=
Try creating a whole new notification, and see if that properly updates the icon in the content view.
Alternatively, if that doesn't do it for you, create a custom content view: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView
Then, on your RemoteViews object, setImageViewResource to update your icon, setTextViewText to update the text, and set the notification.contentView to be your RemoteViews object. I've had success with this properly updating both the icon in the status bar, as well as the icon/text in the expanded task bar.
Tangentially, I noticed your code has notification.flags += notification.FLAG_NO_CLEAR. I believe since this is a bitmask you want |= instead of +=