Android开发获取用户名代码,android - 有没有办法在Android中获取当前的用户名和手机号码? - 堆栈内存溢出...

使用此功能获取所有者信息

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值