完善后的全局性dialog代码:
- 添加权限:
<!--动作管理覆盖权限-->
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<!--系统警报窗口-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
- 完善代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 先判断系统是否为6.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // M == 23
// 再判断是否 有了悬浮窗权限
if (! Settings.canDrawOverlays(LoginActivity.this)) {
// 没有此权限,跳转到此权限页面,让用户手动开启
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
// 通过此方法启动权限页面,当用户退出权限页面时再次判断用户是否开启此权限
startActivityForResult(intent,1);
}else{
showDialog();
}
}
}
@Override // 此处再次判断,看用户是否开启“悬浮窗”权限
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(this)) { //没有开启
Toast.makeText(MainActivity.this, "not", Toast.LENGTH_SHORT).show();
} else { // 开启了
Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
}
}
}
}
// 展示全局性 dialog
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
AlertDialog dialog = builder.setMessage("目标位置已更新,是否重新导航?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
/*** 判断是否为8.0,选择性设置Type ***/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // O == 26
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
不完善的全局性dialog的简单使用:(重要的有以下两点)
- new AlertDialog.Builder(getApplicationContext()); 需要用getApplicationContext()
- dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 设置为全局性dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
AlertDialog dialog = builder.setMessage("目标位置已更新,是否重新导航?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
// 设置为全局性dialog
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.setCanceledOnTouchOutside(false);//点击屏幕不消失
dialog.show();
PS:记得在Mianfest中添加以下权限:
<!--动作管理覆盖权限-->
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />
<!--系统警报窗口-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
但是!!有坑!!
第一个: 6.0系统需要用户手动打开 “允许显示在其他应用顶层” 的权限(悬浮窗权限),所以:
- 需要先判断用户是否开启此权限,然后再去展示全局性dialog:
// 先判断系统是否为6.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // M == 23
// 再判断是否 有了此权限
if (! Settings.canDrawOverlays(LoginActivity.this)) {
// 没有此权限,跳转到此权限页面,让用户手动开启
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
// 通过此方法启动权限页面,当用户退出权限页面时再次判断用户是否开启此权限
startActivityForResult(intent,1);
}else{showDialog();}
}
@Override // 此处再次判断,看用户是否开启“悬浮窗”权限
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(this)) { //没有开启
Toast.makeText(MainActivity.this, "not", Toast.LENGTH_SHORT).show();
} else { // 开启了
Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
}
}
}
}
第二个: 8.0系统需要用TYPE_APPLICATION_OVERLAY代替 TYPE_SYSTEM_ALERT:
- 需要判断是否为8.0系统,然后选择性设置type:
private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
AlertDialog dialog = builder.setMessage("目标位置已更新,是否重新导航?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
/*** 判断是否为8.0,选择性设置Type ***/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // O == 26
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
这样就基本ok了,至少现在我这里没问题了,以后在遇到问题再来更新,写的不对的地方还请帮忙指出!
来源:CSDN
作者:小矮子tt
链接:https://blog.csdn.net/qq_38287890/article/details/103668638