没有静态方法canDrawOverlays

时间:2016-10-07 06:52:41

标签: android

我注意到有人使用我的应用报告了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)

canDrawOverlays是API 23+方法,我使用它:

 /** 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);
        }
    }

我在MainActivity中有这个:

checkDrawOverlayPermission();

使用Android 5.1崩溃的设备

我可以确保我的应用程序可以在ANdroid 5.1上运行吗? (API 22及以下)没有这种方法可以从API 23及以上获得?

3 个答案:

答案 0 :(得分:1)

检查运行代码的设备的当前API。如果> = 23,则可以使用代码

if(Build.VERSION.SDK_INT >= 23) {
     // if (!Settings.canDrawOverlays(this)) {
     //
} else {
     // another similar method that supports device have API < 23 
}

答案 1 :(得分:0)

对于较新SDK版本中出现的所有方法,您应使用以下模式:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  // code for Marshmallow (SDK 23) and newer versions
} else {
  // fallback for older versions
}

答案 2 :(得分:0)

使用此

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 {
     }