当前位置:网站首页>Get permissions dynamically
Get permissions dynamically
2022-07-03 14:27:00 【Novice Xiaowang】
1. stay manifest Required permissions declared in
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. stay activity Dynamically apply for permission in
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"," Already have permission ");
}
}else {
Log.e("tag"," Don't apply for permission dynamically ");
}
}
3.activity rewrite onRequestPermissionResult Method
@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"," Permissions obtained successfully ");
}else {
Log.e("tag"," Failed to get permission ");
}
}
}
边栏推荐
猜你喜欢
Protobuf and grpc
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
一文了解微分段应用场景与实现机制
洛谷P4047 [JSOI2010]部落划分 题解
Sword finger offer 28 Symmetric binary tree
X86 assembly language - Notes from real mode to protected mode
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Leetcode(4)——寻找两个正序数组的中位数
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
随机推荐
Leetcode(4)——尋找兩個正序數組的中比特數
泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
How to bold text in AI
X86 assembly language - Notes from real mode to protected mode
Common commands for getting started with mongodb database
puzzle(016.3)千丝万缕
Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
JVM garbage collector
Convert string to decimal integer
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
Recent learning summary
战略、战术(和 OKR)
Understand the application scenario and implementation mechanism of differential segment
Why don't I have a rookie medal
Exercise 6-1 classify and count the number of characters
洛谷P3065 [USACO12DEC]First! G 题解
愉悦资本新双币基金近40亿元完成首次关账
Find specified characters
Exercise 8-8 moving letters