Android通知栏微技巧,通知栏按钮变得不那么敏感

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!!!");


            }


        }
    }
```


原文地址: android开发http://www.apkbus.com/blog-927916-76546.html
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Android通知工具是指用于管理和展示Android设备上通知应用程序或服务。通知Android系统中用于显示各种通知的一个区域,用户可以从通知中查看和操作通知消息。 通知工具通常具有以下功能: 1. 通知管理:可以管理并展示来自各个应用程序的通知消息,包括短信、电话、社交媒体等各种类型的通知。用户可以通过通知工具集中查看和处理这些通知,提高管理效率。 2. 快捷操作:通知工具可以提供一些快捷操作,例如回复短信、拒接来电、播放音乐等,用户可以直接在通知中完成这些操作,无需打开具体的应用程序。 3. 状态显示:通知工具可以显示设备的一些状态信息,例如电量、网络连接状态、音量等。用户可以通过通知随时查看这些状态信息,方便实时监控设备的运行情况。 4. 个性化设置:通知工具通常支持各种个性化设置,用户可以自定义通知的样式、主题、通知显示方式等,以满足个人需求和喜好。 5. 通知过滤:通知工具可以根据用户的设置对通知进行过滤,屏蔽一些不重要的通知或者将其归类整理。这样可以减少通知的干扰,提高通知的有效性。 总之,Android通知工具为用户提供了一个方便管理和操作通知的工具,帮助用户更高效地处理来自各个应用程序的通知消息,使得使用Android设备更加便捷和舒适。 ### 回答2: Android通知工具是一个用于在Android设备上显示通知的功能。通知工具可以在应用程序运行时发送各种类型的通知,包括文本、图标、声音、振动等。通过使用通知工具,用户可以接收到来自应用程序的重要信息和提示,而无需打开应用程序。 通知工具可以帮助应用程序在后台运行或用户退出应用程序后继续发送通知。它提供了一种简单而高效的方式来向用户发送通知,帮助用户及时了解到关键信息。 通知工具还可以支持用户与通知进行交互。用户可以通过点击通知来打开相关的应用程序或执行其他操作。此外,通知工具还提供了通知的展示方式的设置选项,用户可以根据自己的偏好设置通知的显示样式。 在Android开发中,通知工具是一个很重要的模块。开发人员可以通过使用Android提供的通知相关的API,轻松地创建和发送通知。他们可以定义通知的标题、内容、图标、声音等属性,并设置通知的行为和交互。 总的来说,Android通知工具为应用程序提供了一种方便、快捷的方式来与用户进行沟通和交互。通过使用通知工具,应用程序可以将重要信息推送给用户,并提供与用户的实时互动。这使得Android设备的用户体验更加完善和高效。 ### 回答3: Android通知工具是一种用于在Android设备上显示通知的工具。在Android操作系统中,通知是一个重要的功能,它可以显示各种通知,如消息、提醒、更新等。通知工具提供了一种便捷的方式来管理和显示这些通知通知工具可以帮助用户及时了解到重要的信息,无论是收到短信、电话、邮件,还是其他应用通知,都可以通过通知工具进行展示。用户可以通过下拉通知来查看通知的详细内容,并进行相应的操作,例如回复短信、接听电话、打开邮件等。 通知工具还可以提供一些额外的功能,如展示音乐播放器控制按钮、显示天气信息、提供快捷操作按钮等。这些功能可以提升用户体验,让用户方便地控制和获取信息。 通知工具还支持通知的管理和设置。用户可以根据自己的喜好和需求,对通知进行分类、过滤和优先级设置。用户还可以通过设置来自定义通知的样式、铃声和震动模式等。 总之,Android通知工具是一种非常实用的工具,它可以帮助用户及时了解到各种通知,并提供了方便的操作和管理功能。通过使用通知工具,用户可以更好地掌控自己的Android设备和获取重要的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫非有道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值