【Android 工具类】经常使用工具类(方法)大全
收集经常使用的工具类或者方法:
1.获取手机分辨率
/**
* 获取手机分辨率
*/
public static String getDisplayMetrix(Context context)
{
if (Constant.Screen.SCREEN_WIDTH == 0 || Constant.Screen.SCREEN_HEIGHT == 0)
{
if (context != null)
{
int width = 0;
int height = 0;
SharedPreferences DiaplayMetrixInfo = context.getSharedPreferences("display_metrix_info", 0);
if (context instanceof Activity)
{
WindowManager windowManager = ((Activity)context).getWindowManager();
Display display = windowManager.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
Editor editor = DiaplayMetrixInfo.edit();
editor.putInt("width", width);
editor.putInt("height", height);
editor.commit();
}
else
{
width = DiaplayMetrixInfo.getInt("width", 0);
height = DiaplayMetrixInfo.getInt("height", 0);
}
Constant.Screen.SCREEN_WIDTH = width;
Constant.Screen.SCREEN_HEIGHT = height;
}
}
return Constant.Screen.SCREEN_WIDTH + "×" + Constant.Screen.SCREEN_HEIGHT;
}
2.关闭系统的软键盘
public class SoftKeyboardUtil {
/**
* 关闭系统的软键盘
* @param activity
*/
public static void dismissSoftKeyboard(Activity activity)
{
View view = activity.getWindow().peekDecorView();
if (view != null)
{
InputMethodManager inputmanger = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
3.检測某程序是否安装
/**
* 检測某程序是否安装
*/
public static boolean isInstalledApp(Context context, String packageName)
{
Boolean flag = false;
try
{
PackageManager pm = context.getPackageManager();
List<PackageInfo> pkgs = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo pkg : pkgs)
{
// 当找到了名字和该包名同样的时候,返回
if ((pkg.packageName).equals(packageName))
{
return flag = true;
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
4.安装APK文件
/**
* 安装.apk文件
*
* @param context
*/
public void install(Context context, String fileName)
{
if (TextUtils.isEmpty(fileName) || context == null)
{
return;
}
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
context.startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 安装.apk文件
*
* @param context
*/
public void install(Context context, File file)
{
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
}
5.dp—px相互转换
/**
* 依据手机的分辨率从 dp 的单位 转成为 px(像素)
*
* @return 返回像素值
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 依据手机的分辨率从 px(像素) 的单位 转成为 dp
*
* @return 返回dp值
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
6. Strings.xml中“%s”的使用方式
在strings.xml中加入字符串
string name="text">Hello,%s!</string>
代码中使用
textView.setText(String.format(getResources().getString(R.string.text),"Android"));
输出结果:Hello,Android!
7. 依据mac地址+deviceid获取设备唯一编码
private static String DEVICEKEY = "";
/**
* 依据mac地址+deviceid
* 获取设备唯一编码
* @return
*/
public static String getDeviceKey()
{
if ("".equals(DEVICEKEY))
{
String macAddress = "";
WifiManager wifiMgr = (WifiManager)MainApplication.getIns().getSystemService(MainApplication.WIFI_SERVICE);
WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
if (null != info)
{
macAddress = info.getMacAddress();
}
TelephonyManager telephonyManager =
(TelephonyManager)MainApplication.getIns().getSystemService(MainApplication.TELEPHONY_SERVICE);
String deviceId = telephonyManager.getDeviceId();
DEVICEKEY = MD5Util.toMD5("android" + Constant.APPKEY + Constant.APPPWD + macAddress + deviceId);
}
return DEVICEKEY;
}
8. 获取手机及SIM卡相关信息
/**
* 获取手机及SIM卡相关信息
* @param context
* @return
*/
public static Map<String, String> getPhoneInfo(Context context) {
Map<String, String> map = new HashMap<String, String>();
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
String imsi = tm.getSubscriberId();
String phoneMode = android.os.Build.MODEL;
String phoneSDk = android.os.Build.VERSION.RELEASE;
map.put("imei", imei);
map.put("imsi", imsi);
map.put("phoneMode", phoneMode+"##"+phoneSDk);
map.put("model", phoneMode);
map.put("sdk", phoneSDk);
return map;
}
9.按两次返回键后退出应用
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_MENU)
{
return false;
}
// 按两次返回键后退出应用
if (AppTools.getFirstData(IndexActivity.this))
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (System.currentTimeMillis() - touchTime > 1500)
{
Toast.makeText(IndexActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
touchTime = System.currentTimeMillis();
}
else
{
ScreenManager.getScreenManager().popAllActivityExceptMain(IndexActivity.class);
finish();
}
}
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}
}
【Android 工具类】经常使用工具类(方法)大全的更多相关文章
- Android之SharedPreferences两个工具类
相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...
- Android中创建倒影效果的工具类
一.有时候我们需要创建倒影的效果,我们接触最多的都是图片能够创建倒影,而布局依然可以创建倒影. 二.工具类代码 import android.graphi ...
- JQuery操作类数组的工具方法
JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...
- 反射工具类.提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class,被AOP过的真实类等工具函数.java
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.ap ...
- Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...
- 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实
2.使用工厂方法模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...
- Android 开发工具类 10_Toast 统一管理类
Toast 统一管理类: 1.短时间显示Toast: 2.长时间显示 Toast: 3.自定义显示 Toast 时间. import android.content.Context; import a ...
- Android 开发工具类 05_Logcat 统一管理类
Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_07 Collections工具类_3_Collections集合工具类的方法
第二个参数传递了一个匿名内部类.结果就出现了下面的代码 源码里面有Compare方法,对比两个参数 要重写比较的方法 对对象进行排序 创建学生类.对学生类进行排序 重写Person的ToString方 ...
- 【转】Android开发中让你省时省力的方法、类、接口
转载 http://www.toutiao.com/i6362292864885457410/?tt_from=mobile_qq&utm_campaign=client_share& ...
随机推荐
- 【C++】类的特殊成员变量+初始化列表
参考资料: 1.黄邦勇帅 2.http://blog.163.com/sunshine_linting/blog/static/448933232011810101848652/ 3.http://w ...
- linux awk学习笔记
awk学习笔记 awk语法格式 awk '{pattern + action}' {filenames} awk作用 awk的最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取信息后 ...
- C#实现DES加密解密,AES加密解密
DES算法描述简介: DES是Data Encryption Standard(数据加密标准)的缩写.它是由IBM公司研制的一种加密算法,美国国家标准局于1977年公布把它作为非机要部门使用的数据加密 ...
- java.io.WriteAbortedException异常
java.io.WriteAbortedException异常 未实现 public interface Serializable 接口的类将无法使其任何状态序列化或反序列化. 可序列化类的所有子类型 ...
- (15)python 数据库连接
python连接mysql两种方法 一.python官网提供的 MySQL-python 软件 下载地址 https://pypi.python.org/pypi/MySQL-python/1.2.5 ...
- 0103 最短Hamilton路径【状压DP】
0103 最短Hamilton路径 0x00「基本算法」例题 描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Ham ...
- HDU 1711 Number Sequence【kmp求子串首次出现的位置】
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= ...
- ArrayList解析
ArrayList 属性 // 默认长度 private static final int DEFAULT_CAPACITY = 10; // 底层是以数组格式存储 private static fi ...
- Manacher【p1210】回文检测
题目描述--->P1210 回文检测 分析: 看到回文显然想到了manacher算法(线性求解回文串问题 如果不了解还是去敲一下板子,学习一下比较好.-->manacher 题目要求我们求 ...
- 【前缀和】【前缀MAX】洛谷 P1351 NOIP2014提高组 day1 T2 联合权值
不难发现,树中与某个点距离为2的点只可能是它的父亲的父亲.儿子的儿子 或者 兄弟,分类讨论一下即可. 只有对于兄弟我们不能暴力搞,维护一下每个节点的所有儿子的前缀和.前缀MAX就行了. #includ ...