android 通知 按钮,Android通知栏微技巧,通知栏按钮变得不那么敏感 左手的ㄟ右手 2022-10-17 10:57 155阅读 0赞 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!!!"); \} \} \}
还没有评论,来说两句吧...