实用代码块记录2
来源:互联网 发布:mac 删除桌面文件夹 编辑:程序博客网 时间:2024/05/21 20:21
1.dip转px
public static float dipToPx(Context context, float dpValue) { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics()); }
2.px 转dp
public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); }
3.sp转px
public static float spToPx(Context context, float spValue) { return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics()); }
4.px转sp
public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); }
5.判断sd卡是否处于可以读写状态
public static boolean isSdcardReadable() { final String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; }
6.判断sd卡是否处于可以写入状态
public static boolean isSdcardWritable() { final String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; }
7.Android 安装应用
public static void installApk(Context context, File file) { if (file.exists()) { Intent i = new Intent(Intent.ACTION_VIEW); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); ((ContextWrapper) context).startActivity(i); } else { ToastUtils.showToast(context, "安装文件不存在,无法安装"); } }
8.卸载应用
public static void uninstallApk(Context context, String pkgName) { Uri packageURI = Uri.parse("package:" + pkgName); Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(uninstallIntent); }
9.拨打电话
/** * 拨打电话 <br> * 需要声明权限 <uses-permission * android:name="android.permission.CALL_PHONE" /> */ public static void callMobilePhone(Activity context, String phone) { Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phone)); context.startActivity(intent); }
0 0
- 实用代码块记录2
- 实用代码块记录1
- 实用代码块记录3
- 实用代码块记录4
- 实用代码块记录5
- 实用代码块记录6
- 实用代码块记录7
- 实用代码块记录8
- 实用代码块记录9
- 实用代码块记录10
- android实用代码块
- Shell 实用代码块
- 实用代码块
- 块代码实用入门指南
- 数组实用代码块简记
- android超实用代码块
- [代码块]记录
- 实用代码片段---持续记录
- php rabbitmq操作类及生产者和消费者实例代码
- Codeforces Round #336 (Div. 2) C 二分+dp
- extern "C"
- 5分钟学会Tablayout!
- 前端组件化思维
- 实用代码块记录2
- SQL查询 生成XML
- 257. Binary Tree Paths
- Android Fragment 生命周期图
- 高级并发对象(High Level Concurrency Objects)
- 常见的排序方法
- H.264之纯码流封装成各种格式的文件
- box-shadow实现外发光效果
- 车展