1、
/**
* 获取指定程序下所有Action为Intent.ACTION_MAIN的Activity
*
* @param context
* 上下文对象
* @param pkg
* 包名
* @return 指定程序下所有Action为Intent.ACTION_MAIN的Activity的集合
*/
public static List<String> getActivities(Context context, String pkg) {
List<String> result = new ArrayList<String>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setPackage(pkg);
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, 0);
if (DataUtil.isEmpty(list)) {
return null;
}
for (ResolveInfo info : list) {
result.add(info.activityInfo.name);
}
return result;
}
2、
/**
* 获取指定已安装的APK的安装路径(如:/data/app/com.home.util-2.apk)
*
* @param context
* 上下文对象
* @param pkg
* 包名
* @return 指定APK的安装路径
*/
public static String getAppSourceDir(Context context, String pkg) {
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> list = pm.getInstalledApplications(0);
if (DataUtil.isEmpty(list)) {
return null;
}
for (ApplicationInfo appInfo : list) {
if (appInfo.packageName.equals(pkg)) {
return appInfo.sourceDir;
}
}
return null;
}
3、
/**
* 泛型的集合转为数组
*
* @param cls
* 类模板
* @param list
* 源集合
* @return 转换后的数组
*/
@SuppressWarnings("unchecked")
public static <T> T[] listToArray(Class<?> cls, List<T> list) {
if (isEmpty(list)) {
return (T[]) Array.newInstance(cls, 0);
}
return list.toArray((T[]) Array.newInstance(cls, list.size()));
}
4、
/**
* 检测文本是否全为中文
*
* @param text
* 文本
* @return 全为中文返回true,否则返回false
*/
public static boolean isChinese(String text) {
if (isEmpty(text)) {
return false;
}
return text.matches("[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+");
}
5、
/**
* 检测文本中是否包含汉字
*
* @param text
* 文本
* @return 包含返回true,否则返回false
*/
public static boolean isHasChinese(String text) {
final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";
boolean result = false;
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(text);
result = matcher.find();
return result;
}
6、
/**
* 保存ListView的当前位置
*
* @param context
* 上下文对象
* @param lv
* ListView实例
*
*/
public static void saveListViewCurPos(Context context, ListView lv) {
int pos = lv.getFirstVisiblePosition();
View v = lv.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
SharedUtil.saveInt(context, Constant.DEFAULT_SHARE_NAME, "pos", pos);
SharedUtil.saveInt(context, Constant.DEFAULT_SHARE_NAME, "top", top);
}
/**
* 恢复ListView的之前位置
*
* @param context
* 上下文对象
* @param lv
* ListView实例
* @return 恢复成功返回true,否则返回false
*/
public static boolean resumeListViewPos(Context context, ListView lv) {
int pos = SharedUtil.getInt(context, Constant.DEFAULT_SHARE_NAME, "pos", -1);
int top = SharedUtil.getInt(context, Constant.DEFAULT_SHARE_NAME, "top", -1);
if (pos == -1 || top == -1) {
return false;
}
lv.setSelectionFromTop(pos, top);
return true;
}
7、
/**
* 获取手机ip地址
*
* @param context
* 上下文对象
* @return ip地址
*/
@SuppressWarnings("deprecation")
public static String getPhoneIp(Context context) {
int ip = getWifiManager(context).getConnectionInfo().getIpAddress();
return android.text.format.Formatter.formatIpAddress(ip);
}
8、
/**
* 文件夹排序(先文件夹排序后文件排序)
*
* @param files
* 文件数组
*/
public static void sortFiles(File[] files) {
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
boolean l1 = file1.isDirectory();
boolean l2 = file2.isDirectory();
if (l1 && !l2)
return -1;
else if (!l1 && l2)
return 1;
else {
return file1.getName().compareTo(file2.getName());
}
}
});
}
原文地址:http://blog.csdn.net/u010142437/article/details/41356687