当前位置:网站首页>动态获取权限
动态获取权限
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","获取权限失败");
}
}
}
边栏推荐
- Why don't I have a rookie medal
- 使用并行可微模拟加速策略学习
- 八大排序
- Preliminary summary of structure
- Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
- 7-11 calculation of residential water charges by sections
- Exercise 10-1 judge the three digits that meet the conditions
- SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
- Stop asking yourself if you are suitable for software testing
- JVM class loading
猜你喜欢
Understand the application scenario and implementation mechanism of differential segment
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
Exercise 6-6 use a function to output an integer in reverse order
Exercise 10-8 recursive implementation of sequential output of integers
JVM class loading
Jiuyi cloud black free encryption free version source code
如何查询淘宝天猫的宝贝类目
Page generation QR code
7-10 calculate salary
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
随机推荐
7-8 overspeed judgment
Find the sum of the elements of each row of the matrix
Learn to punch in today
fpga阻塞赋值和非阻塞赋值
ZABBIX saves the page blank after adding calculated items
Article content typesetting and code highlighting
7-24 reduction of the simplest fraction (rolling Division)
protobuf与grpc
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
556. 下一个更大元素 III
Back to top implementation
别再问自己适不适合做软件测试了
Solve the problem of dormitory router campus network sharing login
Toast UI editor (editor allows you to edit your markup document using text or WYSIWYG, with syntax highlighting, scrolling synchronization, real-time preview and chart functions.)
Exercise 8-7 string sorting
Solution to failure or slow downloading of electron when electron uses electron builder to package
Exercise 10-1 judge the three digits that meet the conditions
Jiuyi cloud black free encryption free version source code
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Page generation QR code