1.需求
一个EditText 点击button的时候转到系统通讯录,选中其中一个通讯录item返回当前界面且把
选中的联系人号码用户名显示在EditText 上。
2.示例
3.代码
AddressList.java
public class AddressList extends CheckPermissionsActivity {
EditText text;
String username,usernumber;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.address_list);
text = (EditText)findViewById(R.id.number);
}
public void click(View v){
startActivityForResult(new Intent(
Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI), 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
ContentResolver reContentResolverol = getContentResolver();
Uri contactData = data.getData();
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null);
while (phone.moveToNext()) {
usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
text.setText(usernumber+" ("+username+")");
}
}
}
}
address_list.xml
在manifest 文件中已经添加了user-permission
可能出现错误提示:
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{49af1fb 14363:com.example.gszczepanski.eduandroid/u0a59} (pid=14363, uid=10059) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
错误提示的意思是:需要用户自己去添加对contacts 的授权,而不是让系统给授权,这都是android 6.0及以上的设备上才出现的问题,google 主要是处于安全的考虑,才添加的机制;
解决的方法是:弹出授权提示框,提醒用户授权;
故,增加一个CheckPermissionsActivity
代码如下:
/**
* 继承了Activity,实现Android6.0的运行时权限检测
* 需要进行运行时权限检测的Activity可以继承这个类
*
* @项目名称: AMapLocationDemo
* @文件名称:PermissionsChecker.java
* @类型名称:PermissionsChecker
* @since 2.5.0
*/
public class CheckPermissionsActivity extends Activity
implements
ActivityCompat.OnRequestPermissionsResultCallback {
/**
* 需要进行检测的权限数组
*/
protected String[] needPermissions = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.DISABLE_KEYGUARD,
Manifest.permission.RECEIVE_BOOT_COMPLETED,
Manifest.permission.READ_CONTACTS,
};
private static final int PERMISSON_REQUESTCODE = 0;
/**
* 判断是否需要检测,防止不停的弹框
*/
private boolean isNeedCheck = true;
@Override
protected void onResume() {
super.onResume();
if(isNeedCheck){
checkPermissions(needPermissions);
}
}
/**
*
* @param needRequestPermissonList
* @since 2.5.0
*
*/
private void checkPermissions(String... permissions) {
List needRequestPermissonList = findDeniedPermissions(permissions);
if (null != needRequestPermissonList
&& needRequestPermissonList.size() > 0) {
ActivityCompat.requestPermissions(this,
needRequestPermissonList.toArray(
new String[needRequestPermissonList.size()]),
PERMISSON_REQUESTCODE);
}
}
/**
* 获取权限集中需要申请权限的列表
*
* @param permissions
* @return
* @since 2.5.0
*
*/
private List findDeniedPermissions(String[] permissions) {
List needRequestPermissonList = new ArrayList();
for (String perm : permissions) {
if (ContextCompat.checkSelfPermission(this,
perm) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.shouldShowRequestPermissionRationale(
this, perm)) {
needRequestPermissonList.add(perm);
}
}
return needRequestPermissonList;
}
/**
* 检测是否说有的权限都已经授权
* @param grantResults
* @return
* @since 2.5.0
*
*/
private boolean verifyPermissions(int[] grantResults) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] paramArrayOfInt) {
if (requestCode == PERMISSON_REQUESTCODE) {
if (!verifyPermissions(paramArrayOfInt)) {
showMissingPermissionDialog();
isNeedCheck = false;
}
}
}
/**
* 显示提示信息
*
* @since 2.5.0
*
*/
private void showMissingPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.notifyTitle);
builder.setMessage(R.string.notifyMsg);
// 拒绝, 退出应用
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setPositiveButton(R.string.setting,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
/**
* 启动应用的设置
*
* @since 2.5.0
*
*/
private void startAppSettings() {
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
只需在上面的权限数组中添加所需权限即可:
Manifest.permission.READ_CONTACTS,
修改strings.xml
ScreenLocker2
提示
当前应用缺少必要权限。\n\n请点击\"设置\"-\"权限\"-打开所需权限。
设置
取消