Android极速开发之SD卡缓存目录

  • 读取某个文件夹中的所有Apk文件路径并打开安装页面
  • 读取某文件夹下的所有apk文件
  • 获取SD卡跟目录中的某个文件
  • 弹出安装界面
  • 卸载apk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
*
* @author Javen
*
*/
public class SDKUtil {
private final static String TAG=SDKUtil.class.getSimpleName();
/**
* 读取某个文件夹中的所有Apk文件路径并打开安装页面
* @param context
* @param path
*/
public static void readApkAndStart(Context context,String path){
List<String> listpath = readAllApkForPath(context, path);
if (listpath!=null && listpath.size()>0) {
for (String string : listpath) {
SDKUtil.openInstallView(context, string);
}
}else {
L.e("xxxx-----", "readApkAndStart null。。。。。。。");
}
}
/**
* 读取某文件夹下的所有apk文件
* @param context
* @param path
* @return
*/
public static List<String> readAllApkForPath(Context context,String path){
List<String> fileNameList=new ArrayList<String>();
File dir;
if (Parameter.isDebug) {
dir = getSDir(context, path);
}else {
dir = getDiskCacheDir(context, path);
}
if (dir.isDirectory()) {
File[] files = dir.listFiles();
for (File file : files) {
String filePath = file.getAbsolutePath();
if (filePath.endsWith(".apk")) {
fileNameList.add(filePath);
}
}
return fileNameList;
}
return null;
}
/**
* 获取SD卡跟目录中的某个文件
* @param context
* @param uniqueName
* @return
*/
public static File getSDir(Context context, String uniqueName) {
boolean externalStorageAvailable = Environment
.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
String path = null;
if (externalStorageAvailable) {
path=Environment.getExternalStorageDirectory().getAbsolutePath();
}
return new File(path + File.separator + uniqueName);
}
/**
* 获取缓存地址
* @param context
* @param uniqueName
* @return
*/
public static File getDiskCacheDir(Context context, String uniqueName) {
boolean externalStorageAvailable = Environment
.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
String cachePath;
if (externalStorageAvailable) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
L.i(TAG, cachePath);
return new File(cachePath + File.separator + uniqueName);
}
/**
* 弹出安装界面
* @param context
* @param pathString apk 路径
*/
public static void openInstallView(Context context,String pathString){
//启动安装界面
// Intent install = new Intent(Intent.ACTION_VIEW);
// install.setDataAndType(Uri.fromFile(new File(pathString)),
// "application/vnd.android.package-archive");
// install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(install);
ApkController.install(pathString, context);
}
/**
* 卸载apk
* @param context
* @param packageName
*/
public static void uninstallApk(Context context, String packageName) {
Uri uri = Uri.parse("package:" + packageName);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
context.startActivity(intent);
}
}
Javen wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!