Android(安卓)内部存储安装apk文件实现
16lz
2021-01-26
目前国内市场的山寨机横行,安卓手机升级也是一天一个样,对于原来老手机可能没有SDCARD,导致我们的APP不能下载资源,无法更新APP,针对这种情况有以下解决方案。
通过以下函数判断是否有SD卡再判断下载到哪个目录下。
/** * 安装应用 */public static void update(File apkFile, Context context) {Intent intent = new Intent(Intent.ACTION_VIEW);if (apkFile != null && apkFile.exists()) {chmod(apkFile.getAbsolutePath());//授权intent.setDataAndType(Uri.fromFile(apkFile),"application/vnd.android.package-archive");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent);} else {MLog.makeText("安装失败,安装文件未找到");}}public static void chmod(String pathc) {String chmodCmd = "chmod 666 " + pathc;try {Runtime.getRuntime().exec(chmodCmd);} catch (Exception e) {e.printStackTrace();}}public static boolean isMounted() {return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}/** * 初始文件可以存储的位置 * @param context */public static void init(Context context) {FileUtil.context = context;StringBuffer buffer = new StringBuffer();String pName = context.getPackageName();if (Tools.isMounted()) {buffer.append(Environment.getExternalStorageDirectory().getAbsolutePath());buffer.append(File.separator);buffer.append(pName);fileDir = buffer.toString();} else {buffer.append(context.getFilesDir().getAbsolutePath());buffer.append(File.separator);buffer.append(pName);fileDir = buffer.toString();}File file = new File(fileDir);if (!file.exists())file.mkdir();}
- 本文固定链接:http://www.ithtw.com/910.html
- 转载请注明:leehom2015年01月22日于IT十万为什么发表
更多相关文章
- 【原创】The Error in Android(安卓)developing
- Android(安卓)webview最简单小例子
- Android(安卓)Camera HAL浅析
- 在Android(安卓)Studio中使用Android-PullToRefresh Library
- android 简单试题系统
- Android(安卓)5.0 如何正确启用isLoggable(二)__原理分析
- 彻底解决 INSTALL_FAILED_TEST_ONLY安装失败的问题
- Mac 打 Android(安卓)包遇到 open too many files 解决办法
- 自定义Android日志输出工具类