android M SYSTEM_ALERT_WINDOW权限问题
来源:互联网 发布:mysql if else 编辑:程序博客网 时间:2024/06/10 21:27
public class ForceOfflineReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); dialogBuilder.setTitle("Warning"); dialogBuilder.setMessage("You are forced to be offline.Please try to login again,"); dialogBuilder.setCancelable(true); dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { forceAllActivity(context); } }); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//因为dialog需要依附于activity显示,但是我们现在是在receiver中所以不能正常显示,我们可以通过TYPE_SYSTEM_ALERT将其声明成系统提示框然后就可以正常显示了,但是需要在manifest里面进行权限声明 alertDialog.show(); } private void forceAllActivity(final Context context){ ActivityCollector.finishAll(); Intent intent = new Intent(context, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//在非activity中启动一个activity需要添加一个flag让其运行在一个新的堆栈中 context.startActivity(intent); }}
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
在android M 以前,这样做是可以正常显示的,但是在android M 后运行就会报错,说我们没有权限,官方解释如下:
SYSTEM_ALERT_WINDOWAdded in API level 1String SYSTEM_ALERT_WINDOWAllows an app to create windows using the type TYPE_SYSTEM_ALERT, shown on top of all other apps. Very few apps should use this permission; these windows are intended for system-level interaction with the user.Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action ACTION_MANAGE_OVERLAY_PERMISSION. The app can check whether it has this authorization by calling Settings.canDrawOverlays().Protection level: signatureConstant Value: "android.permission.SYSTEM_ALERT_WINDOW"
ACTION_MANAGE_OVERLAY_PERMISSIONAdded in API level 23String ACTION_MANAGE_OVERLAY_PERMISSIONActivity Action: Show screen for controlling which apps can draw on top of other apps.In some cases, a matching Activity may not exist, so ensure you safeguard against this.Input: Optionally, the Intent's data URI can specify the application package name to directly invoke the management GUI specific to the package name. For example "package:com.my.app".Output: Nothing.Constant Value: "android.settings.action.MANAGE_OVERLAY_PERMISSION"
即我们需要用户的再次确认才可以使用这个权限,再次确认的方式有两种
一、用户手动确认:Settings – Apps – setting – Draw over other apps – 选择需要权限的app – enable
二、代码中动态跳转到相应界面,让用户开启
public static int OVERLAY_PERMISSION_REQUEST_CODE = 8888; public void requestDrawOverLays() { if (!Settings.canDrawOverlays(LoginActivity.this)) { //没有权限,给出提示,并跳转界面然用户enable Toast.makeText(this, "can not DrawOverlays", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + LoginActivity.this.getPackageName())); startActivityForResult(intent, OVERLAY_PERMISSION_REQUEST_CODE); } else { //有权限,正常逻辑走起 Toast.makeText(this, "Permission Allowed", Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == OVERLAY_PERMISSION_REQUEST_CODE) { if (!Settings.canDrawOverlays(this)) { // 还是没权限,说明用户不想给你这个权限,可以弹框说明 Toast.makeText(this, "Permission Denieddd by user.Please Check it in Settings", Toast.LENGTH_SHORT).show(); } else { //用户已经给了权限,正常逻辑走起 Toast.makeText(this, "Permission Allowed", Toast.LENGTH_SHORT).show(); } } }
参考链接如下:
https://developer.android.com/reference/android/Manifest.permission.html
https://developer.android.com/reference/android/provider/Settings.html#ACTION_MANAGE_OVERLAY_PERMISSION
http://www.jianshu.com/p/2746a627c6d2
0 0
- android M SYSTEM_ALERT_WINDOW权限问题
- android 特殊权限SYSTEM_ALERT_WINDOW,WRITE_SETTINGS
- 随笔:关于权限android.permission.SYSTEM_ALERT_WINDOW
- Android 全局弹出对话框SYSTEM_ALERT_WINDOW权限
- android M权限问题
- Android 在Service中弹出窗口及SYSTEM_ALERT_WINDOW权限解决方法
- Android 6.0(M) 获得权限请求问题
- Android M 6.0 READ_CONTACTS WRITE_CONTACTS权限问题
- Android O 特殊权限SYSTEM_ALERT_WINDOW申请,“permission denied for window type”报错处理
- Andrid6.0申请SYSTEM_ALERT_WINDOW和WRITE_SETTINGS权限
- Android M 权限
- Android M权限请求
- Android M权限管理
- Android M权限管理
- android M 系统权限
- Android6.0 申请SYSTEM_ALERT_WINDOW权限 没有弹出权限框
- Android M Permissions 权限管理
- Android (6.0) M 请求权限
- TCP/IP、Http、Socket的区别
- 程序员的基础生存技能:高效用Google
- elasticsearch基本插件
- Java网络工具类
- Java多线程总结
- android M SYSTEM_ALERT_WINDOW权限问题
- Android的onMeasure和onLayout And MeasureSpec揭秘
- dreamweaver图片热点工具
- JAVA学习代码——利用正则表达式判断手机/邮箱/身份证
- mysql之视图
- var_export does not handle circular references
- js闭包的用途
- java折半查找指定数值的最左边的位置
- 第5章 散列——分离链接法