
目前国内市场的山寨机横行,安卓手机升级也是一天一个样,对于原来老手机可能没有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
- 转载请注明: leehom 2015年01月22日 于 IT十万为什么 发表