您的位置:首页 > 移动开发 > Android开发

收集Android常用工具类,方便查阅

2016-09-08 12:15 295 查看
//直接拨打电话,需要权限
public static void gotoCallPhone(Context context,String tel) {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + tel);
intent.setData(data);
context.startActivity(intent);
}
//跳转到拨打电话界面
public static void gotoDialPhone(Context context,String tel) {
Intent intent = new Intent(Intent.ACTION_DIAL);
Uri data = Uri.parse("tel:" + tel);
intent.setData(data);
context.startActivity(intent);
}
//String double类型转换
public static double parseDouble(String dString) {
try {
return Double.parseDouble(dString);
} catch (Exception e) {
return 0f;
}
}
//String float 类型转换
public static float parseFloat(String fString) {
try {
return Float.parseFloat(fString);
} catch (Exception e) {
return 0f;
}
}
//手机IEMI获取
public static String getIEMI() {
TelephonyManager telephonyManager =
(TelephonyManager) getApplication().getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
//dp转px
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
//px转dp
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
//sp转dp
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
//获取屏幕高度    Activity.getWindowManager()
public static int getScreenHight(WindowManager windowManager) {
return windowManager.getDefaultDisplay().getHeight();
}
//获取屏幕宽度    Activity.getWindowManager()
public static int getScreenWidth(WindowManager windowManager) {
return windowManager.getDefaultDisplay().getWidth();
}
//获取系统时间
public static String getSysTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
return formatter.format(curDate);
}
//手动进行GC
public static void GC() {
System.gc();
}
//app下载的图片有时在系统的相册里没有缩列图,此方法可以让其生成缩列图,未验证
public static void updateGallery(Context context) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//MediaStore.Images.Media.EXTERNAL_CONTENT_URI
String path = Environment.getExternalStorageDirectory() + "/DCIM/Camera";
Uri uri = Uri.fromFile(new File(path));
intent.setData(uri);
context.sendBroadcast(intent);
}
//递归删除所有文件
public static void deleteAllFiles(File file) {
if (file.isFile()) {
file.delete();
return;
}
if (file.isDirectory()) {
File[] childFile = file.listFiles();
if (childFile == null || childFile.length == 0) {
file.delete();
return;
}
for (File f : childFile) {
deleteAllFiles(f);
}
file.delete();
}
}
//随机数+时间取文件名
public static String getFileName(String header,String format) {
return header + ((int) (Math.random() * 100000 + 1))
+ String.valueOf(System.currentTimeMillis()) + format;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: