error:Android permission denied for window type 2002/2003
1.Android sdk>=23的时候要注意系统浮选对话框的权限。
A.方案
var sysytemAlert:AlertDialog.Builder = AlertDialog.Builder(context);
sysytemAlert.setTitle("System AlertDialog");
sysytemAlert.setMessage("this is system alertDialog,Close me please \"ok\" btn");
sysytemAlert.setCancelable(false);
sysytemAlert.setPositiveButton("OK"){dialog, which-> Toast.makeText(context,"come on",Toast.LENGTH_LONG).show()}
if (Build.VERSION.SDK_INT >=23) {
if (!Settings.canDrawOverlays(context)) {
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
context?.startActivity(intent)
return
}else {
//绘ui代码, 这里说明6.0系统已经有权限了
alertDialog.window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}
}else {
//绘ui代码,这里android6.0以下的系统直接绘出即可
alertDialog.window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.show();
B.方案
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(!Settings.canDrawOverlays(getApplicationContext())) {
//启动Activity让用户授权
}
}
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data){super.onActivityResult(requestCode,resultCode,data);
if(requestCode== 100)
{if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {if(Settings.canDrawOverlays(this))
{
}
else{//权限被拒绝} } }
2.Builder.VERSION.SDK>=27 自定义静态广播事件发生时候需要设置setComponent的CompentName
var myIntent:Intent = Intent("广播xKey");
myIntent.setComponent(ComponentName(packageName,
className))
sendBroadcast(myIntent)
网友评论