【问题标题】:Android Notification bar buttons become not responsiveAndroid 通知栏按钮变得无响应
【发布时间】:2017-11-13 01:14:14
【问题描述】:

我正在开发一个应用程序,我的服务应该始终在后台运行。该服务负责应该始终可见的通知栏。通知栏有 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();
    }
}

所以基本上如果我关闭应用程序并删除启动活动的代码,两个按钮都会按预期工作,但一旦我启动活动,两个按钮都会停止工作。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    好的,我终于解决了我在更改处理按钮按下方式时遇到的问题。这就是我现在得到的,它按预期工作。

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

    请注意,这需要我将以下行添加到我的清单中

    <receiver android:name=".NotificationBarService$NotificationBarButtonListener"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多