error:Android permission denied for window type 2002/2003
1.Android sdk>=23的時(shí)候要注意系統(tǒng)浮選對話框的權(quán)限贮缅。
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系統(tǒng)已經(jīng)有權(quán)限了
alertDialog.window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}
}else {
//繪ui代碼,這里android6.0以下的系統(tǒng)直接繪出即可
? ? 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())) {
? ? ? ? ? ? ? ? //啟動(dòng)Activity讓用戶授權(quán)
? ? ? ? ? ? }
? ? ? ? }
? ? }
@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{//權(quán)限被拒絕} } }
2.Builder.VERSION.SDK>=27 自定義靜態(tài)廣播事件發(fā)生時(shí)候需要設(shè)置setComponent的CompentName
var myIntent:Intent = Intent("廣播xKey");
myIntent.setComponent(ComponentName(packageName,
className))
sendBroadcast(myIntent)