Android 如何为我的活动授予屏幕覆盖权限

Android 如何为我的活动授予屏幕覆盖权限,android,android-6.0-marshmallow,Android,Android 6.0 Marshmallow,在我的应用程序中,android 6出现了屏幕覆盖问题+ 我试图打开,但为此,我需要给予屏幕覆盖权限 我无法融入到我的活动中 我也试过,似乎两者都起作用,所以我想将它们整合到我的活动中 这是我的活动: 公共类MainActivity扩展活动{ 公共静态最终整数R_PERM=123; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.data)

在我的应用程序中,android 6出现了屏幕覆盖问题+ 我试图打开,但为此,我需要给予屏幕覆盖权限

我无法融入到我的活动中

我也试过,似乎两者都起作用,所以我想将它们整合到我的活动中

这是我的活动:

公共类MainActivity扩展活动{
公共静态最终整数R_PERM=123;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.data);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Picture);
if((CheckPermission(这个,Manifest.permission.CAMERA))&&
(CheckPermission(这个,Manifest.permission.READ\u PHONE\u STATE))&&
(CheckPermission(这个,Manifest.permission.NFC))){
PermHandling();
}否则{
RequestPermission(MainActivity.this、Manifest.permission.CAMERA、R\u PERM);
RequestPermission(MainActivity.this、Manifest.permission.READ\u PHONE\u STATE、R\u PERM);
RequestPermission(MainActivity.this、Manifest.permission.NFC、R_PERM);
//NewPermHandling();
}
}
私有空间处理(){
//我的应用程序内部部件。。。。
//我的东西在这里工作。。。
}
//私有void NewPermHandling(){
//}
@凌驾
public void onRequestPermissionsResult(int permsRequestCode,字符串[]权限,int[]grantResults){
开关(permsRequestCode){
案例R_PERM:{
如果(grantResults.length>0
&&grantResults[0]==PackageManager.PERMISSION\u已授予){
PermHandling();
}否则{
//Toast.makeText(此“请授予其他wise应用程序将关闭的权限”。!”,Toast.LENGTH\u SHORT.show();
}
返回;
}
}
}
public void RequestPermission(活动thisActivity,字符串权限,int代码){
if(ContextCompat.checkSelfPermission)(此活动,
(许可证)
!=PackageManager.权限(已授予){
如果(活动)公司应显示请求许可理由(此活动,
(许可证){
}否则{
ActivityCompat.requestPermissions(此活动,
新字符串[]{Permission},
代码);
}
}
}
公共布尔检查权限(上下文、字符串权限){
if(ContextCompat.checkSelfPermission)(上下文,
权限)==PackageManager.Permission(已授予权限){
返回true;
}否则{
返回false;
}
}
}
有人能建议我如何在我的活动中给予屏幕定向许可吗 因此,用户无需给予它或担心它,请帮助
在这里,我尝试过,但我不知道如何使用
PERM\u REQUEST\u code\u DRAW\u OVERLAYS

任何人请在我的活动上帮助我这不是重复的或其他我正在询问如何将其添加到我的活动中

您检查的第二秒清楚地显示了检查系统警报窗口权限的方法。但要简单地解释一下

如上所述

允许应用程序使用类型\系统\警报创建窗口, 显示在所有其他应用程序之上。很少有应用程序应该使用这个 许可;这些窗口用于系统级交互 与用户进行交互

注意:如果应用程序的目标API级别为23或更高,则应用程序用户必须 通过许可证向应用程序显式授予此权限 管理屏幕。应用程序通过发送电子邮件请求用户批准 意图与操作操作管理覆盖权限。应用程序可以检查 是否通过调用 Settings.canDrawOverlays()

正如你查过的报告中提到的

以下是简化的步骤:-

  • 首先检查当前设备SDK版本是否大于或等于 通过以下if条件等于安卓M(23)

    if (android.os.Build.VERSION.SDK_INT >= 23) {
    }
    
  • 然后使用developer.android.com中提到的Settings.canDrawOverlays()检查您的应用程序是否已经拥有权限,我们将检查是否没有权限

    if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
    }
    
  • 然后,正如developer.android.com中提到的,以及SO post中实现的那样,触发一个具有操作\管理\覆盖\权限的意图

    if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {   //Android M Or Over
       Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
       startActivityForResult(intent, <YOUR REQUEST CODE>);
       return;
    }
    
    if(android.os.Build.VERSION.SDK_INT>=23&&!Settings.canDrawOverlays(this)){//android M或更高版本
    Intent Intent=newintent(Settings.ACTION\u MANAGE\u OVERLAY\u PERMISSION,Uri.parse(“package:+getPackageName());
    startActivityForResult(意图,);
    返回;
    }
    
  • 在Activity中定义的onActivityResult()方法中处理结果,然后再次使用Settings.canDrawOverlays()检查结果,如果仍然没有,则在向用户显示相应警报后完成()活动


  • 您可以在其他权限流完成后实现整个流程。

    下面是一个示例代码,用于使用自定义覆盖禁用拉通知。它在Android以下版本和6+上运行良好

    清单中需要的权限:

    <uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" /> 
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
    
    修改后的代码

    public class MainActivity extends Activity {
    
     public static final int REQUEST_PERMISSION = 123;
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.data);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
       Log.v("App", "Build Version Greater than or equal to M: " + Build.VERSION_CODES.M);
       checkDrawOverlayPermission();
      } else {
       Log.v("App", "OS Version Less than M");
       //No need for Permission as less then M OS.
      }
    
    
      if ((CheckPermission(this, Manifest.permission.CAMERA)) &&
       (CheckPermission(this, Manifest.permission.READ_PHONE_STATE)) &&
       (CheckPermission(this, Manifest.permission.NFC))) {
       PermHandling();
      } else {
       RequestPermission(MainActivity.this, Manifest.permission.CAMERA, REQUEST_PERMISSION);
       RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION);
       RequestPermission(MainActivity.this, Manifest.permission.NFC, REQUEST_PERMISSION);
    
       //NewPermHandling();
      }
    
     }
    
     private void PermHandling() {
      //My app internal parts....
      //Here my stuff works...
     }
    
     //private void NewPermHandling(){
    
     //}
    
     @Override
     public void onRequestPermissionsResult(int permissionRequestCode, String[] permissions, int[] grantResults) {
      if (permissionRequestCode != REQUEST_PERMISSION) {
       return;
      }
    
      if (grantResults.length && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
       PermHandling();
      } else {
       // Ask the user to grant the permission
      }
     }
    
     public void RequestPermission(Activity thisActivity, String Permission, int Code) {
      if (ContextCompat.checkSelfPermission(thisActivity,
        Permission) !=
       PackageManager.PERMISSION_GRANTED) {
       if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
         Permission)) {} else {
        ActivityCompat.requestPermissions(thisActivity,
         new String[] {
          Permission
         },
         Code);
       }
      }
     }
    
     public final static int REQUEST_CODE = -1010101;
    
     @RequiresApi(api = Build.VERSION_CODES.M)
     public void checkDrawOverlayPermission() {
      Log.v("App", "Package Name: " + getApplicationContext().getPackageName());
    
      // Check if we already  have permission to draw over other apps
      if (!Settings.canDrawOverlays(context)) {
       Log.v("App", "Requesting Permission" + Settings.canDrawOverlays(context));
       // if not construct intent to request permission
       Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
        Uri.parse("package:" + getApplicationContext().getPackageName()));
       // request permission via start activity for result 
       startActivityForResult(intent, REQUEST_CODE); //It will call onActivityResult Function After you press Yes/No and go Back after giving permission
      } else {
       Log.v("App", "We already have permission for it.");
       // disablePullNotificationTouch();
       // Do your stuff, we got permission captain
      }
     }
    
     @TargetApi(Build.VERSION_CODES.M)
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.v("App", "OnActivity Result.");
      //check if received result code
      //  is equal our requested code for draw permission
      if (requestCode == REQUEST_CODE) {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Settings.canDrawOverlays(this)) {
         // Permission Granted by Overlay
         // Do your Stuff
        }
       }
      }
     }
    
     public boolean CheckPermission(Context context, String Permission) {
      if (ContextCompat.checkSelfPermission(context,
        Permission) == PackageManager.PERMISSION_GRANTED) {
       return true;
      } else {
       return false;
      }
     }
    }
    

    如果您从活动而不是从服务调用startActivityForResult,则startActivityForResult将调用onActivityResult。阅读更多信息

    这是解决方案,我搜索所有网页。找不到任何有用的东西。答案是:当你请求新的许可时,永远不要做其他事情,比如展示祝酒词或。。。。在我的情况下,我重新启动我的应用程序并请求下一个用于重新启动应用程序的权限

    祝你好运


    @不要消极,你可以试试这个,如果不行,请再敲我一下:

    wm = (WindowManager) content.getSystemService(Service.WINDOW_SERVICE);
    
         orientationChanger = new LinearLayout(content);
         orientationChanger.setClickable(false);
         orientationChanger.setFocusable(false);
         orientationChanger.setFocusableInTouchMode(false);
         orientationChanger.setLongClickable(false);
    
        orientationLayout = new WindowManager.LayoutParams(
            LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT,
            windowType,  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.RGBA_8888);
    
         wm.addView(orientationChanger, orientationLayout);
        orientationChanger.setVisibility(View.GONE);
    
        orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        wm.updateViewLayout(orientationChanger, orientationLayout);
        orientationChanger.setVisibility(View.VISIBLE);
    

    谢谢@Rajen Raiyarela,但这里我在
    PERM\u REQUEST\u code\u DRAW\u OVERLAYS中遇到了错误
    我不知道之后我可以检查一下
    wm = (WindowManager) content.getSystemService(Service.WINDOW_SERVICE);
    
         orientationChanger = new LinearLayout(content);
         orientationChanger.setClickable(false);
         orientationChanger.setFocusable(false);
         orientationChanger.setFocusableInTouchMode(false);
         orientationChanger.setLongClickable(false);
    
        orientationLayout = new WindowManager.LayoutParams(
            LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT,
            windowType,  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.RGBA_8888);
    
         wm.addView(orientationChanger, orientationLayout);
        orientationChanger.setVisibility(View.GONE);
    
        orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        wm.updateViewLayout(orientationChanger, orientationLayout);
        orientationChanger.setVisibility(View.VISIBLE);