How to provide screen resolution to my activity

In my application, I get Screen Overlay Issue in android 6+ I tried to enable, but for this I need to provide permission for the screen overlay

I followed this. I cannot integrate into my activity.

I also tried this one , it seems both work, so I want to integrate them into my activities

This is my activity:

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; } } } 

Can someone suggest me how to provide screen orientation permission in my activity? So that this user does not need this, or worry about it. Please help. Here I tried, but I do not know about PERM_REQUEST_CODE_DRAW_OVERLAYS

Someone Please help me in my work, this is not a duplicate or anything else that I ask. How to add it to your activity.

+5
source share
4 answers

Here is a sample code to disable Pull Notification using a custom overlay and it works in the version below and 6+. After integration, let us know if you have any problems.

Rights required in the manifest:

 <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(); } } } } 

Code change: Remember to use these permissions:

 <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 R_PERM = 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, 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 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 will call onActivityResult if you call it from an action, not from a service. Read more about it here.

+4
source

The second post you checked clearly shows how to check the permissions of SYSTEM_ALERT_WINDOW. But just explain

As mentioned at developer.android.com

Allows an application to create windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications must use this permission; these windows are intended for interaction at the system level with the user.

Note. If the application is intended for API level 23 or higher, the application user must directly allow this permission to the application through the resolution of the control screen. The application seeks user approval by sending an intent with the action ACTION_MANAGE_OVERLAY_PERMISSION. An application can check if it has this permission by calling Settings.canDrawOverlays ().

and as indicated in the SO post , you noted

Here are the simplified steps: -

  • First check if the SDK version of the device is greater than or equal to Android M (23) if the condition

     if (android.os.Build.VERSION.SDK_INT >= 23) { } 
  • Then, using Settings.canDrawOverlays (), as indicated in the developer.android.com file, check if your application already has permission or not, we will check if it has permission

     if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) { } 
  • Then, as mentioned in the developers.android.com file and as implemented in the SO message, the intent is triggered using 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; } 
  • Process the result with the onActivityResult () method defined in Activity, and check again with Settings.canDrawOverlays () if it has not completed () the action after the corresponding warning is displayed to the user.

This whole thread, which you can implement after the completion of another thread of authority.

+3
source

This solution, I am looking for everything on the Internet. And can not find anything useful. answer: when you request a new permission, never do anything else, for example, show a toast or ... in my case, I will restart my application and ask for the next permission, which I use this code to restart the application,

good luck.

@ Do not be negative You can try this, if not work, please hit me again:

 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); 
+2
source

Yes, this is not possible from Android 6.0

You need to request permission at runtime.

-2
source

Source: https://habr.com/ru/post/1259319/


All Articles