我正在尝试在我的项目中实现PermissionsDispatcher
库,但它对我的项目无法正常工作,并且在阅读了更多的库源代码并实现项目之后不要求权限工作不成功
@RuntimePermissions
public class ActivityContactLists extends AppCompatActivity {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_lists);
...
long contact_count = realm.where(ContactLists.class).count();
if (contact_count > 0 && !checkNetWorkConnection() && !connectingToServer) {
reloadFromDatabase();
} else {
MainActivityPermissionsDispatcher.showContactsWithCheck(this);
syncByServer();
}
}
private void reloadFromDatabase() {
}
/* MUST ASK PERMISSION REQUEST */
@NeedsPermission({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS})
public void syncByServer() {
/* READ CONTACTS FROM PHONE DATABASE */
}
@OnClick(R.id.syncronizeContacts)
public void syncronizeContacts(View view) {
syncByServer();
}
@OnShowRationale({Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS})
void showRationaleForContact(PermissionRequest request) {
showRationaleDialog(R.string.permission_contacts_rationale, request);
}
private void showRationaleDialog(@StringRes int messageResId, final PermissionRequest request) {
new AlertDialog.Builder(this)
.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
request.proceed();
}
})
.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
request.cancel();
}
})
.setCancelable(false)
.setMessage(messageResId)
.show();
}
}
和MainActivityPermissionsDispatcher
类一起处理Permission,就像使用库sampple代码一样:
final class MainActivityPermissionsDispatcher {
private static final int REQUEST_SHOWCONTACTS = 1;
private static final String[] PERMISSION_SHOWCONTACTS = new String[]{
"android.permission.READ_CONTACTS",
"android.permission.WRITE_CONTACTS"
};
private MainActivityPermissionsDispatcher() {
}
static void showContactsWithCheck(ActivityContactLists target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCONTACTS)) {
target.syncByServer();
}
if (PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_SHOWCONTACTS)) {
target.showRationaleForContact(new ShowContactsPermissionRequest(target));
} else {
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCONTACTS, REQUEST_SHOWCONTACTS);
}
}
static void onRequestPermissionsResult(ActivityContactLists target, int requestCode, int[] grantResults) {
switch (requestCode) {
case REQUEST_SHOWCONTACTS:
if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCONTACTS)) {
return;
}
if (PermissionUtils.verifyPermissions(grantResults)) {
target.syncByServer();
}
break;
default:
break;
}
}
private static final class ShowContactsPermissionRequest implements PermissionRequest {
private final WeakReference<ActivityContactLists> weakTarget;
private ShowContactsPermissionRequest(ActivityContactLists target) {
this.weakTarget = new WeakReference<>(target);
}
@Override
public void proceed() {
ActivityContactLists target = weakTarget.get();
if (target == null) return;
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCONTACTS, REQUEST_SHOWCONTACTS);
}
@Override
public void cancel() {
}
}
}
我找不到不问许可的问题