2023年11月23日发(作者:)
Android动态获取系统权限+调⽤外部应⽤读取本地⽂件(TBS+⾃定义)
⼀、获取动态权限
1.创建Android应⽤,在配置⽂件中添加临时访问⽂件权限
<provider
android:name="ovider"
android:authorities="_input"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
provider>
新建xml⽂件,配置name和path路径
xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="camera_photos"
path="/" />
paths>
2.配置系统需要⽤到的系统权限
<uses-permission android:name="_EXTERNAL_STORAGE" />
<uses-permission android:name="_EXTERNAL_STORAGE" />
<uses-permission
android:name="_SETTINGS"
tools:ignore="ProtectedPermissions" />
3.编写申请动态权限⽅法,在onCreate()⽅法中调⽤
//申请权限
private boolean getPermission(Activity activity){
boolean rs=false;
// 声明⼀个集合,在后⾯的代码中⽤来存储⽤户拒绝授权的权
List
String[] permissions = new String[]{
_EXTERNAL_STORAGE,//读取内存卡
_EXTERNAL_STORAGE,//写⼊内存卡
// _UNMOUNT_FILESYSTEMS,//允许挂载和反挂载⽂件系统可移动存储
// _PHONE_STATE,//读取⼿机状态和⾝份
/* _WIFI_STATE,//获取使⽤Wi-Fi等WLAN⽆线⽹络
_WIFI_STATE,//允许程序改变Wi-Fi连接状态
_NETWORK_STATE,//允许程序访问有关GSM⽹络信息
_NETWORK_STATE,//允许程序改变⽹络连接状态
ET,//访问⽹络连接,可能产⽣GPRS流量
OTH,//允许程序连接到已配对的蓝⽛设备
,//相机
_SETTINGS,//允许程序读取或写⼊系统设置*/
};
();
for (int i = 0; i < ; i++) {
if (elfPermission(activity, permissions[i]) != SION_GRANTED) {
Log.e(TAG,"权限未允许:"+permissions[i]+"....");
(permissions[i]);
}
}
if (y()) {//未授予的权限为空,表⽰都授予了
Log.e(TAG,"已经获取到了所有权限");
return true;
} else {//请求权限⽅法
String[] spermissions = y(new String[()]);//将List转为数组
tPermissions(activity, spermissions, 1);
}
return rs;
}
4.处理授权回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==1){
if (_INT >= N_CODES.M) {
boolean request_rs=true;
for(int i=0;i<;i++){
if (grantResults[i] != SION_GRANTED) {
request_rs=false;
}
}
if(request_rs){
Log.e("软件获取权限结果:","获取软件权限成功");
init();
}else{
xt(this, "您拒绝了软件应⽤权限,因此⽆法使⽤该软件!", _LONG).show();
showDialogTipUserGoToAppSettting();
}
}
}
}
若未获取的权限,则提醒⽤户⼿动开启权限
/**
* 提⽰⽤户去应⽤设置界⾯⼿动开启权限
*/
private void showDialogTipUserGoToAppSettting() {
new r(this)
.setTitle("存储权限不可⽤")
.setMessage("请在-应⽤设置-权限-中,允许应⽤使⽤存储权限来保存⽤户数据")
.setPositiveButton("⽴即开启", new kListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 跳转到应⽤设置界⾯
goToAppSetting();
ocess(());
}
})
.setNegativeButton("取消", new kListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ocess(());
}
}).setCancelable(false).show();
}
/**
* 跳转到当前应⽤的设置界⾯
*/
private void goToAppSetting() {
Intent intent = new Intent();
ion(_APPLICATION_DETAILS_SETTINGS);
Uri uri = rts("package", getPackageName(), null);
a(uri);
startActivityForResult(intent, 123);
}
⼆、调⽤应⽤打开本地⽂件
第⼀种,根据⽂件类型⾃动选取应⽤打开⽂件
1.创建⽂件类型数组
/**
* -- MIME 列表 --
*/
public static final String[][] MIME_MapTable = {
// --{后缀名, MIME类型} --
{".3gp", "video/3gpp"},{".3gpp", "video/3gpp"},{".aac", "audio/x-mpeg"},
{".amr", "audio/x-mpeg"},{".apk", "application/e-archive"},
{".avi", "video/x-msvideo"},
{".aab", "application/x-authoware-bin"},
{".aam", "application/x-authoware-map"},
{".aas", "application/x-authoware-seg"},
{".ai", "application/postscript"},
{".aif", "audio/x-aiff"},
{".aifc", "audio/x-aiff"},
{".aiff", "audio/x-aiff"},
{".als", "audio/x-alpha5"},
{".au", "audio/basic"},
{".ief", "image/ief"},
{".mrm", "application/x-mrm"},
{".si6", "image/si6"},
{".wma", "audio/x-ms-wma"},
{".wmv", "audio/x-ms-wmv"},
{".wi", "image/wavelet"},
{".wis", "application/x-InstallShield"},
{".wm", "video/x-ms-wm"},
{".wmd", "application/x-ms-wmd"},
{".wmf", "application/x-msmetafile"},
{".wml", "text/"},
{".wmlc", "application/"},
{".wmls", "text/ipt"},
{".wmlsc", "application/iptc"},
{".wmlscript", "text/ipt"},
{".wmv", "video/x-ms-wmv"},
{".wmx", "video/x-ms-wmx"},
{".wmz", "application/x-ms-wmz"},
{".wpng", "image/x-up-wpng"},
{".wps", "application/-works"},
{".wpt", "x-lml/x-gps"},
{".wri", "application/x-mswrite"},
{".wrl", "x-world/x-vrml"},
{".wrz", "x-world/x-vrml"},
{".ws", "text/ipt"},
{".wsc", "application/iptc"},
{".wv", "video/wavelet"},
{".wvx", "video/x-ms-wvx"},
{".wxl", "application/x-wxl"},
{".x-gzip", "application/x-gzip"},
String end = ing(dotIndex, ()).toLowerCase();
if (end == "") {
return type;
}
for (int i = 0; i < MIME_; i++) {
if ((MIME_MapTable[i][0])) {
type = MIME_MapTable[i][1];
}
}
return type;
}
第⼆种,调⽤TBS服务打开⽂件
1.引⼊jar包或gradle
//引⼊腾讯x5 服务
implementation ':sdk:43939'
2.初始化TBS、调⽤⽅法打开⽂件
// 在调⽤TBS初始化、创建WebView之前进⾏如下配置
HashMap map = new HashMap();
(_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
(_SETTINGS_USE_DEXLOADER_SERVICE, true);
sSettings(map);
leReader(MainActivity.this, (position).getUrl(), null, new ValueCallback
@Override
public void onReceiveValue(String s) {
// xt(getApplicationContext(),s,_SHORT).show();
}
});
~~~~~~~~~~~~~~Over~~~~~~~~~~~~~~~~~~


发布评论