Android常用工具方法(关闭软键盘,安装apk,检测程序是否安装...)
2015-08-13 08:50
483 查看
Android如何关闭系统软键盘:
Android检测程序是否安装:
Android安装apk文件:
strings.xml中s%的用法:
str
/** * 关闭系统的软键盘 * @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); } }
Android检测程序是否安装:
/** * 检测某程序是否安装 */ 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; }
Android安装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(); } }Android中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); } <pre name="code" class="java">在strings.xml中添加字符串 string name="text">你好,%s!</string> 代码中使用 textView.setText(String.format(getResources().getString(R.string.text),"Android")); 输出结果:Hello,Android!
strings.xml中s%的用法:
在strings.xml中添加字符串 string name="text">你好,%s!</string> 代码中使用 textView.setText(String.format(getResources().getString(R.string.text),"Android")); 输出结果:你好,Android!
str
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories