Android学习笔记--常用工具类
来源:互联网 发布:淘宝手持身份证照片 编辑:程序博客网 时间:2024/05/17 02:47
常用工具类
自定义Application继承系统Application
package rc.loveq.p2ptest.common;import android.app.Activity;import android.app.Application;import android.content.Context;import android.os.Handler;import android.os.Process;import java.util.LinkedList;import java.util.List;import java.util.ListIterator;/** * Author:Rc * Csdn:http://blog.csdn.net/loveqrc * 0n 2017/1/7 22:05 * Email:664215432@qq.com */public class MyApplication extends Application { public static Context sApplicationContext; public static Handler sHandler; public static Thread mThread; public static int mMainThreadId; private List<Activity> activitys = new LinkedList<Activity>(); @Override public void onCreate() { sApplicationContext = getApplicationContext(); sHandler = new Handler(); mThread = Thread.currentThread(); mMainThreadId = Process.myTid(); } public void addActivity(Activity activity) { activitys.add(activity); } public void removeActivity(Activity activity) { activitys.remove(activity); } public void closeApplication() { closeActivitys(); } private void closeActivitys() { ListIterator<Activity> iterator = activitys.listIterator(); while (iterator.hasNext()) { Activity activity = iterator.next(); if (activity != null) { activity.finish(); } } }}
UIUtil
public class UIUtil { public static Context getContext() { return MyApplication.sApplicationContext; } public static Handler getHandler() { return MyApplication.sHandler; } public static int getColor(int colorId) { return getContext().getResources().getColor(colorId); } public static View getXmlView(int layoutId) { return View.inflate(getContext(), layoutId, null); } public static String[] getStringArray(int stringArr) { return getContext().getResources().getStringArray(stringArr); } public static int dp2px(int dp) { float density = getContext().getResources().getDisplayMetrics().density; return (int) (dp * density + 0.5); } public static int px2dp(int px) { float density = getContext().getResources().getDisplayMetrics().density; return (int) (px / density + 0.5); } private static Toast toast; /** * 避免多次显示 * @param content */ public static void showToast(String content) { if (toast == null) { toast = Toast.makeText(getContext(), content, Toast.LENGTH_SHORT); } else { toast.setText(content); } toast.show(); } public static boolean isMainThread(){ int tid = android.os.Process.myTid(); return tid== MyApplication.mMainThreadId; } public static boolean isMainThread(){ int tid = Process.myTid(); return tid==MyApplication.mMainThreadId; }}
MD5
public class MD5Utils { public static String MD5(String sourceStr) { String result = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(sourceStr.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } result = buf.toString(); } catch (NoSuchAlgorithmException e) { System.out.println(e); } return result; }}
SharedPreferences
import android.content.Context;import android.content.SharedPreferences;/** * SharePreference封装 * * */public class PrefUtils { public static boolean getBoolean(Context ctx, String key, boolean defValue) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); return sp.getBoolean(key, defValue); } public static void setBoolean(Context ctx, String key, boolean value) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); sp.edit().putBoolean(key, value).commit(); } public static void setString(Context ctx, String key, String value) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); sp.edit().putString(key, value).commit(); } public static String getString(Context ctx, String key, String defValue) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); return sp.getString(key, defValue); } public static void setInt(Context ctx, String key, int value) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); sp.edit().putInt(key, value).commit(); } public static int getInt(Context ctx, String key, int defValue) { SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE); return sp.getInt(key, defValue); }}
0 0
- Android学习笔记--常用工具类
- 常用工具类学习笔记
- [学习笔记]Java常用工具类
- [学习笔记]Java常用工具类
- Android笔记:日期常用工具类(DateUtils)
- 脱壳学习笔记一:常用工具
- BackTrack5 学习笔记2 常用工具
- javascript 学习笔记 基础篇(1) 语法&常用工具类
- Android 常用工具类封装
- Android 常用工具类封装
- Android中常用工具类
- Android常用工具类2
- android常用工具类 andutils
- Android常用工具类
- Android常用工具类
- android常用工具类
- Android常用工具类
- Android 常用工具类
- krpano元素之textstyle标签
- krpano元素之view标签中文说明
- 在OkHttp的使用过程中出现下列问题Picasso detected an unsupported OkHttp on the classpath.
- task_struct结构体
- 2016年Java面试题整理
- Android学习笔记--常用工具类
- Python断点调试
- 欢迎使用CSDN-markdown编辑器
- HTML5画布Canvas线段、矩形、弧形及贝塞尔曲线等简单图形绘制
- Qt c++与QML混合编程
- C++ 数据类型所占的长度和位数
- LeetCode #403 Frog Jump
- a标签的一些用法
- Lattice系列FPGA入门相关6(理解SerDes之1)