当前位置:网站首页>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
边栏推荐
- [in depth study of 4g/5g/6g topic -39]: urllc-10 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -3- how to distinguish urllc services? Detailed expl
- Day 6 (array example)
- day 1
- 文献阅读:GoPose 3D Human Pose Estimation Using WiFi
- Integer
- Common problems of multithreading and concurrent programming (to be continued)
- How to convert the world coordinates of unity's camera into view matrix
- In the spring of domestic databases
- day 2
- Get module handle / base address
猜你喜欢

Database index: index is not a panacea

Leetcode652 finding duplicate subtrees

High speed ASIC packaging trends: integration, SKU and 25g+

Reading notes: you only look once:unified, real time object detection
Cmake series tutorial 2 HelloWorld

Convolutional Neural Networks in TensorFlow quizs on Coursera
思源笔记 v2.1.2 同步问题

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)

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

原反补及大小端
随机推荐
【校招面经】8道指针面试真题,快来检测自己掌握了几道。
[face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.
Siyuan notes V2.1.2 synchronization problem
day 2
Mysql8.0 learning record 20 - trigger
Tupu software digital twin civil aviation flight networking, building a new business form of smart Civil Aviation
Analysis of the basic concept of digital warehouse
asp. Net core, C # summary about path
Day 4 (item 1: household income and expenditure records)
profile环境切换
LSTM and Gru of RNN_ Attention mechanism
Day 5 (array)
Free and open source website navigation source code collection, sorting and summary - self built personal navigation Homepage
Cesium uses czml to implement dynamic routes
Mysql database, subquery, union, limit
【无标题】
PCI express physical layer - electrical part
Meshlab & PCL ISS key points
OPENGL学习(二)OPENGL渲染管线
Common problems of multithreading and concurrent programming (to be continued)