我是android新手。我試圖授予自定義類的權限。我有兩個接口,一個單獨的類,像這樣使用自定義類授予權限
public interface RequestPermissionsResultInterface
{
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults);
}
public interface PermissionManagerInterface
{
void onPermissionGranted(String message, int requestCode);
void onPermissionDenied(String message, int requestCode);
}
public class PermissionManager
{
private Activity mActivity;
private static volatile PermissionManagerInterface mManagerInterface;
public static PermissionManager getInstance(Context context)
{
if (mPermissionManager == null)
{
synchronized (PermissionManager.class)
{
if (mPermissionManager == null)
{
mPermissionManager = new PermissionManager(context);
}
}
}
return mPermissionManager;
}
private boolean isReadStorageAllowed()
{
int result = ContextCompat.checkSelfPermission(this.mActivity, Manifest.permission.READ_EXTERNAL_STORAGE);
return ((result == PackageManager.PERMISSION_GRANTED));
}
public RequestPermissionsResultInterface askPermission(
Activity mActivity,
String permissionName,
final PermissionManagerInterface managerInterface,
final int requestCode)
{
boolean isReadExternalStorageAllowed = isReadStorageAllowed();
if(isReadExternalStorageAllowed == false)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permissionName))
{
final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setMessage("Please allow all permissions in App Settings for additional functionality.");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
managerInterface.onPermissionGranted("Permission Granted", requestCode);
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
managerInterface.onPermissionDenied("Permission Denied", requestCode);
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
else managerInterface.onPermissionGranted("Permission Already Granted", requestCode);
}
}
這裏是我如何在MainActivity
PermissionManager permManager = PermissionManager.getInstance();
permManager.askPermission(
MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE,
mPermissionManagerInterface, // assume this exists
EXTERNAL_STORAGE_PERMISSION_CODE);
使用此類再次....這是不是永久性的。如果我允許,然後停止並重新啓動應用程序....這是再次要求我的許可。什麼可能是錯誤的?謝謝
如果您是Android的新手,請嘗試按照記錄的方式執行此操作,然後再嘗試將自己的想法付諸實踐。 – Kuffs