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


1.获取手机分辨率

  1. /**
  2. * 获取手机分辨率
  3. */
  4. public static String getDisplayMetrix(Context context)
  5. {
  6. if (Constant.Screen.SCREEN_WIDTH == 0 || Constant.Screen.SCREEN_HEIGHT == 0)
  7. {
  8. if (context != null)
  9. {
  10. int width = 0;
  11. int height = 0;
  12. SharedPreferences DiaplayMetrixInfo = context.getSharedPreferences("display_metrix_info", 0);
  13. if (context instanceof Activity)
  14. {
  15. WindowManager windowManager = ((Activity)context).getWindowManager();
  16. Display display = windowManager.getDefaultDisplay();
  17. DisplayMetrics dm = new DisplayMetrics();
  18. display.getMetrics(dm);
  19. width = dm.widthPixels;
  20. height = dm.heightPixels;
  21. Editor editor = DiaplayMetrixInfo.edit();
  22. editor.putInt("width", width);
  23. editor.putInt("height", height);
  24. editor.commit();
  25. }
  26. else
  27. {
  28. width = DiaplayMetrixInfo.getInt("width", 0);
  29. height = DiaplayMetrixInfo.getInt("height", 0);
  30. }
  31. Constant.Screen.SCREEN_WIDTH = width;
  32. Constant.Screen.SCREEN_HEIGHT = height;
  33. }
  34. }
  35. return Constant.Screen.SCREEN_WIDTH + "×" + Constant.Screen.SCREEN_HEIGHT;
  36. }

2.关闭系统的软键盘

  1. public class SoftKeyboardUtil {
  2. /**
  3. * 关闭系统的软键盘
  4. * @param activity
  5. */
  6. public static void dismissSoftKeyboard(Activity activity)
  7. {
  8. View view = activity.getWindow().peekDecorView();
  9. if (view != null)
  10. {
  11. InputMethodManager inputmanger = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  12. inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
  13. }
  14. }
  15. }

3.检測某程序是否安装

  1. /**
  2. * 检測某程序是否安装
  3. */
  4. public static boolean isInstalledApp(Context context, String packageName)
  5. {
  6. Boolean flag = false;
  7. try
  8. {
  9. PackageManager pm = context.getPackageManager();
  10. List<PackageInfo> pkgs = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
  11. for (PackageInfo pkg : pkgs)
  12. {
  13. // 当找到了名字和该包名同样的时候,返回
  14. if ((pkg.packageName).equals(packageName))
  15. {
  16. return flag = true;
  17. }
  18. }
  19. }
  20. catch (Exception e)
  21. {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. return flag;
  26. }

4.安装APK文件

  1. /**
  2. * 安装.apk文件
  3. *
  4. * @param context
  5. */
  6. public void install(Context context, String fileName)
  7. {
  8. if (TextUtils.isEmpty(fileName) || context == null)
  9. {
  10. return;
  11. }
  12. try
  13. {
  14. Intent intent = new Intent(Intent.ACTION_VIEW);
  15. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  16. intent.setAction(android.content.Intent.ACTION_VIEW);
  17. intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
  18. context.startActivity(intent);
  19. }
  20. catch (Exception e)
  21. {
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. * 安装.apk文件
  27. *
  28. * @param context
  29. */
  30. public void install(Context context, File file)
  31. {
  32. try
  33. {
  34. Intent intent = new Intent(Intent.ACTION_VIEW);
  35. intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  36. context.startActivity(intent);
  37. }
  38. catch (Exception e)
  39. {
  40. e.printStackTrace();
  41. }
  42. }

5.dp—px相互转换

  1. /**
  2. * 依据手机的分辨率从 dp 的单位 转成为 px(像素)
  3. *
  4. * @return 返回像素值
  5. */
  6. public static int dp2px(Context context, float dpValue) {
  7. final float scale = context.getResources().getDisplayMetrics().density;
  8. return (int) (dpValue * scale + 0.5f);
  9. }
  10. /**
  11. * 依据手机的分辨率从 px(像素) 的单位 转成为 dp
  12. *
  13. * @return 返回dp值
  14. */
  15. public static int px2dp(Context context, float pxValue) {
  16. final float scale = context.getResources().getDisplayMetrics().density;
  17. return (int) (pxValue / scale + 0.5f);
  18. }

6. Strings.xml中“%s”的使用方式

在strings.xml中加入字符串

  1. string name="text">Hello,%s!</string>

代码中使用

  1. textView.setText(String.format(getResources().getString(R.string.text),"Android"));

输出结果:Hello,Android!


7. 依据mac地址+deviceid获取设备唯一编码

  1. private static String DEVICEKEY = "";
  2. /**
  3. * 依据mac地址+deviceid
  4. * 获取设备唯一编码
  5. * @return
  6. */
  7. public static String getDeviceKey()
  8. {
  9. if ("".equals(DEVICEKEY))
  10. {
  11. String macAddress = "";
  12. WifiManager wifiMgr = (WifiManager)MainApplication.getIns().getSystemService(MainApplication.WIFI_SERVICE);
  13. WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
  14. if (null != info)
  15. {
  16. macAddress = info.getMacAddress();
  17. }
  18. TelephonyManager telephonyManager =
  19. (TelephonyManager)MainApplication.getIns().getSystemService(MainApplication.TELEPHONY_SERVICE);
  20. String deviceId = telephonyManager.getDeviceId();
  21. DEVICEKEY = MD5Util.toMD5("android" + Constant.APPKEY + Constant.APPPWD + macAddress + deviceId);
  22. }
  23. return DEVICEKEY;
  24. }

8. 获取手机及SIM卡相关信息

  1. /**
  2. * 获取手机及SIM卡相关信息
  3. * @param context
  4. * @return
  5. */
  6. public static Map<String, String> getPhoneInfo(Context context) {
  7. Map<String, String> map = new HashMap<String, String>();
  8. TelephonyManager tm = (TelephonyManager) context
  9. .getSystemService(Context.TELEPHONY_SERVICE);
  10. String imei = tm.getDeviceId();
  11. String imsi = tm.getSubscriberId();
  12. String phoneMode = android.os.Build.MODEL;
  13. String phoneSDk = android.os.Build.VERSION.RELEASE;
  14. map.put("imei", imei);
  15. map.put("imsi", imsi);
  16. map.put("phoneMode", phoneMode+"##"+phoneSDk);
  17. map.put("model", phoneMode);
  18. map.put("sdk", phoneSDk);
  19. return map;
  20. }

9.按两次返回键后退出应用

  1. @Override
  2. public boolean onKeyDown(int keyCode, KeyEvent event)
  3. {
  4. if (keyCode == KeyEvent.KEYCODE_MENU)
  5. {
  6. return false;
  7. }
  8. // 按两次返回键后退出应用
  9. if (AppTools.getFirstData(IndexActivity.this))
  10. {
  11. if (keyCode == KeyEvent.KEYCODE_BACK)
  12. {
  13. if (System.currentTimeMillis() - touchTime > 1500)
  14. {
  15. Toast.makeText(IndexActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
  16. touchTime = System.currentTimeMillis();
  17. }
  18. else
  19. {
  20. ScreenManager.getScreenManager().popAllActivityExceptMain(IndexActivity.class);
  21. finish();
  22. }
  23. }
  24. return true;
  25. }
  26. else
  27. {
  28. return super.onKeyDown(keyCode, event);
  29. }
  30. }

【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. SQL--面试题

    表A字段如下 month  name income 月份   人员 收入 1      a    1000 2      a    2000 3      a    3000要求用一个SQL语句(注意 ...

  2. 正则表达式、re、常用模块

    阅读目录 正则表达式 字符 量词 . ^ $ * + ? { } 字符集[][^] 分组 ()与 或 |[^] 转义符 \ 贪婪匹配 re 总结 正则 re 常用模块 namedtuple deque ...

  3. 在lua中正确使用uuid的方法:

    -- 参考:http://ju.outofmemory.cn/entry/97724local function guid()        local template ="xxxxxxx ...

  4. MYECLIPSE中快速解决项目的错误的方法

    Use the IDE's help as follows:- Right mouse click on the error in the 'Problems' view- Select the 'Q ...

  5. 【状压基础题】poj3254 Corn Fields

    题目大意 :农夫约翰有n*m块地,其中一些地荒掉了.玉米是一种傲娇的植物,种在相邻的地里会导致不孕不育.求所有种法数对100000000求余. 读入:第一行一个n一个m, 接下来是一个n行m列的矩形, ...

  6. CodeForces 348C Subset Sums(分块)(nsqrtn)

    C. Subset Sums time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  7. 4、Flask实战第4天:自定义url转换器

    url传参可以限定参数的数据类型,例如:限定user_id数据类型为int @app.route('/user/<int:user_id>') def my_list(user_id): ...

  8. centos7下扩充swap空间

    命令:swapon -s #查看系统的swap配置命令 创建步骤: 1. 决定SWAP文件的大小,先指定区块大小:bs,再指定区块数量count,则SWAP文件的大小是:count*bs 在root用 ...

  9. HashSet如何排序

    方法一: 把HashSet保存在ArrayList里,再用Collections.sort()方法比較 private void doSort(){ final HashSet<Integer& ...

  10. [BZOJ2226]LCMSum

    转化一下,$\sum\limits_{i=1}^n[i,n]=n\sum\limits_{i=1}^n\dfrac i{(i,n)}$ 枚举$d=(i,n)$,上式变为$n\sum\limits_{d ...