Permission
https://developer.android.com/training/permissions/requesting
https://developer.android.com/guide/topics/permissions/overview#normal-dangerous
权限管理
Normal permissions
Dangerous permissions

Android11权限申请
申请位置权限,先申请前台,后申请后台权限。
https://zhuanlan.zhihu.com/p/275758740
基本使用
1 | public static final int PERMISSION_REQUEST_CODE = 1000; |
注意:需要在androidmanifest.xml声明 否则一直返回false
1 | <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> |
1、检查权限是否授予。
1 | Activity.java |
2、申请权限。
1 | Activity.java |
这个时候,会弹出系统授权弹窗(授权弹窗是不支持自定义的,原因理所当然)。
3、权限回调。
用户在系统弹窗里面选择后,结果会通过Activity
的 onRequestPermissionsResult
方法回调APP。
1 | public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
4、权限说明。
用户如果选择了拒绝,下一次在需要声明该权限的时候,Google建议APP开发者给予用户更多的说明,因此提供了下面这个API,这个方法返回值在使用过程中会发现有点纠结(具体解析见下面代码块说明)。
1 | public boolean shouldShowRequestPermissionRationale(permission) |
这个 看情况选用
参考 https://juejin.im/entry/58b2e490ac502e0069d9ae62
https://developer.android.com/guide/topics/security/permissions?hl=zh-cn#normal-dangerous
http://www.10tiao.com/html/227/201610/2650237473/1.html
PERMISSION
1 | <!-- 这个权限用于进行网络定位--> |
One-time permissions
Starting in Android 11 (API level 30), whenever your app requests a permission related to location, microphone, or camera, the user-facing permissions dialog contains an option called Only this time. If the user selects this option in the dialog, your app is granted a temporary one-time permission.
Permission groups
If the app has already been granted another dangerous permission in the same permission group, the system immediately grants the permission without any interaction with the user. For example, if an app had previously requested and been granted the
READ_CONTACTS
permission, and it then requestsWRITE_CONTACTS
, the system immediately grants that permission without showing the permissions dialog to the user.Caution: Future versions of the Android SDK might move a particular permission from one group to another. Therefore, don’t base your app’s logic on the structure of these permission groups.
For example,
READ_CONTACTS
is in the same permission group asWRITE_CONTACTS
as of Android 8.1 (API level 27). If your app requests theREAD_CONTACTS
permission, and then requests theWRITE_CONTACTS
permission, don’t assume that the system can automatically grant theWRITE_CONTACTS
permission.
多权限申请
https://github.com/permissions-dispatcher/PermissionsDispatcher
https://github.com/googlesamples/android-RuntimePermissions
https://www.jianshu.com/p/a51593817825
https://developer.android.com/training/permissions/requesting
livedata
https://mp.weixin.qq.com/s/Hw_FI0GRpUDWJ1NwjK652w
https://mp.weixin.qq.com/s/7RpGzTjXo9rnHRCVEnrYWQ
https://mp.weixin.qq.com/s/i8K-6CSxYuLanR4x1WNSGA
https://mp.weixin.qq.com/s/gaSmpT5UQLqNa4Ck0r0eOg
http://blog.csdn.net/huaiyiheyuan/article/details/52473984
https://developer.android.com/training/permissions/requesting.html
Android 10权限
在后台运行时访问设备位置信息需要权限
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
该权限允许应用程序在后台访问位置。如果请求此权限,则还必须请求ACCESS_FINE_LOCATION
或 ACCESS_COARSE_LOCATION
权限。只请求此权限无效果
在Android 10的设备上,如果你的应用的 targetSdkVersion
< 29,则在请求ACCESS_FINE_LOCATION
或ACCESS_COARSE_LOCATION
权限时,系统会自动同时请求ACCESS_BACKGROUND_LOCATION
。在请求弹框中,选择“始终允许”表示同意后台获取位置信息,选择“仅在应用使用过程中允许”或”拒绝”选项表示拒绝授权。
如果你的应用的 targetSdkVersion
>= 29,则请求ACCESS_FINE_LOCATION
或 ACCESS_COARSE_LOCATION
权限表示在前台时拥有访问设备位置信息的权限。在请求弹框中,选择“始终允许”表示前后台都可以获取位置信息,选择“仅在应用使用过程中允许”只表示拥有前台的权限。
https://developer.android.com/about/versions/10/privacy/changes
https://juejin.im/post/6844904073024503822#heading-5
request permission demo
https://github.com/android/platform-samples/tree/main/samples/privacy/permissions
Custom Permission
can run Demo
https://github.com/Ibtisam/Dangerous-Activity
https://github.com/Ibtisam/Using-Dangerous-Activity
https://github.com/commonsguy/cwac-netsecurity
https://www.youtube.com/watch?v=YeLmufMZfcI
https://stackoverflow.com/questions/28975706/start-activity-with-permissions
https://stackoverflow.com/questions/26700767/custom-permission-for-activity
Sensors
An acceleration sensor measures the acceleration applied to the device, including the force of gravity. The following code shows you how to get an instance of the default acceleration sensor:
1 | val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager |
https://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-accel
If you are publishing your application on Google Play you can use the
1 | <uses-feature android:name="android.hardware.sensor.accelerometer" |