在掃碼功能中使用PermissionsAndroid請(qǐng)求相機(jī)權(quán)限沒(méi)有彈出權(quán)限彈窗并且報(bào)警告:"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("權(quán)限返回",res);
if(res["android.permission.CAMERA"] !== 'granted' || res["android.permission.WRITE_EXTERNAL_STORAGE"] !== 'granted') {
Alert.alert('提示', '請(qǐng)?jiān)谑謾C(jī)的"設(shè)置-應(yīng)用-授權(quán)管理"設(shè)置相機(jī)及存儲(chǔ)權(quán)限');
}else NativeModules.RNDiyModule.startActivity();
});
該怎么解決呢昵观?
解決方法:
網(wǎng)上查了資料萧落,找到一個(gè)親測(cè)好用的方法樱蛤,使activity implement PermissionAwareActivity朱嘴,然后重寫(xiě)以下4個(gè)方法:
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;
}
}
};
}
}
這樣就可以正常使用了~