How to check new android 6.0 Permissions from service

后端 未结 3 1843
南笙
南笙 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条回答
  • 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) {
          }
      }
      
    0 讨论(0)
  • 2021-01-23 12:00

    For that you have to check before your service start if your service start before checking permission will not work properly

    0 讨论(0)
  • 2021-01-23 12:10

    Yes you can accept the permissions once in activity and use it in service , if you grant the permission , then it will never ask again. Following is the code for showing dialog for ACCESS_FINE_LOCATION ,You can also refer Requesting Permissions at Run Time

    private void showPermissionDialog() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(mActivity,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {
    
                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
    
            } else {
    
                // No explanation needed, we can request the permission.
    
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_FOR_LOCATION);
    
                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            if (mGoogleApiClient != null)
                mGoogleApiClient.connect();
        }
    }
    
    0 讨论(0)
提交回复
热议问题