如何在我的活动上提供屏幕覆盖权限

时间:2016-11-05 11:30:37

标签: android android-6.0-marshmallow

在我的应用程序中,我在android 6+中遇到屏幕覆盖问题 我试图打开但是为此我需要为屏幕覆盖提供权限

我关注this我无法在我的活动中进行整合

我也试过this似乎两者都有效,所以我想将它们整合到我的活动中

这是我的活动:

Person

任何人都可以建议我如何在我的活动中给予屏幕方向权限 所以用户不需要给它或担心它请帮助 在这里我试过但我不知道 public class MainActivity extends Activity { public static final int R_PERM = 123; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.data); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 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, R_PERM); RequestPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE, R_PERM); RequestPermission(MainActivity.this, Manifest.permission.NFC, R_PERM); //NewPermHandling(); } } private void PermHandling() { //My app internal parts.... //Here my stuff works... } //private void NewPermHandling(){ //} @Override public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) { switch (permsRequestCode) { case R_PERM: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { PermHandling(); } else { //Toast.makeText(this, "Please Grant Permissions other wise app will close.!", Toast.LENGTH_SHORT).show(); } return; } } } 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 boolean CheckPermission(Context context, String Permission) { if (ContextCompat.checkSelfPermission(context, Permission) == PackageManager.PERMISSION_GRANTED) { return true; } else { return false; } } }

任何人请帮助我完成我的活动这不重复或我要求的其他内容如何在我的活动中添加

4 个答案:

答案 0 :(得分:11)

以下是使用自定义叠加层禁用拉动通知的示例代码。它适用于以下Android版本和6 +。

清单中所需的权限:

<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" /> 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

停用提取通知   

private void disablePullNotificationTouch() {
   try {
    Log.v("App", "Disable Pull Notification");

    private HUDView mView = new HUDView(this);
    int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);
    Log.v("App", "" + statusBarHeight);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
     WindowManager.LayoutParams.MATCH_PARENT,
     statusBarHeight,
     WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
     WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
     WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, //Disables status bar
     PixelFormat.TRANSPARENT); //Transparent

    params.gravity = Gravity.CENTER | Gravity.TOP;
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mView, params);
   } catch (Exception e) {
    Log.v("App", "Exception: " + e.getMessage());

   }
  }

  // code to post/handler request for permission 
  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);
   } else {
    Log.v("App", "We already have permission for it.");
    disablePullNotificationTouch();
   }
  }
  @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)) {
      disablePullNotificationTouch();
     }
    }
   }
  }

修改后的代码

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将调用onActivityResult。详细了解here

答案 1 :(得分:3)

您检查过的第二个post清楚地显示了检查SYSTEM_ALERT_WINDOW权限的方法。但要简单地解释一下,

正如developer.android.com

所述
  

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

     

注意:如果应用针对API级别23或更高级别,则应用用户必须   通过权限明确授予应用程序此权限   管理屏幕。该应用程序通过发送请求用户的批准   意图采取行动ACTION_MANAGE_OVERLAY_PERMISSION。该应用程序可以检查   是否通过电话获得此授权   Settings.canDrawOverlays()。

并且如您检查的SO post中所述,

以下是简化步骤: -

  1. 首先检查当前设备SDK版本是否大于或 等于条件

    等于Android M(23)
    if (android.os.Build.VERSION.SDK_INT >= 23) {
    }
    
  2. 然后使用developer.android.com中提到的Settings.canDrawOverlays()检查您的应用程序是否已获得许可,我们将检查是否有权限

    if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
    }
    
  3. 然后如developer.android.com中所述,并且在SO帖子中实现,触发ACTION_MANAGE_OVERLAY_PERMISSION的意图。

    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;
    }
    
  4. 处理Activity中定义的onActivityResult()方法中的结果,如果仍未执行,则再次使用Settings.canDrawOverlays()检查,然后在向用户显示适当的警报后完成()活动。

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

答案 2 :(得分:2)

这是解决方案,我搜索所有网络。并且找不到任何有用的东西。答案是:当你要求获得新的许可时,不要做其他事情,比如显示吐司或....在我的情况下,我重启我的应用程序并要求下一个权限,我使用this code重启app,

祝你好运。

@ Don不要你可以尝试这个,如果不行,请再次敲我说:

basefilename=`echo $connectdb | sed 's/[0-9].*//'`
counter=`echo $connectdb | sed 's/[^0-9]//g'`
let counter++
nextdatafilename=$basefilename$counter'.dbf'

答案 3 :(得分:-4)

是的,从Android 6.0开始这是不可能的

您需要在运行时请求权限。