我正在为学校做一个项目,我需要用户的全名,我将在我的应用程序的侧边栏中显示。我试过以下:
Cursor c = this.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
int count = c.getCount();
String[] columnNames = c.getColumnNames();
boolean b = c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
for (int j = 0; j < columnNames.length; j++) {
String columnName = columnNames[j];
//nameView.setText(columnName);
}
}
c.close();
从堆栈溢出中发现的多个论坛但是每当我打开名为shape的活动时,应用程序就会崩溃。
有一个名为get account或类似内容的权限,但我不知道如何使用它,我也找不到它。
答案 0 :(得分:0)
GET_ACCOUNTS
已移至Android 6.0中的CONTACTS
权限组。所以你需要申请联系人。要申请任何权限,您可以使用以下代码
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.READ_CONTACTS}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
执行此操作后,对于API&gt; = 23的设备,您将在运行时弹出,然后在用户接受权限或拒绝权限后,将调用onRequestPermissionsResult
方法。所以在这里你必须处理你的检查是否用户授予了应用程序的权限。如果是,您可以继续使用您的逻辑
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0:
boolean isPerpermissionForAllGranted = false;
if (grantResults.length > 0 && permissions.length==grantResults.length) {
for (int i = 0; i < permissions.length; i++){
if (grantResults[i] == PackageManager.PERMISSION_GRANTED){
isPerpermissionForAllGranted=true;
}else{
isPerpermissionForAllGranted=false;
}
}
Log.e("value", "Permission Granted, Now get user account");
} else {
isPerpermissionForAllGranted=true;
Log.e("value", "Permission Denied");
}
if(isPerpermissionForAllGranted){
// do your stuff here
}
break;
}
}
要获取用户详细信息,您还可以执行以下操作 -
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personGivenName = acct.getGivenName();
String personFamilyName = acct.getFamilyName();