2016-03-10 39 views
1

我想通过给我的应用程序提供许可模型来实现新的棉花糖支持。 现在我已经在我的清单文件中给出了下面的权限。棉花糖新的许可模式

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

现在,在一个活动我已加载在其中我具有recyclerview和数据使用recyclerview适配器在此recyclerview加载的片段。

在这种recyclerview适配器的列表中,我是个有一个按钮,按下哪些我将要求上述许可。所以,当用户点击这个按钮时,我已经调用了下面的函数。

public void checkForManifestPermission(){ 

    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(mContext, 
      Manifest.permission.READ_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)mContext, 
       Manifest.permission.READ_EXTERNAL_STORAGE)) { 
      // 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. 

      ActivityCompat.requestPermissions((Activity)mContext, 
        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
        ConstantVariables.READ_EXTERNAL_STORAGE); 

     } else { 

      // No explanation needed, we can request the permission. 
      ActivityCompat.requestPermissions((Activity)mContext, 
        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 
        ConstantVariables.READ_EXTERNAL_STORAGE); 

      // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
     } 
    } 
} 

现在在活动中我重写了下面的回调方法。

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case ConstantVariables.READ_EXTERNAL_STORAGE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 
       ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true; 

      } else { 

       ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = false; 
       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

在我打电话的功能checkForManifestPermission和检查的条件 if(ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE){} 然后只重定向到其他活动适配器。

现在我的问题是,这里面的条件编写的代码调用onRequestPermissionsResult方法之前得到执行,我想,如果允许用户授予那么只有它应该重定向到其他活动。

我怎样才能做到这一点,我想我的代码,以等待用户给出回应有关批准或拒绝的权限,然后进行进一步的相应。

非常感谢您的帮助。

+1

请更新完整的代码。 –

+0

尝试这可能是工作http://stackoverflow.com/a/41221852/5488468 –

回答

0

试试这个:

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    if (requestCode == PERMISSIONS_CODE) { 
     for (int i = 0; i < permissions.length; i++) { 
      String permission = permissions[i]; 
      int grantResult = grantResults[i]; 

      if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) { 
       if (grantResult == PackageManager.PERMISSION_GRANTED) { 
        onPPSButtonPress(); 
       } else { 
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_CODE); 
       } 
      } 
     } 
    } 
} 

注:添加您的读取权限代码像上面。

+0

我想设置一些变量,而不是调用函数onPPSButtonPress();并检查该变量是否设置为true重定向到其他活动。 我该怎么做? –

0

可以在棉花糖设置下面检查许可代码:

if ((int) Build.VERSION.SDK_INT >= 23) { 
       if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
        // TODO: Consider WRITE_EXTERNAL_STORAGE 
        if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, 
          Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
        } else { 
         ActivityCompat.requestPermissions(mActivity, 
           new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
           MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); 
        } 
        return; 
       } 
      } 
0

我在两个不同的项目类似的问题,我的工作。

在一个我简单地进行权限的检查在进入活动。 如果没有给出权限,那么我禁用了该按钮。

如果这是不实际的,则另一种可能性是采取代码,如你所描述的,“是你的条件”,并把它变成一个独立的方法。 然后更改checkForManifestPermission()以返回布尔值,以确定该程序是否已具有所需的权限。

那么,那部分代码会读这样的事情:

private void methodThatNeedsPermission(){ 
    if (checkForManifestPermission()) 
     doTheAction(); 
} 

private void doTheAction(){ 
    ---- your code 
} 

现在,在onRequestPermissionsResult,如果用户okays许可,您可以再调用相同的方法。因此,它变成:

@Override 
public void onRequestPermissionsResult(int requestCode, 
             String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case ConstantVariables.READ_EXTERNAL_STORAGE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
        && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 
      ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = true; 
      doTheAction(); 

     } else { 

      ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE = false; 
      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
     } 
     return; 
    } 

    // other 'case' lines to check for other 
    // permissions this app might request 
} 
+0

我有我的代码写在适配器类,我不能使用该适配器以外的任何方法调用。 如何在'onRequestPermissionsResult'方法中将该值设置为true后将此值'ConstantVariables.ENABLE_READ_EXTERNAL_STORAGE'返回给我已调用'checkForManifestPermission'函数的适配器。 –

+0

但是,只要类未定义为静态类,就可以将新方法移出适配器,并从适配器内部调用它。我能想到的另一种可能性是使用EventBus传回结果。 https://greenrobot.github.io/EventBus/ http://square.github.io/otto/ – theblitz