Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply for permission dynamically in Android 6. 0

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to apply for permission dynamically in Android 6.0. in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

There are two types of system permissions:

Normal permission: if there is no danger to user privacy, you can apply for authorization directly in the manifest file.

Dangerous permission:app needs to access the user's privacy information, etc., even if registered in the manifest file, but also needs to be authorized by the user when running.

Dangerous permission needs to apply for permissions separately, which is divided into 9 groups. As long as one permission is successfully applied for in each group, the whole group of permissions can be used by default.

Group:android.permission-group.CONTACTS

Permission:android.permission.WRITE_CONTACTS

Permission:android.permission.GET_ACCOUNTS

Permission:android.permission.READ_CONTACTS

Group:android.permission-group.PHONE

Permission:android.permission.READ_CALL_LOG

Permission:android.permission.READ_PHONE_STATE

Permission:android.permission.CALL_PHONE

Permission:android.permission.WRITE_CALL_LOG

Permission:android.permission.USE_SIP

Permission:android.permission.PROCESS_OUTGOING_CALLS

Permission:com.android.voicemail.permission.ADD_VOICEMAIL

Group:android.permission-group.CALENDAR

Permission:android.permission.READ_CALENDAR

Permission:android.permission.WRITE_CALENDAR

Group:android.permission-group.CAMERA

Permission:android.permission.CAMERA

Group:android.permission-group.SENSORS

Permission:android.permission.BODY_SENSORS

Group:android.permission-group.LOCATION

Permission:android.permission.ACCESS_FINE_LOCATION

Permission:android.permission.ACCESS_COARSE_LOCATION

Group:android.permission-group.STORAGE

Permission:android.permission.READ_EXTERNAL_STORAGE

Permission:android.permission.WRITE_EXTERNAL_STORAGE

Group:android.permission-group.MICROPHONE

Permission:android.permission.RECORD_AUDIO

Group:android.permission-group.SMS

Permission:android.permission.READ_SMS

Permission:android.permission.RECEIVE_WAP_PUSH

Permission:android.permission.RECEIVE_MMS

Permission:android.permission.RECEIVE_SMS

Permission:android.permission.SEND_SMS

Permission:android.permission.READ_CELL_BROADCASTS

Core code of application permission

/ * *

* dynamic application permission is required for android 6.0or above

, /

Private void initPermission () {

/ / define permission groups for dynamic applications

String permissions [] = {Manifest.permission.RECORD_AUDIO

Manifest.permission.ACCESS_NETWORK_STATE

Manifest.permission.INTERNET

Manifest.permission.READ_PHONE_STATE}

ArrayList toApplyList = new ArrayList ()

/ / traverse whether each permission in the permission group to be applied for has been obtained

/ / if no permission is obtained, add it to the toApplyList list for dynamic application

For (String perm: permissions) {

If (PackageManager.PERMISSION_GRANTED! = ContextCompat.checkSelfPermission (this, perm)) {

ToApplyList.add (perm)

/ / entering here means you don't have permission.

}

}

/ / determine whether there is a need for dynamic application permission to use requestPermissions to apply

If (toApplyList.size () > 0) {

String tmpList [] = new String [toApplyList.size ()]

If (! toApplyList.isEmpty ()) {

ActivityCompat.requestPermissions (this, toApplyList.toArray (tmpList), 123)

}

} else {

/ / if you don't need the permission to apply dynamically, you can directly enter the event we want to do.

InitVoice ()

}

}

At the same time, you should rewrite the onRequestPermissionsResult method in activity for the action of obtaining permissions.

@ Override

Public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @ NonNull int [] grantResults) {

Super.onRequestPermissionsResult (requestCode, permissions, grantResults)

/ / this is the callback of dynamic authorization above android 6.0, which is implemented by users.

For (String perm: permissions) {

/ / get the return result of the permission applied for. If you do not get the permission, close the current activity directly.

If (PackageManager.PERMISSION_GRANTED! = ContextCompat.checkSelfPermission (this, perm)) {

Finish ()

}

}

/ / execute the events we need to do after applying for permission

InitVoice ()

}

Add the call to permission request in the onCreate event of activity

Try {

Final PackageInfo info = this.getPackageManager () .getPackageInfo (this.getPackageName (), 0)

Int targetSdkVersion = info.applicationInfo.targetSdkVersion

If (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {

If (targetSdkVersion > = Build.VERSION_CODES.M) {

InitPermission ()

}

} else {

InitVoice ()

}

} catch (PackageManager.NameNotFoundException e) {

E.printStackTrace ()

}

This is the answer to the question about how to apply for permission dynamically in Android 6.0. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report