收集经常使用的工具类或者方法:


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 工具类】经常使用工具类(方法)大全的更多相关文章

  1. Android之SharedPreferences两个工具类

    相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...

  2. Android中创建倒影效果的工具类

                     一.有时候我们需要创建倒影的效果,我们接触最多的都是图片能够创建倒影,而布局依然可以创建倒影.       二.工具类代码 import android.graphi ...

  3. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  4. 反射工具类.提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class,被AOP过的真实类等工具函数.java

    import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.ap ...

  5. Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

  6. 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实

    2.使用工厂方法模式设计能够实现包含加法(+).减法(-).乘法(*).除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果.要求使用相关的工具绘制UML类图并严格按照类图的设计编写程 ...

  7. Android 开发工具类 10_Toast 统一管理类

    Toast 统一管理类: 1.短时间显示Toast: 2.长时间显示 Toast: 3.自定义显示 Toast 时间. import android.content.Context; import a ...

  8. Android 开发工具类 05_Logcat 统一管理类

    Logcat 统一管理类: 1.默 认tag 的函数: 2.自定义 tag 的函数. import android.util.Log; // Logcat 统一管理类 public class L { ...

  9. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_07 Collections工具类_3_Collections集合工具类的方法

    第二个参数传递了一个匿名内部类.结果就出现了下面的代码 源码里面有Compare方法,对比两个参数 要重写比较的方法 对对象进行排序 创建学生类.对学生类进行排序 重写Person的ToString方 ...

  10. 【转】Android开发中让你省时省力的方法、类、接口

    转载 http://www.toutiao.com/i6362292864885457410/?tt_from=mobile_qq&utm_campaign=client_share& ...

随机推荐

  1. C# 通过串口发送短信

    手机短信群发作为企业日常通知,公告,天气预报等信息的一个发布平台,在于成本低,操作方便等诸多特点,成为企业通讯之首选.本文介绍短信的编码方式,AT指令以及用C#实现串口通讯的方法. 前言目前,发送短信 ...

  2. 如何在windows平台下使用hsdis与jitwatch查看JIT后的汇编码

    1. 安装hsids 这一步比较麻烦,需要提前安装cygwin,以及下载openjdk的源码 具体步骤请参考下面的两篇文章 How to build hsdis-amd64.dll and hsdis ...

  3. Java基础:异常机制

    最近开始了找工作的面试,在面试过程中,面试官问了关于Java当中的异常处理机制,一直以来,无论写代码还是看书,自己对异常处理这一块就没有很好的重视过,对它的认知也仅仅停留在通过Try-catch去进行 ...

  4. [php] 解析JSON字符串

    $json_str = '{"thumb":"","photo":[{"url":"557a3d2184db0 ...

  5. PHP abstract与interface之间的区别

    1.php 接口类:interface 其实他们的作用很简单,当有很多人一起开发一个项目时,可能都会去调用别人写的一些类,那你就会问,我怎么知道他的某个功能的实现方法是怎么命名的呢,这个时候php接口 ...

  6. Codeforces Round 536 (Div. 2) (E)

    layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  7. DeprecationWarning: current URL string parser is deprecated解决方法

    我最近在使用mongoDB的时候,发现了这个警告语句,纳闷了,按照官方文档的教程去连接数据库还能出错,也是醉了. 后来尝试去阅读相关资料,发现只是需要将{ useNewUrlParser: true ...

  8. AOJ 2251 Merry Christmas (最小点覆盖)

    [题目链接] http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2251 [题目大意] 给出一张图,现在有一些任务,要求在ti时刻送礼物 ...

  9. 【后缀数组】poj3581 Sequence

    考虑第一次切割,必然切割的是翻转后字典序最小的前缀,伪证: 若切割位置更靠前:则会导致第一个数翻转后更靠前,字典序必然更大. 若切割位置更靠后,则显然也会导致字典序更大. ↑,sa即可 对于第二次切割 ...

  10. python3开发进阶-Django框架的详解

    一.MVC框架和MTV框架 MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分: 模型(Model).视图(View)和控制器(Con ...