How to check new android 6.0 Permissions from service

后端 未结 3 1841
南笙
南笙 2021-01-23 11:30

I\'ve an Android 6.0 Application with an unbound android service connecting to Google API for receiving Fused API Location Provider Updates. For this i need the ACCESS_FINE_LOCA

3条回答
  •  -上瘾入骨i
    2021-01-23 11:54

    Use this method. This should work.

    1. On Oncreate() :

       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)   {
                      givePerMisson();
                  } else {
                      requestIntent();
                  }
      
    2. Add/Delete as per your need:

       @TargetApi(Build.VERSION_CODES.M)
      private void givePerMisson() {
          if ((AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) &&
                  AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CALL_PHONE) &&
                  AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
                  AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.GET_ACCOUNTS) &&
                  AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) &&
                  AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CAMERA)
          )) {
              requestIntent();
          } else {
              AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION, AndyUtils.PERMISSOIN);
              AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.CALL_PHONE, AndyUtils.PERMISSOIN);
              AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, AndyUtils.PERMISSOIN);
              AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION, AndyUtils.PERMISSOIN);
              AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.GET_ACCOUNTS, AndyUtils.PERMISSOIN);
              AndyUtils.givePermisson(SplashActivity.this, Manifest.permission.CAMERA, AndyUtils.PERMISSOIN);
              if (!AndyUtils.permissionsList.isEmpty()) {
                  requestPermissions(AndyUtils.permissionsList.toArray(new String[AndyUtils.permissionsList.size()]), AndyUtils.PERMISSOIN_CODE);
              }
          }
      
      }
      

    3.

     @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case AndyUtils.PERMISSOIN_CODE: {
                if ((AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) &&
                        AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CALL_PHONE) &&
                        AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
                        AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.GET_ACCOUNTS) &&
                        AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) &&
                        AndyUtils.checkPermission(SplashActivity.this, Manifest.permission.CAMERA)
                )) {
                    requestIntent();
                } else {
                    finish();
                }
            }
        }
    }
    
    1.   public static boolean checkPermission(Context context, String permission) {
      try {
          PackageManager pm = context.getPackageManager();
          if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
              return true;
          } else {
          }
      
      } catch (Exception e) {
          Log.e("PermissionError", e.toString());
      }
      return false;
      }
      

      5.

      public static void givePermisson(Context context, String permisson, String permissonType) {
          int per = context.checkSelfPermission(permisson);
          if (per != PackageManager.PERMISSION_GRANTED) {
              permissionsList.add(permisson);
      
          } else if (per != PackageManager.PERMISSION_DENIED) {
          }
      }
      

提交回复
热议问题