Java 如何以编程方式折叠android中的通知面板

Java 如何以编程方式折叠android中的通知面板,java,android,kotlin,notifications,system,Java,Android,Kotlin,Notifications,System,我正在开发一个屏幕锁定应用程序。我在主储物柜屏幕上使用屏幕覆盖,我面临的问题是每当我放下通知面板并单击任何破坏我的主储物柜活动的通知时 所以我需要一个禁用下拉功能的代码 我尝试过这个代码和其他许多代码,但没有成功 private static void collpasePanel(Context _context) { try { Object sbservice = _context.getSystemService("statusbar"); Class&

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

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

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

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();
}
private static void collpapsepanel(上下文\u上下文){
试一试{
对象sbservice=_context.getSystemService(“statusbar”);
班级经理;
statusbarManager=Class.forName(“android.app.statusbarManager”);
方法b;
如果(Build.VERSION.SDK_INT>=17){
showsb=statusbarManager.getMethod(“collapsePanels”);
}否则{
showsb=statusbarManager.getMethod(“崩溃”);
}
showsb.invoke(sbservice);
}捕获(ClassNotFoundException\u e){
_e、 printStackTrace();
}catch(NoSuchMethodException){
_e、 printStackTrace();
}捕获(IllegalArgumentException){
_e、 printStackTrace();
}捕获(非法访问例外){
_e、 printStackTrace();
}捕获(调用TargetException){
_e、 printStackTrace();
}

我找到了我的查询的解决方案,效果很好

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)
但这不是正确的解决办法

 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()) ;
         }
      }) ;
   }
}

参考资料:

这不起作用