智慧云端日记

Just do IT


  • 首页

  • 归档

  • 标签

  • 捐赠列表

  • 搜索
close

Retrofit2+Rxjava+MVP实践

发表于 2016-08-27   |   分类于 Android , Android MVP   |     |   阅读次数

此博文根据前面两篇文章 Android MVP 架构初试 Android MVP 架构封装 再结合主流框架Retrofit2+Rxjava来个实践

源码地址RxMVP

项目截图

阅读全文 »

Android MVP 构架封装

发表于 2016-08-27   |   分类于 Android , Android MVP   |     |   阅读次数

上一篇我们简单实现了一个MVP的构架,下面我们来做一个简单的封装使其使用更简单方便

源码地址RxMVP分支Tag03

最终实现目录结构如下


阅读全文 »

Android MVP 构架初试

发表于 2016-08-27   |   分类于 Android , Android MVP   |     |   阅读次数

目前讨论MVP MVVM 的架构也来越多,这种构架也很适合Android。研究MVP记录如下

源码地址RxMVP分支Tag02

原有的MVC构架

刚开始接触Android的时候会觉得Android的整个代码架构就是一个MVC。

  • M : 业务层和模型层,相当与javabean和我们的业务请求代码
  • V : 视图层,对应Android的layout.xml布局文件
  • C : 控制层,对应于Activity中对于UI 的各种操作

看起来MVC架构很清晰,但是实际的开发中,请求的业务代码往往被丢到了Activity里面,大家都知道layout.xml的布局文件只能提供默认的UI设置,所以开发中视图层的变化也被丢到了Activity里面,再加上Activity本身承担着控制层的责任。所以Activity达成了MVC集合的成就,最终我们的Activity就变得越来越难看,从几百行变成了几千行。维护的成本也越来越高

新的MVP架构

  • M : 还是业务层和模型层
  • V : 视图层的责任由Activity来担当
  • P : 新成员Prensenter 用来代理 C(control) 控制层

MVP与MVC最大的不同,其实是Activity职责的变化,由原来的C (控制层) 变成了 V(视图层),不再管控制层的问题,只管如何去显示。控制层的角色就由我们的新人 Presenter来担当,这种架构就解决了Activity过度耦合控制层和视图层的问题。

阅读全文 »

Android极速开发一(Apk安装)

发表于 2016-08-27   |   分类于 Android , Android极速开发   |     |   阅读次数

安装之前判断是否有root权限,如果有root权限就静默安转,如果没有就利用意图进行安装

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
*
* @author Javen
* @since 2016/05/24
*/
public class ApkController {
/**
* 描述: 安装
*/
public static boolean install(String apkPath,Context context){
// 先判断手机是否有root权限
if(hasRootPerssion()){
// 有root权限,利用静默安装实现
return clientInstall(apkPath);
}else{
// 没有root权限,利用意图进行安装
File file = new File(apkPath);
if(!file.exists())
return false;
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
context.startActivity(intent);
return true;
}
}
/**
* 描述: 卸载
*/
public static boolean uninstall(String packageName,Context context){
if(hasRootPerssion()){
// 有root权限,利用静默卸载实现
return clientUninstall(packageName);
}else{
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,packageURI);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
return true;
}
}
/**
* 判断手机是否有root权限
*/
private static boolean hasRootPerssion(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默安装
*/
private static boolean clientInstall(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r "+apkPath);
// PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 静默卸载
*/
private static boolean clientUninstall(String packageName){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("LD_LIBRARY_PATH=/vendor/lib:/system/lib ");
PrintWriter.println("pm uninstall "+packageName);
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 启动app
* com.exmaple.client/.MainActivity
* com.exmaple.client/com.exmaple.client.MainActivity
*/
public static boolean startApp(String packageName,String activityName){
boolean isSuccess = false;
String cmd = "am start -n " + packageName + "/" + activityName + " \n";
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(process!=null){
process.destroy();
}
}
return isSuccess;
}
private static boolean returnResult(int value){
// 代表成功
if (value == 0) {
return true;
} else if (value == 1) { // 失败
return false;
} else { // 未知情况
return false;
}
}
/**
* 查询所有非系统app的信息
* @param mContext
* @return
*/
public static List<Map<String, Object>> getAPPInstalled(Context mContext) {
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
// 获取系统内的所有程序信息
Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
List<PackageInfo> packageinfo = mContext.getPackageManager()
.getInstalledPackages(0);
int count = packageinfo.size();
for (int i = 0; i < count; i++) {
PackageInfo pinfo = packageinfo.get(i);
ApplicationInfo appInfo = pinfo.applicationInfo;
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
// 系统程序 忽略
} else {
// 非系统程序
Map<String, Object> map = new HashMap<String, Object>();
map.put("app_logo", pinfo.applicationInfo.loadIcon(mContext
.getPackageManager()));
map.put("app_name", pinfo.applicationInfo.loadLabel(mContext
.getPackageManager()));
map.put("package_name", pinfo.applicationInfo.packageName);
map.put("app_version_name", pinfo.versionName);
map.put("app_version_code", pinfo.versionCode);
listItems.add(map);
}
}
return listItems;
}
/**
* 判断应用是否需要安装
* @param mContext
* @param packageName
* @param versionCode
* @return
*/
public static boolean isInstalled(Context mContext,String packageName,int versionCode) {
// 获取系统内的所有程序信息
Intent mainintent = new Intent(Intent.ACTION_MAIN, null);
mainintent.addCategory(Intent.CATEGORY_LAUNCHER);
List<PackageInfo> packageinfo = mContext.getPackageManager()
.getInstalledPackages(0);
int count = packageinfo.size();
String pn;
int vc;
for (int i = 0; i < count; i++) {
PackageInfo pinfo = packageinfo.get(i);
ApplicationInfo appInfo = pinfo.applicationInfo;
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
// 系统程序 忽略
} else {
// 非系统程序
pn=pinfo.applicationInfo.packageName;
vc=pinfo.versionCode;
if (pn.equalsIgnoreCase(packageName) && vc >= versionCode) {
return true;
}
}
}
return false;
}
}

Android Studio 添加apt的支持

发表于 2016-08-27   |   分类于 Android   |     |   阅读次数

我们在使用butterknife的时候需要apt的支持

1
2
3
//注解绑定view
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'

默认的情况下gradle 是支持的,下面是解决方案

阅读全文 »
1…567…10
Javen

Javen

凡事皆有终结,因此,耐心是赢得成功的一种手段。

48 日志
16 分类
23 标签
Git 微博 博客 QQ
友情链接
  • Git教程
  • NextT
  • 友情链接
© 2015 - 2017 Javen
由 Hexo 强力驱动
主题 - NexT.Pisces
本站访客数人次 本站总访问量次