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


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);
}
}

最新文章

  1. PHP之使用网络函数和协议函数
  2. [09]APUE:进程关系
  3. python 的编码问题
  4. Python实战:美女图片下载器,海量图片任你下载
  5. Eclipse用法和技巧二十一:工程的展示途径
  6. 温故而知新----stack
  7. 常用sql语句总结(二)(更新数据,序列,创建数据表,约束,注释)
  8. 【还是回来了】博客搬家--https://cangbean.github.io
  9. PXE高效能批量网络装机
  10. MySQL利用mysqldump在线修复或新增从库
  11. 13.1 dubbo服务降级源码解析
  12. 洛谷P4437 排列 [HNOI/AHOI2018] 贪心
  13. div模拟textarea文本域轻松实现高度自适应——张鑫旭
  14. js-权威指南学习笔记16
  15. 记账本APP(2)
  16. 实验吧 recursive write up
  17. 【Error】InterfaceError (0, &#39;&#39;)
  18. java实例三维空间求点之间的距离。。。。
  19. 在IDEA 中用maven创建web项目
  20. bootstrap之PressKeyCode&amp;amp;&amp;amp;LongPressKeyCode

热门文章

  1. CSS中的HSLA颜色
  2. 《Java编程思想》笔记 第一章 对象导论
  3. mysql 如何提高批量导入的速度
  4. ajax按钮改变数据状态
  5. [BZOJ2661][BeiJing wc2012]连连看 费用流
  6. hdu5829
  7. 文件的上传(TCP)
  8. Codeforces Round #424 A(模拟)
  9. POJ 2236 Wireless&#160;Network&#160;[并查集+几何坐标 ]
  10. HDU 1236 排名(结构体+排序)