摘要:通知栏,应始终可见。通知栏有个按钮,其中第一个是抓一些数据存储和第二个开放活动,将显示所有的数据。我遇到了一个问题,当我关闭应用程序,然后按通知按钮开始活动,活动开始后,我通知按钮停止响应。
Android通知栏,应始终可见。通知栏有2个按钮,其中第一个是抓一些数据存储和第二个开放活动,将显示所有的数据。我遇到了一个问题,当我关闭应用程序,然后按通知按钮开始活动,活动开始后,我通知按钮停止响应。需要注意的是按键做工精细前点所在的第二按钮点击开始活动。
这里是一个模板代码为我的通知服务和通知栏按钮处理程序
服务处理的通知栏
public class NotificationBarService extends Service { private int notificationID; @Override public IBinder onBind(Intent intent){ return null; } @Override public int onStartCommand(Intent intent, int flags, int startId){ notificationID = new Random().nextInt(); RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher); contentView.setTextViewText(R.id.title, "Custom notification"); contentView.setTextViewText(R.id.text, "This is a custom layout"); //Handle the button for showing bookmarks on custom notification Intent buttonsIntent2 = new Intent(this, NotificationBarButtonActivityHandler.class); buttonsIntent2.putExtra(PENDING_ACTION, SHOW_BOOKMARKS); contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, PendingIntent.getActivity(this, 0, buttonsIntent2, 0)); //Handle the button for adding bookmark on custom notification Intent buttonsIntent = new Intent(this, NotificationBarButtonActivityHandler.class); buttonsIntent.putExtra(PENDING_ACTION, REGISTER_BOOKMARK); contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, PendingIntent.getActivity(this, 1, buttonsIntent, 0)); RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.custom_notification); Intent switchIntent = new Intent(this, NotificationBarService.class); PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0); notificationView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, pendingSwitchIntent); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setContent(contentView) .setSmallIcon(R.drawable.notification_small_icon) .setOngoing(true); Notification notification = mBuilder.build(); startForeground(notificationID, notification); return START_STICKY; } @Override public void onDestroy(){ super.onDestroy(); stopForeground(true); } }
类处理按钮按下通知栏
public class NotificationBarButtonActivityHandler extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String action = (String) getIntent().getExtras().get(NotificationBarService.PENDING_ACTION); if (action != null) { if (action.equals(NotificationBarService.REGISTER_BOOKMARK)){ CustomLogger.log("---------------- BUTTON FOR COLLECT DATA WAS PRESSED!!!"); //Does something here } else if(action.equals(NotificationBarService.SHOW_BOOKMARKS)){ CustomLogger.log("---------------- BUTTON FOR SHOW DATA WAS PRESSSED!!!"); //Notification bar buttons start not responding right after //this is executed. Note that this problem only occurs if I close the app //and press the notification button to execute this code. //Otherwise this works just fine. Intent intent2; intent2 = new Intent(this, BookmarkDisplayActivity.class); startActivity(intent2); } } finish(); } }
最后,我终于解决了这个问题,我在改变,我处理按键的方式。这是我现在的工作。
在NotificationBarService,可以这样处理按钮
Intent addBookmarkIntent = new Intent(this, NotificationBarButtonListener.class); addBookmarkIntent.setAction(ADD_BOOKMARK_ACTION); PendingIntent pendingAddBookmarkIntent = PendingIntent.getBroadcast(this, 0, addBookmarkIntent, 0); contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, pendingAddBookmarkIntent); Intent showBookmarkIntent = new Intent(this, NotificationBarButtonListener.class); showBookmarkIntent.setAction(SHOW_BOOKMARK_ACTION); PendingIntent pendingShowBookmarkIntent = PendingIntent.getBroadcast(this, 0, showBookmarkIntent, 0); contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, pendingShowBookmarkIntent);
然后我收到广播甚至和处理这样的
public static class NotificationBarButtonListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if(action.equals(ADD_BOOKMARK_ACTION)){ CustomLogger.log("---------------- BUTTON FOR REGISTER BOOKMARK WAS PRESSED!!! "); } else if(action.equals(SHOW_BOOKMARK_ACTION)){ CustomLogger.log("---------------- BUTTON FOR SHOW BOOKMARK WAS PRESSSED!!!"); } } }
原文地址:http://www.apkbus.com/blog-92...
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/14072.html
摘要:修改记录版本的通知栏消息功能上并未发生变化,右上角的缩减为了。增加了,允许可穿戴设备远程控制通知栏消息。锁屏状态下,可以控制通知栏消息的隐私程度。但是谷歌规定,自定义布局展示的通知栏消息最大高度是。具体适配不正常的机型有。 此文已由作者黎星授权网易云社区发布。 欢迎访问网易云社区,了解更多网易技术产品运营经验。 由于历史原因,Android在发布之初对通知栏Notification的设...
摘要:酷炫即时通讯和开源项目汇总这是一个整理即时通讯和社交系统优秀开源项目的文档,项目上传欢迎提交更新。 UI之可折叠的TextView 先上效果 一、思路 1. 计算text的行数 实现可折叠的TextView最重要的一点是在setText()前计算出text所需的行数计算行数需要分为两种情况 1.1 没有换行符的text 行数等于text的宽度除于TextView的宽度 再判断text的...
摘要:加上的通知还能提升进程优先级,大有被滥用的趋势。自己保活不了不算,还要别人拉起来帮忙通知后台起另外一个程序的用法也被比如华为系统限制。通知栏不显示高版本的原生系统自带通知栏显示管理功能,默认是允许显示。接下来我想聊一聊通知栏的跳转设计。 我即将在2017.7.8号开一个直播讲堂,感兴趣的同学点击快来参加吧:https://segmentfault.com/l/15...内容包括: ...
摘要:上列表左右滑动开源组件掘金是一款用于为上的排布提供左滑右滑操作的库。这里我贴出最终的兼容方案教你用两层嵌套实现三级展示界面掘金最近项目中使用一个三级展示列表,要求第一级和第二季都可以折叠,并有不同的图标变换,第三层展示数据,可点击。 八年 Android 开发,看我如何简化 Android 的 UI 开发! - Android - 掘金作者 : Super Mary 校对者: Zhao...
阅读 1056·2023-04-25 19:10
阅读 1522·2021-10-08 10:04
阅读 957·2021-09-10 10:50
阅读 2856·2021-09-02 15:21
阅读 1263·2019-08-30 15:52
阅读 1574·2019-08-30 13:56
阅读 1910·2019-08-30 12:53
阅读 1724·2019-08-28 18:22