欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 中的危险权限

程序员文章站 2022-06-05 14:22:20
...

以下是Android开发中常会遇见的危险权限,一共是9组24个危险权限。使用以下权限的时候需要适配6.0以上的系统

权限组名 权限名称
CALENDAR READ_CALENDAR
WRITE_CALENDAR
CAMERA CAMERA
CONTACTS READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
LOCATION ACCESS_FIND_LOCATION
ACCESS_COARSE_LOCATION
MICROPHONE RECORD_AUDIO
PHONE READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
SENSORS BODY_SENSORS
SMS SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE

附上Android6.0+权限请求代码

public class MainActivity extends AppCompatActivity {
	private String[] permissions = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
	public void onCreate(Bundle savedInstanceState) {
		super(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn = findViewById(R.id.btn_request_permission);
		btn.setOnClickListener(new OnClickListener() {
			@Ovrride
			public void onClick(View v) {
				initPermission();
			}
		});
	}
	/**
	 * 权限检测
	 */
	private void initPermission() {
		//Activity中申请权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //申请拍照和读取sd卡权限
            if (ContextCompat.checkSelfPermission(this, permissions) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, 10001);
            } else {
                //do something
            }
        } else {
            //do something
        }
	}

	@Override
	public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
		switch(requestCode) {
			case 10001:
				if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
					//do something
				} else {
					Toast.makeText(MainActivity.this, "You denied the permissions", Toast.LENGTH_SHORT).show();
				}
			break;
			default:
			break;
		}
	}
}
相关标签: Android permissions