在android6.0下,是不是不用去手动的申请权限,就是直接在manifest中注册后,就获得了权限
replaceall在代码的最后一行,应为获取不到num所以会空指针。在魅族中能跳转到通信录界面,但是点击号码是不能返回得到num。显示没有获得权限
我现在用模拟器测试了4.x和6.0的系统,可以正常运行
希望大哥,能帮我调下下面的代码
下面是我的代码:
private TextView numberTxt;
private Button button;
String TAG = "NUMBER: ";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gwtnumber_two);
numberTxt = (TextView) findViewById(R.id.number_tv);
button = (Button) findViewById(R.id.getNumber_Btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PackageManager pm = getPackageManager();
boolean permission = (PackageManager.PERMISSION_GRANTED ==
pm.checkPermission("android.permission.READ_CONTACTS", "packageName"));
if (permission) {
Log.e(TAG, "有");
}else {
Log.e(TAG, "木有这个权限");
}
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.setType("vnd.android.cursor.dir/phone_v2");
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
String num = null;
// 创建内容解析者
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(uri,
null, null, null, null);
while (cursor.moveToNext()) {
num = cursor.getString(cursor.getColumnIndex("data1"));
}
cursor.close();
num = num.replaceAll("-", "");//替换的操作,555-6 -> 5556\
numberTxt.setText(num);
}
}