2017-10-06 266 views
1

如何在应用程序自动启用应用程序权限时响应本机应用程序权限?如何在应用程序自动启用时自动启用应用程序权限获取系统警报

import { PermissionsAndroid } from 'react-native'; 
import Config from '../models/Config'; 

// Permission param must be provided from Android Docs. Ex: READ_CONTACTS 
export const requestPermission=async(permission='READ_CONTACTS')=> { 
    try { 
    const granted = await PermissionsAndroid.request(
     PermissionsAndroid.PERMISSIONS[permission], 
    ) 
    if(granted=='never_ask_again'){ 
     Config.setPermission(false); 
    } 
    return granted; 
    }catch (err) { 
    console.log(err); 
    //alert plz give the suitable permission 
    } 
} 

在构建应用程序时,我期待获取应用程序权限的系统警报。但是在应用系统警报没有出现时,我正在查看应用权限设置,它已经启用。谢谢。

+1

你能解释你的问题吗?没有这些,很难说人们可以如何帮助你。 –

+0

构建应用程序时,我期待获取应用程序权限的系统警报。但是在应用系统警报没有出现时,我正在查看应用权限设置,它已经启用。 谢谢。 –

+0

请更新问题,无需在评论 –

回答

0

这是你如何得到通知代码的前几行是来检查,如果我们有一个SD卡,然后我们开始处理您在Android清单文件中设置的权限,我将不包括代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { 
      //.... write file into storage ... 
      System.out.println("SDK > BuildVersion TRUE"); 
     } else { 
      requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 666); // Comment 26 
      System.out.println("go to requestPermissions"); 
     } 
    } 
    onLoad(); 
} 

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

    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 

     case 666: // Allowed was selected so Permission granted 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Permission Granted",Snackbar.LENGTH_LONG); 
       View snackbarView = s.getView(); 
       TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text); 
       textView.setTextColor(Color.RED); 
       textView.setTextSize(18); 
       textView.setMaxLines(6); 
       s.show(); 

       // do your work here 

      } else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) { 
       // User selected the Never Ask Again Option Change settings in app settings manually 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle); 
       alertDialogBuilder.setTitle("Change Permissions in Settings"); 
       alertDialogBuilder 
         .setMessage("Click SETTINGS to Manually Set\n\n"+"Permissions to use Database Storage") 
         .setCancelable(false) 
         .setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
           Uri uri = Uri.fromParts("package", getPackageName(), null); 
           intent.setData(uri); 
           startActivityForResult(intent, 1000);  // Comment 3. 
          } 
         }); 

       AlertDialog alertDialog = alertDialogBuilder.create(); 
       alertDialog.show(); 

      } else { 
        // User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { 

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle); 
        alertDialogBuilder.setTitle("Second Chance"); 
        alertDialogBuilder 
          .setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App") 
          .setCancelable(false) 
          .setPositiveButton("RETRY", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            //ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Integer.parseInt(WRITE_EXTERNAL_STORAGE)); 
            Intent i = new Intent(MainActivity.this,MainActivity.class); 
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(i); 
            } 
          }) 
          .setNegativeButton("EXIT", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            finish(); 
            dialog.cancel(); 
           } 
          }); 
        AlertDialog alertDialog = alertDialogBuilder.create(); 
        alertDialog.show(); 
       } 
      } 
      break; 
    }};