当前位置:网站首页>动态获取权限
动态获取权限
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","获取权限失败");
}
}
}边栏推荐
- Exercise 10-6 recursively find Fabonacci sequence
- 7-2 and then what time (15 minutes)
- Exercise 10-1 judge the three digits that meet the conditions
- protobuf与grpc
- MongoDB数据库入门的常用命令
- Redis: operation command of string type data
- Why is this error reported when modifying records in the database
- JS matrix zero
- Find the sum of the elements of each row of the matrix
- Too many files with unapproved license
猜你喜欢

Understanding of closures

7-18 finding the single root of polynomial by dichotomy

The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log

GRPC的四种数据流以及案例

Eight sorts

Exercise 10-1 calculate the sum of 1 to n using recursive functions

Exercise 6-1 classify and count the number of characters

Article content typesetting and code highlighting

如何查询淘宝天猫的宝贝类目

JVM object lifecycle
随机推荐
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
The mail function of LNMP environment cannot send mail
How to bold text in AI
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
Raft agreement
Thread. Sleep and timeunit SECONDS. The difference between sleep
js 2023. String pair equal to the target string after connection
常见问题之PHP——ldap_add(): Add: Undefined attribute type in
x86汇编语言-从实模式到保护模式 笔记
7-19 check denomination (solve binary linear equation)
Preliminary summary of structure
Too many files with unapproved license
Fabric. JS document
Current situation, analysis and prediction of information and innovation industry
JVM垃圾回收机
js . Find the first palindrome string in the array
Redis:字符串類型數據的操作命令
适用于XP的DDK
使用并行可微模拟加速策略学习
Exercise 8-8 moving letters