当前位置:网站首页>动态获取权限
动态获取权限
2022-07-03 14:15:00 【菜鸟xiaowang】
1.在manifest中声明需要的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />2.在activity中动态申请权限
public void getPermission(){
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
}else {
Log.e("tag","已拥有权限");
}
}else {
Log.e("tag","不用动态申请权限");
}
}3.activity 重写onRequestPermissionResult 方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0){
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.e("tag","获取权限成功");
}else {
Log.e("tag","获取权限失败");
}
}
}边栏推荐
- Zabbix添加Calculated items后保存页面成空白
- Solve the problem of dormitory router campus network sharing login
- Sendmail无法发送邮件及发送过慢解决
- 7-23 currency conversion (using array conversion)
- Selective sorting
- Understand the application scenario and implementation mechanism of differential segment
- How to delete an attribute or method of an object
- Exercise 6-1 classify and count the number of characters
- Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
- Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
猜你喜欢

7-8 overspeed judgment

牛客网:过河卒

Jiuyi cloud black free encryption free version source code

必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿

中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿

Exercise 8-8 moving letters

Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?

Page generation QR code

Redis: commandes d'action pour les données de type chaîne

7-11 calculation of residential water charges by sections
随机推荐
JS download files through URL links
fpga阻塞赋值和非阻塞赋值
Exercise 10-8 recursive implementation of sequential output of integers
String substitution
Duet date picker (time plug-in that can manually enter the date)
Sendmail无法发送邮件及发送过慢解决
Exercise 9-1 time conversion
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Common plug-ins for vite project development
一文了解微分段应用场景与实现机制
MongoDB索引
Webpage connection database ~ simple implementation of addition, deletion, modification and query complete code
7-17 crawling worms (break exercise)
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
JS first summary
Leetcode(4)——寻找两个正序数组的中位数
全文检索引擎Solr系列—–全文检索基本原理
JS get DPI, PX to cm, cm to PX
JS shift operators (< <,> > and > > >)
剑指 Offer 28. 对称的二叉树