【问题标题】:How to collapse notification panel in android programmatically [closed]如何以编程方式折叠android中的通知面板[关闭]
【发布时间】:2021-08-06 09:40:58
【问题描述】:

我正在开发一个屏幕锁定应用程序。我正在为我的主要储物柜屏幕使用屏幕覆盖,我面临的问题是每当我下拉通知面板并单击任何破坏我的主要储物柜活动的通知时。

所以我需要一个禁用下拉功能的代码。

我已经尝试过这段代码和许多其他代码,但没有成功

private static void collpasePanel(Context _context) {
try {
    Object sbservice = _context.getSystemService("statusbar");
    Class<?> statusbarManager;
    statusbarManager = Class.forName("android.app.StatusBarManager");
    Method showsb;
    if (Build.VERSION.SDK_INT >= 17) {
        showsb = statusbarManager.getMethod("collapsePanels");
    } else {
        showsb = statusbarManager.getMethod("collapse");
    }
    showsb.invoke(sbservice);
} catch (ClassNotFoundException _e) {
    _e.printStackTrace();
} catch (NoSuchMethodException _e) {
    _e.printStackTrace();
} catch (IllegalArgumentException _e) {
    _e.printStackTrace();
} catch (IllegalAccessException _e) {
    _e.printStackTrace();
} catch (InvocationTargetException _e) {
    _e.printStackTrace();
}

【问题讨论】:

    标签: java android kotlin notifications system


    【解决方案1】:

    我发现我的查询的解决方案效果很好

    val handler = Handler()
            val r: Runnable = object : Runnable {
                override fun run() {
    
                    val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
                    sendBroadcast(it)
    
                    handler.postDelayed(this, 1)
                }
            }
    
            handler.postDelayed(r, 1)
    

    但这不是正确的解决方案。

    【讨论】:

      【解决方案2】:
       Button btnCreateNotification = findViewById(R.id. btnCreateNotification ) ;
            btnCreateNotification.setOnClickListener( new View.OnClickListener() {
               @Override
               public void onClick (View v) {
                  Intent notificationIntent = new Intent(MainActivity. this, MainActivity. class );
                  notificationIntent.addCategory(Intent. CATEGORY_LAUNCHER );
                  notificationIntent.setAction(Intent. ACTION_MAIN );
                  notificationIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP ) ;
                  PendingIntent resultIntent = PendingIntent. getActivity (MainActivity. this, 0 , notificationIntent , 0 ) ;
                  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity. this, default_notification_channel_id )
                     .setSmallIcon(R.drawable. ic_launcher_foreground )
                     .setContentTitle( "Test" )
                     .setContentIntent(resultIntent)
                     .setStyle( new NotificationCompat.InboxStyle())
                     .setContentText( "Hello! This is my first push notification" ) ;
                  NotificationManager mNotificationManager = (NotificationManager)
                  getSystemService(Context. NOTIFICATION_SERVICE ) ;
                  if (android.os.Build.VERSION. SDK_INT >= android.os.Build.VERSION_CODES. O ) {
                     int importance = NotificationManager. IMPORTANCE_HIGH ;
                     NotificationChannel notificationChannel = new
                     NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
                     mBuilder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
                     assert mNotificationManager != null;
                     mNotificationManager.createNotificationChannel(notificationChannel) ;
                  }
                  assert mNotificationManager != null;
                  mNotificationManager.notify(( int ) System. currentTimeMillis () ,
                  mBuilder.build()) ;
               }
            }) ;
         }
      }
      

      参考:https://www.tutorialspoint.com/how-to-close-the-notification-panel-after-notification-button-is-clicked-in-android

      【讨论】:

      • 那是行不通的
      猜你喜欢
      • 2014-10-25
      • 2022-11-10
      • 1970-01-01
      • 2021-03-17
      • 2021-01-17
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多