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?