1、获取应用程序下所有Activity
1 2 3 4 5 6 7 8 9 |
public static ArrayList<String> getActivities(Context ctx) {
ArrayList<String> result = new ArrayList<String>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setPackage(ctx.getPackageName());
for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) {
result.add(info.activityInfo.name);
}
return result;
}
|
2、检测字符串中是否包含汉字
1 2 3 4 5 6 7 8 |
public static boolean checkChinese(String sequence) {
final String format = "[\u4E00-\u9FA5\uF900-\uFA2D]";
boolean result = false;
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
result = matcher.find();
return result;
}
|
3、检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
1 2 3 4 5 6 |
public static boolean checkNickname(String sequence) {
final String format = "[^\u4E00-\u9FA5\uF900-\uFA2D\w-_]";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
return !matcher.find();
}
|
4、检查有没有应用程序来接受处理你发出的intent
1 2 3 4 5 6 |
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager =context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
|
5、使用TransitionDrawable实现渐变效果(比使用AlphaAnimation效果要好,可避免出现闪烁问题)
1 2 3 4 5 6 7 8 |
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
// Use TransitionDrawable to fade in.
final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mContext.getResources(), bitmap) });
//noinspection deprecation
imageView.setBackgroundDrawable(imageView.getDrawable());
imageView.setImageDrawable(td);
td.startTransition(200);
}
|
6、Android中dp、sp和px的相互转换
(在此之前px和sp相互转换一直都用density,后来才发现是错的,有些手机上density和scaledDensity的值一样所以没发现有什么区别,但是在大分辨率的手机上两个值不一样,导致转换出来的字体尺寸有问题)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param pxValue
* @param scale(DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(float pxValue, float scale) {
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param dipValue
* @param scale(DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(float dipValue, float scale) {
return (int) (dipValue * scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @param fontScale(DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(float pxValue, float fontScale) {
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param fontScale(DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(float spValue, float fontScale) {
return (int) (spValue * fontScale + 0.5f);
}
}
|
7、精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕)
1 2 3 4 5 6 |
public static double getScreenPhysicalSize(Activity ctx) {
DisplayMetrics dm = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
return diagonalPixels / (160 * dm.density);
}
|
8、判断是否是平板(官方用法)
1 2 3 |
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
|
9、启动APK的默认Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static void startApkActivity(final Context ctx, String packageName) {
PackageManager pm = ctx.getPackageManager();
PackageInfo pi;
try {
pi = pm.getPackageInfo(packageName, 0);
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
String className = ri.activityInfo.name;
intent.setComponent(new ComponentName(packageName, className));
ctx.startActivity(intent);
}
} catch (NameNotFoundException e) {
Log.e("startActivity", e);
}
}
|
10、计算字宽
1 2 3 4 5 |
public static float GetTextWidth(String text, float Size) {
TextPaint FontPaint = new TextPaint();
FontPaint.setTextSize(Size);
return FontPaint.measureText(text);
}
|
(注意如果设置了textStyle,还需要进一步设置TextPaint。)
11、半角、全角字符转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/**
* 半角转全角
*
* @param input
* String.
* @return 全角字符串.
*/
public static String ToSBC(String input) {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == ' ') {
c[i] = 'u3000'; // 采用十六进制,相当于十进制的12288
} else if (c[i] < '177') { // 采用八进制,相当于十进制的127
c[i] = (char) (c[i] + 65248);
}
}
return new String(c);
}
/**
* 全角转半角
*
* @param input
* String.
* @return 半角字符串
*/
public static String ToDBC(String input) {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 'u3000') {
c[i] = ' ';
} else if (c[i] > 'uFF00' && c[i] < 'uFF5F') {
c[i] = (char) (c[i] - 65248);
}
}
String returnString = new String(c);
return returnString;
}
|
12、查看应用最高可用内存
1 2 |
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
Log.d("TAG", "Max memory is " + maxMemory + "KB");
|
转自:http://my.eoe.cn/sisuer/archive/5917.html