这里填写的权限和普通的权限相差无异。
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 二、权限申请 单权限申请下面来实现一个单权限的申请。
package com.example.test; import android.Manifest; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback { private static String TAG = "MainActivity"; View mLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLayout = findViewById(R.id.activity_main); } @Override public boolean shouldShowRequestPermissionRationale(String permission) { return super.shouldShowRequestPermissionRationale(permission); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { openSettingActivity(this, "没有此权限,无法开启这个功能,请开启权限。PERMISSION_READ_ACCOUNTS 和 PERMISSION_GET_ACCOUNTS" ); } } private static void openSettingActivity(final Activity activity, String message) { showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Log.d(TAG, "getPackageName(): " + activity.getPackageName()); Uri uri = Uri.fromParts("package", activity.getPackageName(), null); intent.setData(uri); activity.startActivity(intent); } }); } private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) { new AlertDialog.Builder(context) .setMessage(message) .setPositiveButton("OK", okListener) .setNegativeButton("Cancel", null) .create() .show(); } public void showContacts(View v) { Log.i(TAG, "Show contacts button pressed. Checking permissions."); //检查是否够权限 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) { Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions."); requestContactsPermissions(); } } private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS}; /** * Id to identify a contacts permission request. */ private static final int REQUEST_CONTACTS = 1; /** * 申请权限 */ private void requestContactsPermissions() { //检查是否用户点击了不再提示选项 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_CONTACTS)) { Log.i(TAG, "Displaying contacts permission rationale to provide additional context."); //提示信息 Snackbar.make(mLayout, "Contacts permissions are needed to demonstrate access.", Snackbar.LENGTH_INDEFINITE) .setAction("ok", new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); } }) .show(); } else { // Contact permissions have not been granted yet. Request them directly. ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); } } }