2016-11-05 42 views
5

在我的應用我得到屏幕覆蓋問題在android系統6+ 我試圖打開但我需要給的屏幕疊加一個權限如何給屏幕覆蓋權限在我的活動

我跟着this我無法在我的活動,以整合

我也試過this似乎兩者都工作,所以我想整合他們在我的活動

這是我的活動:

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

任何一個可以建議我如何給屏幕方向允許在我的活動 使用戶無需給它,也不用擔心它請幫助 在這裏,我試過,但我不知道PERM_REQUEST_CODE_DRAW_OVERLAYS

任何人請幫助我對我的活動這不是重複或別的東西我問如何將其添加到我的活動

+0

爲什麼你需要這個應用程序的權限? –

+0

它詢問屏幕覆蓋權限沒有該應用程序不工作......但是當我嘗試打開它有選項「否」給我的應用程序....如果您已經知道Android 6+的屏幕覆蓋問題爲每個應用程序然後試圖幫助我先生... –

回答

4

下面是一個示例代碼,可以通過使用自定義覆蓋來禁用Pull通知,並且它可以在下面的版本和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(); 
       } 
      } 
     } 
    } 

修改代碼: 不要忘了使用這些權限:

<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會叫onActivityResult如果從一個活動叫這個,而不是從服務。閱讀更多關於它here

+0

謝謝@Ahmad Shahwiz先生你能否在我的守則中更新你的答案其實我無法將兩者結合起來我將向你贈予賞金 –

+0

已更新它。如果您需要幫助,請告訴我。 –

+0

非常感謝它解決了80%的問題..我會重命名這樣...請將我的問題投票我已經失去了一些在賞金... –

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. Th連接使用Settings.canDrawOverlays作爲developer.android.com檢查您的應用程序是否已經許可或沒有提到(),我們將檢查在developer.android提到的沒有權限

    if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) { 
    } 
    
  3. 然後。 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. 處理的結果在onActivityResult在活動()定義的方法,並再次檢查使用Settings.canDrawOverlays()如果仍然沒有示出,然後向用戶適當的警報後完成()的活性。

您可以在其他許可流程完成後執行此整個流程。

+0

謝謝@Rajen Raiyarela但我在這裏得到錯誤在'PERM_REQUEST_CODE_DRAW_OVERLAYS'我不知道之後,我可以檢查代碼是否工作,然後我需要給予此權限以及我的許可......你能否用我的代碼更新你的答案所以如果它的工作我會給予賞金 –

+0

PERM_REQUEST_CODE_DRAW_OVERLAYS不過是REQUEST_CODE來標識在OnActivityResult方法中接收到的結果。我修改並放置了<您的請求代碼>而不是PERM_REQUEST_CODE_DRAW_OVERLAYS。關於實現,你可以從你的PermHandling()方法調用這個流程。 –

+0

對不起,我可以在那裏得到錯誤..如果可能,你可以更新我的代碼中的答案....在這裏,我感到1.PERM_REQUEST_CODE_DRAW_OVERLAYS和2.在我的代碼中集成此... –

2

這是解決方案,我搜索所有的網站。並找不到有用的東西。答案是:當你要求新的權限時,千萬不要做其他事情,如顯示烤麪包或....在我的情況下,我重新啓動我的應用程序,並要求我使用this code重新啓動應用程序的下一個權限,

祝你好運。

@不要成爲負你可以試試這個,如果不行請再次敲我:

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); 
+0

謝謝@Jamil Hasnine Tamim先生但重新啓動之前我需要添加覆蓋權限我已經嘗試過但可能會以不同的方式進入你能否在我的代碼中更新你的答案我會給你一個獎勵...... –

+0

@不要負面你可以嘗試我的更新部分比如果它不工作再次敲我請... –

+0

謝謝@Jamil Hasnine Tamim先生但已經給我在我的應用程序作爲'SetRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); ' –

-2

是啊,這是不可能的,從在Android 6.0

您需要在運行時請求權限。

+1

我已經給定運行時間權限請在回答前仔細閱讀問題 –