Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No static method canDrawOverlays

Tags:

android

I've noticed someone who is using my app reported a crash which logged by the Google Developer Console:

java.lang.NoSuchMethodError: No static method canDrawOverlays(Landroid/content/Context;)Z in class Landroid/provider/Settings; or its super classes (declaration of 'android.provider.Settings' appears in /system/framework/framework.jar)
    at com.pack.MainActivity.checkDrawOverlayPermission(MainActivity.java:311)
    at com.pack.MainActivity.onCreate(MainActivity.java:127)
    at android.app.Activity.performCreate(Activity.java:6033)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5268)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

The canDrawOverlays is an API 23+ method and I Use it like that:

 /** code to post/handler request for permission */
    public final static int REQUEST_CODE = 100; /*(see edit II)*/

    @SuppressLint("NewApi")
    public void checkDrawOverlayPermission() {
        /** check if we already  have permission to draw over other apps */
        if (!Settings.canDrawOverlays(this)) {
            /** if not construct intent to request permission */
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            /** request permission via start activity for result */
            startActivityForResult(intent, REQUEST_CODE);
        }
    }

And I have this in my MainActivity:

checkDrawOverlayPermission();

The device which crashed using Android 5.1

HOw I can make sure my app will work on ANdroid 5.1? (API 22 and below) who don't have this method which available from API 23 and up?

like image 251
TheUnreal Avatar asked Sep 07 '25 11:09

TheUnreal


2 Answers

Check the current API of the device which runs your code. If it >= 23, you can use the code

if(Build.VERSION.SDK_INT >= 23) {
     // if (!Settings.canDrawOverlays(this)) {
     //
} else {
     // another similar method that supports device have API < 23 
}
like image 101
Linh Avatar answered Sep 10 '25 01:09

Linh


Use this

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

       if (!Settings.canDrawOverlays(this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        /** request permission via start activity for result */ 
        startActivityForResult(intent, REQUEST_CODE);
    } 

    } else {
     }
like image 21
Keyur Thumar Avatar answered Sep 10 '25 02:09

Keyur Thumar