uni-app Android 6.0 动态权限申请代码分享
uni-app Android 6.0 动态权限申请代码分享
/*
正常权限,无需动态申请:
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
2)危险权限,需要动态申请:
group:android.permission-group.STORAGE
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
group:android.permission-group.CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
READ_CONTACTS
group:android.permission-group.PHONE
READ_CALL_LOG
READ_PHONE_STATE
CALL_PHONE
WRITE_CALL_LOG
USE_SIP
PROCESS_OUTGOING_CALLS
com.android.voicemail.permission.ADD_VOICEMAIL
group:android.permission-group.CALENDAR
READ_CALENDAR
WRITE_CALENDAR
group:android.permission-group.CAMERA
CAMERA
group:android.permissiongroup.SENSORS
BODY_SENSORS
group:android.permission-group.LOCATION
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
group:android.permission-group.MICROPHONE
RECORD_AUDIO
group:android.permission-group.SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
RECEIVE_SMS
SEND_SMS
READ_CELL_BROADCASTS
*/
/**
- Me:
- cnscn 214363570@qq.com
- 参考:
- https://blog.csdn.net/lvyoujt/article/details/52826556
- https://developer.android.com/reference/android/Manifest.permission
- https://developer.android.com/reference/android/os/Build.VERSION
*/
function plusReady() {
var Build = plus.android.importClass(“android.os.Build”);
var Manifest = plus.android.importClass(“android.Manifest”);
var MainActivity = plus.android.runtimeMainActivity();
//var context=main.getApplicationContext(); //未用到,在此仅供参考
var ArrPermissions = [
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
];
function PermissionCheck(permission) {
if (Build.VERSION.SDK_INT >= 23) {
if (MainActivity.checkSelfPermission(permission) == -1) {
return false;
}
}
return true;
}
function PermissionChecks(Arr) {
var HasPermission = true;
for (var index in Arr) {
var permission = Arr[index];
//如果此处没有权限,则是用户拒绝了
if (!PermissionCheck(permission)) {
HasPermission = false;
break;
}
}
return HasPermission;
}
function PermissionRequest(Arr) {
var REQUEST_CODE_CONTACT = 101;
if (Build.VERSION.SDK_INT >= 23) {
MainActivity.requestPermissions(Arr, REQUEST_CODE_CONTACT);
}
}
//如果没有权限,则申请
if (!PermissionChecks(ArrPermissions)) {
PermissionRequest(ArrPermissions);
} else { //如果拥有权限,那么干点啥吧^_^
//.......
}
}
if (window.plus) {
plusReady();
} else {
document.addEventListener(‘plusready’, plusReady, false);
}
![](https://www.itying.com/uniimg.php?url=https://img-cdn-tc.dcloud.net.cn/uploads/article/20181013/be30aef0559e9733ccb1970a0e73df37.PNG)
在uni-app中处理Android 6.0及以上版本的动态权限申请,可以通过在原生插件或者自定义组件中实现权限请求逻辑,然后在uni-app中调用这些原生接口。以下是一个基于uni-app和Android原生插件实现的动态权限申请示例代码。
1. 创建Android原生插件
首先,创建一个Android原生插件来请求权限。
步骤一:创建插件项目
在native-plugins
目录下创建一个新文件夹,比如AndroidPermissionPlugin
。
步骤二:编写插件代码
在AndroidPermissionPlugin
文件夹中,创建AndroidPermissionPlugin.java
文件,内容如下:
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
public class AndroidPermissionPlugin implements WXModule {
private Context mContext;
@Override
public void initialize(Context context) {
mContext = context;
}
public void requestPermissions(String[] permissions, JSCallback callback) {
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((android.app.Activity) mContext, permissions, 1);
callback.invokeAndKeepAlive(false); // Permission not granted yet
} else {
callback.invokeAndKeepAlive(true); // Permission already granted
}
}
}
2. 配置插件
在manifest.json
中配置原生插件:
{
"nativePlugins": {
"AndroidPermissionPlugin": {
"package": "com.yourpackage.AndroidPermissionPlugin",
"methods": [
{
"name": "requestPermissions",
"args": [
{
"type": "array",
"subType": "string"
},
{
"type": "function"
}
]
}
]
}
}
}
3. 在uni-app中调用插件
在uni-app的页面中调用这个插件:
export default {
methods: {
requestAndroidPermissions() {
const permissions = ['android.permission.CAMERA'];
uni.requireNativePlugin('AndroidPermissionPlugin').requestPermissions(permissions, (result) => {
if (result) {
console.log('Permissions granted');
} else {
console.log('Permissions not granted yet');
}
});
}
},
onLoad() {
this.requestAndroidPermissions();
}
}
总结
上述代码展示了如何在uni-app中通过Android原生插件实现动态权限申请。需要注意的是,这只是一个简单的示例,实际项目中可能需要处理更多的权限类型和回调逻辑。此外,对于iOS平台,权限申请的逻辑会有所不同,需要使用iOS的原生开发语言(如Objective-C或Swift)来实现。