Ninad7N
Ninad7N

Reputation: 726

Close notification drawer in android 12 CRASH

I am trying to open a activity in broadcast receiver but the notification drawer's not closing. as ACTION_CLOSE_SYSTEM_DIALOGS is deprecated the app is crashing, is there any other solution for this?

nLServiceReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        println("BroadcastReceiver----${intent.action}---------------/--/-/-/-/-/-/-/-")
        Handler(Looper.getMainLooper()).postDelayed({
            currentAppActivityList.clear()
            if(SDK_INT < 31){
                val closeIntent = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
                applicationContext.sendBroadcast(closeIntent)
            }
                      startActivity(applicationContext.packageManager.getLaunchIntentForPackage("${intent.action}"))
        }, 1000)

    }
}

on notification there are different buttons (dynamic), every button defines a different app. so if i tap on the button, it should open its assigned application.

ive tried providing below in the pending intent

val intent1 = Intent(context.packageManager.getLaunchIntentForPackage(appInfo!![i].packageName))
val pendingIntent1 = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_IMMUTABLE)

but wanted to do some operations before application launch, so i added broadcast reciever.

val pendingIntent1 = PendingIntent.getBroadcast(this, 0, Intent(appInfo!![i].packageName), PendingIntent.FLAG_IMMUTABLE)

its working fine but notification drawer is not closing.

Upvotes: 0

Views: 492

Answers (1)

snachmsm
snachmsm

Reputation: 19263

question is about crash, where is it? don't you think that this is crucial for solving your issue? put some log, exception stacktrace...

but I know what Exception you are getting: SecurityException. Thats because you are using ACTION_CLOSE_SYSTEM_DIALOGS and straight in doc you can find:

This constant was deprecated in API level 31.

This intent is deprecated for third-party applications starting from Android Build.VERSION_CODES#S for security reasons. Unauthorized usage by applications will result in the broadcast intent being dropped for apps targeting API level less than Build.VERSION_CODES#S and in a SecurityException for apps targeting SDK level Build.VERSION_CODES#S or higher. Instrumentation initiated from the shell (eg. tests) is still able to use the intent. The platform will automatically collapse the proper system dialogs in the proper use-cases. For all others, the user is the one in control of closing dialogs.

so:

  • now only user or system can close notification drawer
  • always read/post exception stacktrace
  • read the doc

Upvotes: 1

Related Questions