2017-10-06 169 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; 
    }}; 
相關問題