B4A 安卓开发(VB语言)储存权限申请(三大类型权限)
作者:VB小源码 微信:vbee_club 转载请注明出处
危险权限申请图示:
特殊权限申请图示:
Android 的权限大致分为三种:
- 普通权限:只需要在清单编辑器Manifest Editor中添加即可
- 危险权限:需要在B4A代码中使用RuntimePermissions动态申请,以Dialog 弹窗形式进行请求
- 特殊权限:需要在代码中动态申请,以系统 Activity 的形式进行请求
而我们今天要讲的主题,是关于存储权限,在 Android 6.0 之后就变成了危险权限,而到了 Android 11 上面变成了特殊权限,而最明显的区别是一个是通过 Dialog 展示给用户看,另外一个是通过 Activity 展现给用户看。
那么在B4A中如何是申请适应不同版本的API的储存权限呢?
- 1、普通权限:在Android 6.0之前(含Android 6.0),即API<=23之前,直接在Manifest Editor 编辑器里面添加相应的权限清单即可:
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
''添加储存权限
AddPermission(android.permission.READ_EXTERNAL_STORAGE) ''读取储存
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE) ''写入储存
- 2、危险权限:在Android 7.0到Android 10.0,即 29>API>23 之前,除了要在Manifest Editor 编辑器里面添加相应的权限清单外,还需要在 Activity 代码页添加Dialog权限动态申请
代码语言:javascript复制清单代码
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
''添加储存权限
AddPermission(android.permission.READ_EXTERNAL_STORAGE) ''读取储存
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE) ''写入储存
Activity代码
引用RuntimePermissions类库
代码语言:javascript复制Sub Process_Globals
Private rp As RuntimePermissions ''动态申请权限
End Sub
Sub Activity_Create(FirstTime As Boolean)
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE) ''动态申请储存读取权限
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ''动态申请储存写入权限
End Sub
''注意需要添加权限申请返回事件,否则报错闪退
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
Log(Permission)
End Sub
- 3、特殊权限:在Android 10.0以上,即 API>=29 之前,除了要在Manifest Editor 编辑器里面添加相应的权限清单外,还需要在 Activity 代码页添加Dialog权限动态申请,还需要添加 Activity 权限申请代码
代码语言:javascript复制清单代码
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
''添加权限
AddPermission(android.permission.READ_EXTERNAL_STORAGE) ''读取储存
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE) ''写入储存
''api>=29
AddPermission(android.permission.MANAGE_EXTERNAL_STORAGE) ''特殊权限
SetApplicationAttribute(android:requestLegacyExternalStorage, true)
Activity代码清单
1.引用RuntimePermissions类库
2.引用JavaObject类库
3.引用Phone类库
- 新建标准类库
#Event: StorageAvailable
Sub Class_Globals
Private ion As Object
Private mCallback As Object
Private mEventName As String
End Sub
Public Sub Initialize (Callback As Object, EventName As String)
mCallback = Callback
mEventName = EventName
End Sub
Public Sub HasPermission As Boolean
Dim has As Boolean
Dim jo As JavaObject
jo.InitializeStatic("android.os.Environment")
has = jo.RunMethod("isExternalStorageManager", Null)
Return has
End Sub
Public Sub GetPermission
If HasPermission Then
RaiseEvent
Return
End If
Dim in As Intent
in.Initialize("android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION", "package:" & Application.PackageName)
StartActivityForResult(in)
End Sub
Private Sub RaiseEvent
CallSubDelayed(mCallback, mEventName & "_StorageAvailable")
End Sub
Private Sub ion_Event (MethodName As String, Args() As Object) As Object
RaiseEvent
Return Null
End Sub
Private Sub StartActivityForResult(i As Intent)
Dim jo As JavaObject = GetBA
ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub
Private Sub GetBA As Object
Dim jo As JavaObject = Me
Return jo.RunMethod("getBA", Null)
End Sub
代码语言:javascript复制Activity代码
Sub Process_Globals
Private xui As XUI
Private rp As RuntimePermissions
Dim Phone As Phone
Dim AP As ActivityPermission
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
''申请特殊权限
If FirstTime Then
AP.Initialize(Me, "AP")
End If
Dim SdkVersion As Int = Phone.SdkVersion
If SdkVersion < 30 Then
Dim rp As RuntimePermissions
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
Else
If Not(AP.HasPermission) Then
MsgboxAsync("应用即将申请访问所有文件,是否允许?", "文件访问")
Wait For Msgbox_Result(Res As Int)
AP.GetPermission
Wait For MES_StorageAvailable
End If
End If
''权限申请
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
End Sub
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
Log(Permission)
End Sub
好了今天就介绍到这里了
实例下载:
代码语言:javascript复制链接:https://pan.baidu.com/s/1CvD_RbkgkTDQxJDxFhPFVw
提取码:vbee