有没有办法在android中获取当前的用户名和手机号码?
我想在android中获取当前电话号码和用户名的用户信息。
任何人都有代码或想法,请帮助我..谢谢..
有没有办法在android中获取当前的用户名和手机号码?
我想在android中获取当前电话号码和用户名的用户信息。
任何人都有代码或想法,请帮助我..谢谢..
使用此函数获取 Owner 信息
public String [] OwnerInfo() {
final AccountManager manager = AccountManager.get(context);
final Account[] accounts = manager.getAccountsByType("com.google");
try{
if (accounts[0].name != null) {
//get the account_name
ownerinfo[0] = accounts[0].name;
ContentResolver cr = context.getContentResolver();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.DATA + " = ?",
new String[] { ownerinfo[0] }, null);
while (emailCur.moveToNext()) {
//get the id
ownerinfo[1] = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID));
//get the Email
ownerinfo[2] = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String newName = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (ownerinfo[3] == null || newName.length() > ownerinfo[3].length())
//get the Name
ownerinfo[3] = newName;
Log.v("Got contacts", "ID " + ownerinfo[1] + " Email : " + ownerinfo[2]
+ " Name : " + ownerinfo[3]);
}
emailCur.close();
// get the phone number
final TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
ownerinfo[4] = tm.getLine1Number();
if(ownerinfo[4]==null)
{
if (ownerinfo[1] != null) {
// get the phone number
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { ownerinfo[1] }, null);
while (pCur.moveToNext()) {
ownerinfo[4] = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.v("Got contacts", "phone" + ownerinfo[4]);
}
pCur.close();
}
}
}
}catch(ArrayIndexOutOfBoundsException e){
Log.d("OwnerInfo","ArrayIndexOutOfBoundsException due to email account not available in your device");
}
return ownerinfo;
}
您可以使用 TelephonyManager 获取手机号码。
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number_User = tm.getLine1Number();
并在您的清单文件中添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
为此,这将帮助你..
final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
names[i] = accounts[i].name;
}
您可以使用 TelephonyManager 执行此操作:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
另请参阅此答案。
getLine1Number()的文档说,如果数字“不可用”,此方法将返回 null,但没有说明数字何时可能不可用。
您需要在清单文件中添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
对于所有者名称,请参阅此。