
目录
1. 概述
2. 自定义的service
3. 系统service
(1) 调用 ContextWrapper.checkSelfPermission 确认是否有权限。 如果没有授权,则调用 Activity.requestPermissions
(2) onRequestPermissionsResult 回调结果
1. 概述
当使用一些服务时,涉及到用户隐私时(如相机、联系人、电话权限等),一般需要用户手动授权。用户授权后,才能进行 *** 作。以此来保证用户隐私。
2. 自定义的service对于自定义的服务, 在的AndroIDManifest 会加上权限限定, 例如
<service androID:name=".SimpleService" androID:permission="com.sample.simpleservice.permission.CALLME"></service>其中"com.sample.simpleservice.permission.CALLME" 是自定义的约定的权限
而用调用这个service 的应用ClIEnt中,就要申明使用到这个权限:
<uses-permission androID:name="com.sample.simpleservice.permission.CALLME"/>3. 系统service如 TelecomManager.endCall,
参考 AndroID 四大组件之 Service_4_挂断电话(AIDL): https://blog.csdn.net/whjk20/article/details/112983081
这里调用endCall 需要AndoID.Manifest.java 中的权限
public static final String ANSWER_PHONE_CALLS = "androID.permission.ANSWER_PHONE_CALLS";因此,需要提示用户授权。
(1) 调用 Contextwrapper.checkSelfPermission 确认是否有权限。 如果没有授权,则调用 Activity.requestPermissions private voID checkPhoneCallPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (this.checkSelfPermission(Manifest.permission.ANSWER_PHONE_CALLS) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ANSWER_PHONE_CALLS}, REQUEST_ANSWER_PHONE_CALLS_CODE); } } }注意 requestPermissions 的详细说明, 需要判断:
是否已经获取了权限是否已经拒绝了权限以上为否,才进行调用requestPermissions需要传入权限名称,以及代码(自定义)。 并且结果会通过回调 Activity.onRequestPermissionsResult 通知
/** * Requests permissions to be granted to this application. These permissions * must be requested in your manifest, they should not be granted to your app, * and they should have protection level {@link * androID.content.pm.PermissionInfo#PROTECTION_DANGEROUS dangerous}, regardless * whether they are declared by the platform or a third-party app. * <p> * normal permissions {@link androID.content.pm.PermissionInfo#PROTECTION_norMAL} * are granted at install time if requested in the manifest. Signature permissions * {@link androID.content.pm.PermissionInfo#PROTECTION_SIGNATURE} are granted at * install time if requested in the manifest and the signature of your app matches * the signature of the app declaring the permissions. * </p> * <p> * Call {@link #shouldShowRequestPermissionRationale(String)} before calling this API to * check if the system recommends to show a rationale UI before asking for a permission. * </p> * <p> * If your app does not have the requested permissions the user will be presented * with UI for accepting them. After the user has accepted or rejected the * requested permissions you will receive a callback on {@link * #onRequestPermissionsResult(int, String[], int[])} reporting whether the * permissions were granted or not. * </p> * <p> * Note that requesting a permission does not guarantee it will be granted and * your app should be able to run without having this permission. * </p> * <p> * This method may start an activity allowing the user to choose which permissions * to grant and which to reject. Hence, you should be prepared that your activity * may be paused and resumed. Further, granting some permissions may require * a restart of you application. In such a case, the system will recreate the * activity stack before delivering the result to {@link * #onRequestPermissionsResult(int, String[], int[])}. * </p> * <p> * When checking whether you have a permission you should use {@link * #checkSelfPermission(String)}. * </p> * <p> * Calling this API for permissions already granted to your app would show UI * to the user to decIDe whether the app can still hold these permissions. This * can be useful if the way your app uses data guarded by the permissions * changes significantly. * </p> * <p> * You cannot request a permission if your activity sets {@link * androID.R.styleable#AndroIDManifestActivity_noHistory noHistory} to * <code>true</code> because in this case the activity would not receive * result callbacks including {@link #onRequestPermissionsResult(int, String[], int[])}. * </p> * <p> * The <a href="https://github.com/Googlesamples/android-runtimePermissions"> * RuntimePermissions</a> sample app demonstrates how to use this method to * request permissions at run time. * </p> * * @param permissions The requested permissions. Must me non-null and not empty. * @param requestCode Application specific request code to match with a result * reported to {@link #onRequestPermissionsResult(int, String[], int[])}. * Should be >= 0. * * @throws IllegalArgumentException if requestCode is negative. * * @see #onRequestPermissionsResult(int, String[], int[]) * @see #checkSelfPermission(String) * @see #shouldShowRequestPermissionRationale(String) */ public final voID requestPermissions(@NonNull String[] permissions, int requestCode) {(2) onRequestPermissionsResult 回调结果 @OverrIDe public voID onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_ANSWER_PHONE_CALLS_CODE) { if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {//已获取了 Toast.makeText(this, "Have grant permission: ANSWER_PHONE_CALLS", Toast.LENGTH_SHORT).show(); } else {//没有允许,也没有拒绝过 Toast.makeText(this, "ANSWER_PHONE_CALLS permission was NOT granted", Toast.LENGTH_SHORT).show(); } } }其中: PackageManager.java中定义
public static final int PERMISSION_GRANTED = 0;public static final int PERMISSION_DENIED = -1;在获取了结果后,再做相应处理,例如启动camera等等
参考Github上AndroID 官方的例子:
https://github.com/Googlesamples/android-runtimePermissions
(MainActivity.java)
总结以上是内存溢出为你收集整理的Android 四大组件之 Activity_ 权限验证全部内容,希望文章能够帮你解决Android 四大组件之 Activity_ 权限验证所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)