为方便查找,已进行大致归类,其大纲如下所示:
- 尺寸相关
- dp与px转换
- sp与px转换
- 各种单位转换
- 在onCreate()即可获取View的宽高
- ListView中提前测量View尺寸
- 手机相关
- 判断设备是否是手机
- 获取当前设备的IMIE,需与上面的isPhone一起使用
- 获取手机状态信息
- 是否有SD卡
- 获取MAC地址
- 获取手机厂商,如Xiaomi
- 获取手机型号,如MI2SC
- 跳转至拨号界面
- 拨打电话
- 发送短信
- 获取手机联系人
- 直接打开手机联系人界面,并获取联系人号码
- 获取手机短信并保存到xml中
- 网络相关
- 打开网络设置界面
- 判断是否网络连接
- 判断wifi是否连接状态
- 获取移动网络运营商名称,如中国联通、中国移动、中国电信
- 返回移动终端类型
- 判断手机连接的网络类型(2G,3G,4G)
- 判断当前手机的网络类型(WIFI还是2,3,4G)
- App相关
- 安装指定路径下的Apk
- 卸载指定包名的App
- 获取App名称
- 获取当前App版本号
- 打开指定包名的App
- 打开指定包名的App应用信息界面
- 分享Apk信息
- 获取App信息的一个封装类(包名、版本号、应用信息、图标、名称等)
- 判断当前App处于前台还是后台
- 屏幕相关
- 获取手机分辨率
- 获取状态栏高度
- 获取状态栏高度+标题栏(ActionBar)高度
- 获取屏幕截图
- 设置透明状态栏,需在setContentView之前调用
- 键盘相关
- 避免输入法面板遮挡
- 动态隐藏软键盘
- 点击屏幕空白区域隐藏软键盘
- 动态显示软键盘
- 切换键盘显示与否状态
- 正则相关
- 正则工具类
- 加解密相关
- MD5加密
- SHA加密
- 未归类
- 获取服务是否开启
- 更新Log
大部分代码已验证过可行,如有错误,请及时告之。
分类已上传至Github,传送门→期待你的Star和完善
尺寸相关
dp与px转换
/**
* dp转px
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px转dp
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
sp与px转换
/**
* sp转px
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* px转sp
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
各种单位转换
// 该方法存在于TypedValue
/**
* 各种单位转换
*/
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
switch (unit) {
case TypedValue.COMPLEX_UNIT_PX:
return value;
case TypedValue.COMPLEX_UNIT_DIP:
return value * metrics.density;
case TypedValue.COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case TypedValue.COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f / 72);
case TypedValue.COMPLEX_UNIT_IN:
return value * metrics.xdpi;
case TypedValue.COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f / 25.4f);
}
return 0;
}
在onCreate()即可获取View的宽高
/**
* 在onCreate()即可获取View的宽高
*/
public static int[] getViewMeasure(View view) {
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthMeasureSpec, heightMeasureSpec);
return new int[]{view.getMeasuredWidth(), view.getMeasuredHeight()};
}
ListView中提前测量View尺寸
// 通知父布局,占用的宽,高;
/**
* ListView中提前测量View尺寸,如headerView
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight,
MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
}
手机相关
判断设备是否是手机
/**
* 判断设备是否是手机
*/
public static boolean isPhone(Context context) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephony.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
}
获取当前设备的IMIE,需与上面的isPhone一起使用
/**
* 获取当前设备的IMIE,需与上面的isPhone一起使用
*/
public static String getDeviceIMEI(Context context) {
String deviceId;
if (isPhone(context)) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
deviceId = telephony.getDeviceId();
} else {
deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
return deviceId;
}
获取手机状态信息
// 需添加权限<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
/**
* 获取手机状态信息
*
* 返回如下
* DeviceId(IMEI) = 99000311726612
* DeviceSoftwareVersion = 00
* Line1Number =
* NetworkCountryIso = cn
* NetworkOperator = 46003
* NetworkOperatorName = 中国电信
* NetworkType = 6
* honeType = 2
* SimCountryIso = cn
* SimOperator = 46003
* SimOperatorName = 中国电信
* SimSerialNumber = 89860315045710604022
* SimState = 5
* SubscriberId(IMSI) = 460030419724900
* VoiceMailNumber = *86
*/
public static String getPhoneStatus(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String str = "";
str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
str += "Line1Number = " + tm.getLine1Number() + "\n";
str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
str += "NetworkType = " + tm.getNetworkType() + "\n";
str += "honeType = " + tm.getPhoneType() + "\n";
str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
str += "SimOperator = " + tm.getSimOperator() + "\n";
str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
str += "SimState = " + tm.getSimState() + "\n";
str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
return str;
}
是否有SD卡
/**
* 是否有SD卡
*/
public static boolean haveSDCard() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
获取MAC地址
// 需添加权限<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
/**
* 获取MAC地址
*/
public static</