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 

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

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.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章