1

I want to show a floating view when the user gets a phone call or a text message. But overlay permission check in broadcastReceiver returns false with built apk on Android 13.

I use BroadcastReceiver with RxWorker (more than 12) and Service (less than 12) to get a caller information. In Worker or Service, when you success to get a information, you call WindowManager.addview(mView). When the app is first launched, the user has already granted the permission to draw overlays.

        <receiver
            android:name=".receiver.PhoneCallReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>

In this receiver, I check SYSTEM_ALERT_WINDOW permission by using Settings.canDrawOverlays(context). When I run this in Android Studio, it works well. However, permission is not checked when built with apk only on Android 13. Returns false even though you have permission. Sometimes, an error occurs when calling addView within Worker even if the permission is correctly confirmed on the broadcast!

"android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7c33a48 -- permission denied for window type 2038"

        <receiver
            android:name=".receiver.MessageReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

When receiving a phone call or text message, WindowManager.addView is called within the same worker, but the view is normally displayed when receiving a text message, and there is no permission when receiving a phone call. For your information, I'm testing with Samsung Galaxy S21.

Is there any solution?

Ars_Codicis
  • 440
  • 1
  • 4
  • 16
sun
  • 11
  • 3

2 Answers2

0

Asking permission when a call is received harms the user experience, but I tested it with reference to Fazle Rabbi's answer. Since the context within the broadcast is not an activity context, a separate activity was created to request permission. (The code has been modified. Check to allow permissions with Settings.canDrawOverlays() instead of comparing with result.resultCode.)

@RequiresApi(Build.VERSION_CODES.R)
class PermissionActivity : AppCompatActivity() {

    private val requestOverlayPermission = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) {
        if (Settings.canDrawOverlays(this)) {
            //Permission granted

            /*
            do something
             */
            
        } else {
            //Permission denied
        }

        finish()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_pemission)

        val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
        intent.data = Uri.parse("package:$packageName")
        requestOverlayPermission.launch(intent)
    }
}

In BroadcastReceiver,

if (!Settings.canDrawOverlays(ctx)) {
   val i = Intent(ctx,PermissionActivity::class.java)
   i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
   ctx.startActivity(i)

   return
}

Settings.canDrawOverlays returns false and goes to the Permission Request page whenever BroadcastReceiver is called, even if the app's settings confirm that it has overlay privileges. In addition, permissions are still disabled on the moved overlay permission request page.

It's really weird. Funny how it's normal when it's running on Android studio. It is well confirmed that there is also permission. This only happens when executed with the built APK On Android 13! The problem seems to occur not only on one particular device, but on most Samsung phones updated with Android 13. Did I miss anything?

sun
  • 11
  • 3
0

It's an issue only with Android 13 Samsung phones.

I also contacted Samsung about the issue. And they replied:

they have SamsungRestrictOverlayProcessor. and Overlay-related permissions are temporarily disabled during calls for applications that have not been installed through an official store (Play store / Galaxy store) or ADB

so you need to distribute and test your application through proper stores.

I hope this was helpful to you.

이종효
  • 11
  • 2
  • I have now confirmed your answer. When I installed my app as an APK installation app in the Play Store, the permission was confirmed normally. I thought there was a special task when installing the app. I understood it as the answer you got from asking Samsung. The answer is to distribute the app officially. Thank you for your answer! – sun Feb 24 '23 at 01:12