得到手机所有应用名和包名
public static Map<String, String> queryAppInfo(Context mContext) {
Map<String, String> mapName = new HashMap<String, String>();
PackageManager pm = mContext.getPackageManager();
List<PackageInfo> list2 = pm
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo packageInfo : list2) {
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String appName = packageInfo.applicationInfo.loadLabel(
mContext.getPackageManager()).toString();
String packageName = packageInfo.packageName;
mapName.put(appName, packageName);
}
}
return mapName;
}
得到所有电话号码(名字和号码的map)
public static Map<String, String> getAllPhone(Context mContext) {
Map<String, String> mapPhone = new HashMap<String, String>()
// 使用ContentResolver查找联系人数据
Cursor cursor = mContext.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null)
// 遍历查询结果,找到所需号码
String phoneNumber = ""
while (cursor.moveToNext()) {
// 获取联系人ID
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID))
// 获取联系人的名字
String contactName = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
// 使用ContentResolver查找联系人的电话号码
Cursor phone = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null)
if (phone.moveToNext()) {
phoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
mapPhone.put(contactName, phoneNumber)
}
}
return mapPhone
}
通过输入名字获取电话号码
public static String getnumber(Context mContext, String name) {
// 使用ContentResolver查找联系人数据
Cursor cursor = mContext.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null)
// 遍历查询结果,找到所需号码
String phoneNumber = ""
String dsfsd = ""
while (cursor.moveToNext()) {
// 获取联系人ID
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID))
// 获取联系人的名字
String contactName = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
dsfsd += contactName + ","
if (name.equals(contactName)) {
// 使用ContentResolver查找联系人的电话号码
Cursor phone = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null)
if (phone.moveToNext()) {
phoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
break
}
}
}
return phoneNumber
}
判断手机中是否安装指定包名的软件
public static boolean isInstallByread(Context context, String name) {
List<PackageInfo> packages = context.getPackageManager()
.getInstalledPackages(0);
for (int i = 0; i < packages.size(); i++) {
PackageInfo packageInfo = packages.get(i);
if (packageInfo.packageName.equals(name)) {
return true;
} else {
continue;
}
}
return false;
}