android targetSdkVersion / alertdialog pop error when running on 6.0+

while pop up one alert dialog in service, for sdk <=19, only required permission in manifest 



then set dialog
 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

is success.

but for sdk > 23 android 6.0 ++ , it failed.

we need require permission when your app first time run, then reply permissoin 

if (!Settings.canDrawOverlays(this)) {
                
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                this.startActivity(intent);
                dialogConnectLost.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
} else {
         
         dialogConnectLost.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}

BUT , another way to fix that is keep your targetSdkVersion <23, it will work as on sdk below 23. and 

you just keep old code no need change.   

no matter your buid sdk of project is >23 or not.

this is what targetSdkVersion 's magic effect.

included: build sdk >= 23 , then app could run on android 6.0++, keep target sdk < 23, then app could run like old sdk.

 

你可能感兴趣的:(Android,文章)