当前位置:网站首页>6.0 holes stepped by fragment request permission in the system
6.0 holes stepped by fragment request permission in the system
2022-07-24 19:24:00 【Martin-Rayman】
6.0 It's been a long time since I came out , The project is not equipped 6.0 Caused a lot of permissions BUG. After that, I saw Hong Yang's Blog, Basically know how to request permission , Administrative authority .
But recently found in Fragment There is something wrong with the permission request in .
There are two pits : One is Fragment The way of request in , The second one is Fragment Request permission callback in .
How to achieve Fragment Permission requests in
see Blog We all know , Permission can be requested in the following ways , The simulated request here is GPS Location jurisdiction
private void checkBlueToothPermission() {
if (Build.VERSION.SDK_INT >= 23) {
boolean hasLocationPermission =
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
if (!hasLocationPermission) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_ENABLE_BT);
}
}
}
But in Fragment We can't pass ActivityCompat.requestPermissions To request , Because of this paper request Fragment Can't receive onRequestPermissionsResult. So we should directly use XXXfragment.this.requestPermissions To request permission .
Fragment Request permission callback
Maybe I wonder , Maybe everyone dalao The project will not have such problems , But this problem happened in my project . I'm asking for it according to the above XXXfragment.this.requestPermissions To request permission , But my Fragment Still can't receive onRequestPermissionsResult Callback .
Observe the project and find , In this Fragment Of Activity There are the same permission requests , There are also onRequestPermissionsResult. But every time I ask , Will be Activity Of onRequestPermissionsResult Intercepted , and Fragment There will never be onRequestPermissionsResult Callback .
So I found out , stay Activity Medium onRequestPermissionsResult No call to super.onRequestPermissionsResult(requestCode, permissions, grantResults); When I add it, there is a magical effect ,Fragment You can receive onRequestPermissionsResult It's called back !!!
I found that the source code did something for me :
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
int index = (requestCode>>16)&0xffff;
if (index != 0) {
index--;
// there mPendingFragmentActivityResults It's using SparseArrayCompat structure
// Store requestCode And specific requests Fragment.
String who = mPendingFragmentActivityResults.get(index);
mPendingFragmentActivityResults.remove(index);
if (who == null) {
Log.w(TAG, "Activity result delivered for unknown Fragment.");
return;
}
Fragment frag = mFragments.findFragmentByWho(who); // find Fragment And callback
if (frag == null) {
Log.w(TAG, "Activity result no fragment exists for who: " + who);
} else {
frag.onRequestPermissionsResult(requestCode&0xffff, permissions, grantResults);
}
}
} // Store requestCode And specific requests Fragment.
String who = mPendingFragmentActivityResults.get(index);
mPendingFragmentActivityResults.remove(index);
if (who == null) {
Log.w(TAG, "Activity result delivered for unknown Fragment.");
return;
}
Fragment frag = mFragments.findFragmentByWho(who); // find Fragment And callback
if (frag == null) {
Log.w(TAG, "Activity result no fragment exists for who: " + who);
} else {
frag.onRequestPermissionsResult(requestCode&0xffff, permissions, grantResults);
}
}
}
Remember Activity Permission is also requested in , Remember in onRequestPermissionsResult Add... To the callback super The callback .
Don't accidentally delete the callback like me and look confused
边栏推荐
- Wireshark simple filter rule
- 原反补及大小端
- 思源笔记 v2.1.2 同步问题
- JDBC batch inserts 100000 /1million pieces of data
- Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
- day 2
- Channel state information (CSI) conjugate multiplication denoising method
- Anaconda installs labelimg (super simple and available)
- Timed task framework
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)
猜你喜欢

多线程与并发编程常见问题(未完待续)

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)

day 3

Tencent Browser service TBS usage

Analysis of the basic concept of digital warehouse

Day 9 (this keyword and experiment)

What are the benefits of knowledge management in enterprises?

MySQL1

OPENGL学习(二)OPENGL渲染管线

Convolutional neural network CNN
随机推荐
C language implementation of raii
OpenGL learning (IV) glut 3D image rendering
[question 39] special question for Niuke in-depth learning
Biopharmaceutical safety, power supply and production guarantee
On dynamic application of binary array
原反补及大小端
Crazy God redis notes 11
On July 31, 2022, the dama-cdga/cdgp data governance certification class was opened!
Hold the C pointer
Machine learning_ Softmax function (multi classification problem)
Convolutional neural network CNN
Day 5 (array)
Summary of articles in 2020
Onemanager and cloudflare workers deployment and installation - binding domain names and using cloudflare CDN acceleration
In the spring of domestic databases
He has been in charge of the British Society of engineering and technology for 13 years, and van nugget officially retired
Meshlab&PCL ISS关键点
【校招面经】8道指针面试真题,快来检测自己掌握了几道。
How to encrypt your own program with dongle
第4章 复合类型