在扫码功能中使用PermissionsAndroid请求相机权限没有弹出权限弹窗并且报警告:"Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity",使用代码如下:
let array = new Array(PermissionsAndroid.PERMISSIONS.CAMERA,PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
PermissionsAndroid.requestMultiple(array)
.then(res => {
console.log("权限返回",res);
if(res["android.permission.CAMERA"] !== 'granted' || res["android.permission.WRITE_EXTERNAL_STORAGE"] !== 'granted') {
Alert.alert('提示', '请在手机的"设置-应用-授权管理"设置相机及存储权限');
}else NativeModules.RNDiyModule.startActivity();
});
该怎么解决呢?
解决方法:
网上查了资料,找到一个亲测好用的方法,使activity implement PermissionAwareActivity,然后重写以下4个方法:
int checkPermission(String permission, int pid, int uid);
int checkSelfPermission(String permission);
boolean shouldShowRequestPermissionRationale(String permission);
void requestPermissions(String[] permissions, int requestCode, PermissionListener listener);
并且定义 Callback 和 PermissionListener。
代码如下:
public class MainActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler,PermissionAwareActivity {
ReactRootView reactRootView;
ReactInstanceManager mReactInstanceManager;
private @Nullable Callback mPermissionsCallback;
private @Nullable PermissionListener mPermissionListener;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
reactRootView = (ReactRootView) findViewById(R.id.root);
mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
...
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public Resources getResources() {
Resources res = super.getResources();
...
return res;
}
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostPause(this);
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this, this);
}
if (mPermissionsCallback != null) {
mPermissionsCallback.invoke();
mPermissionsCallback = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy();
}
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
@Override
public int checkPermission(String permission, int pid, int uid) {
return super.checkPermission(permission, pid, uid);
}
@Override
public int checkSelfPermission(String permission) {
return super.checkSelfPermission(permission);
}
@Override
public boolean shouldShowRequestPermissionRationale(@android.support.annotation.NonNull String permission) {
return super.shouldShowRequestPermissionRationale(permission);
}
@Override
@TargetApi(Build.VERSION_CODES.M)
public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
mPermissionListener = listener;
requestPermissions(permissions, requestCode);
}
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
mPermissionsCallback = new Callback() {
@Override
public void invoke(Object... args) {
if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
mPermissionListener = null;
}
}
};
}
}
这样就可以正常使用了~
网友评论