0

任何人都可以提供一些見解如何請求在Android Marshmallow中的用戶定義的自定義運行時權限?我正在開發具有讀取權限的自定義內容提供程序,另一個應用程序正在嘗試使用自定義內容提供程序訪問數據。Android:在運行時請求自定義權限

+0

我已經回答了類似的問題。請參閱[這裏](http://stackoverflow.com/a/40639430/3423932)。 – muthuraj

+0

這是一個老問題,但我碰到了自定義權限問題的「安裝順序依賴」,並且無法使用其他解決方案(如「簽名」保護),因爲其他應用程序是由另一個開發人員開發的。您應該可以像請求系統權限一樣請求自定義權限,但請確保自定義權限具有權限組和「危險」保護級別。有了這兩項要求,系統將提供運行時權限請求。 – AChez9

回答

1

我個人更喜歡使用權限庫。取而代之的大型樣板代碼,他們往往是有效的,代碼是非常精確的

請參閱本PermissionsDispatcher

@RuntimePermissions 
public class MainActivity extends AppCompatActivity { 
    //sepcify permissions here 
    @NeedsPermission(Manifest.permission.CAMERA) 
    void showCamera() { 
     //work after permission is granted 
    } 

    @OnShowRationale({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}) 
    void showRationaleForCamera(final PermissionRequest request) { 
     new AlertDialog.Builder(this) 
      .setMessage(R.string.permission_camera_rationale) 
      .setPositiveButton(R.string.button_allow, (dialog, button) -> request.proceed()) 
      .setNegativeButton(R.string.button_deny, (dialog, button) -> request.cancel()) 
      .show(); 
    } 

    @OnPermissionDenied({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}) 
    void showDeniedForCamera() { 
     //works when permissions denied 
     Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show(); 
    } 

    @OnNeverAskAgain({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}) 
    void showNeverAskForCamera() { 
     Toast.makeText(this, R.string.permission_camera_neverask, Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

感謝您的示例。你可以給我一些關於如何使用自定義的權限的例子。 –

+0

如果你要求多個權限像read_storage和相機..等等,那麼我有更新我的答案你可以定義許多你想要的許可 – Vicky

+0

這些都是內置的權限。我正在尋找一個用戶定義權限的例子。如何處理用戶定義的用戶定義的自定義權限? –

0

這對我的作品。

public class MainActivity extends AppCompatActivity { 
private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1; 

private boolean checkAndRequestPermissions() { 
    Context context=getApplicationContext(); 
    int locationpermission = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION); 
    int readphonestatepermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE); 
    int permissionSendMessage = ContextCompat.checkSelfPermission(context,Manifest.permission.SEND_SMS); 
    int writepermission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
    int readexternalstoragepermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE); 


    List<String> listPermissionsNeeded = new ArrayList<>(); 

    if (locationpermission != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION); 
    } 

    if (readphonestatepermission != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE); 
    } 
    if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.SEND_SMS); 
    } 

    if (writepermission != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); 
    } 

    if (readexternalstoragepermission != PackageManager.PERMISSION_GRANTED) { 
     listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE); 
    } 

    if (!listPermissionsNeeded.isEmpty()) { 
     requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS); 
     return false; 
    } 
    return true; 
} 



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


    if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS) { 



     if (grantResults.length > 0) { 
      for (int i = 0; i < permissions.length; i++) { 


       if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION)) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         Log.e("msg", "location granted"); 


        } 
       } else if (permissions[i].equals(Manifest.permission.READ_PHONE_STATE)) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         Log.e("msg", "read phone state granted"); 


        } 
       } else if (permissions[i].equals(Manifest.permission.SEND_SMS)) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         Log.e("msg", "sms granted"); 


        } 

       }else if (permissions[i].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         Log.e("msg", "write external granted"); 


        } 
       }else if (permissions[i].equals(Manifest.permission.READ_EXTERNAL_STORAGE)) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         Log.e("msg", "read external storage granted"); 

         Toast.makeText(SplashActivity.this, "Please Restart the Application....... ", Toast.LENGTH_LONG).show(); 
        } 
       }else if (permissions[i].equals(Manifest.permission.READ_LOGS)) { 
        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { 
         Log.e("msg", "read logs granted"); 



        } 
       } 


      } 

     } 


    } 
} 
@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle v) { 
    // TODO Auto-generated method stub 
    super.onCreate(v); 
    setContentView(R.layout.activity_splash); 


    if (checkAndRequestPermissions()) { 

     Toast.makeText(MainActivity.this, "Granted all permissions", Toast.LENGTH_SHORT).show(); 


    } 
} 
} 
+1

謝謝@Amol。這是內置權限的示例。你能否提供一些處理用戶定義權限的例子。 –

+1

謝謝。這工作@Amol –