如何构建一个结构清晰的Android程序
随着Android程序功能模块越来越多,模块之间的交互也日渐平常,一个结构在开始就设计良好的系统,不会因为上述的原因就出现大量的复制粘贴,如果出现大量的复制粘贴,那说明这个系统的结构设计是失败的。以前在写一个电子商务类联网应用的时候,每个Activity都有一个AsyncTask的异步线程去执行联网下载数据,解析数据,每当新增一个功能模块的时候就会出现大量的代码复制粘贴。导致程序难以维护。所以对于这些需要处理大量耗时工作的应用,提出了一个由数据中心负责接收数据请求,处理好数据后将数据异步返回的机制。
初步的设想是将数据中心放在Service中,然后通过binderService,每个想要和Service通信的Activity都可以连接到Service中。然后通过Handler发送数据请求给Service,Service根据请求,处理数据,然后通过BroadcastReceiver机制将数据返回给对此类数据感兴趣的对象。
一下是实现的关键代码:
01
package com.hxcy;
02
03
import android.app.Service;
04
import android.content.Intent;
05
import android.os.Binder;
06
import android.os.Bundle;
07
import android.os.Handler;
08
import android.os.Handler.Callback;
09
import android.os.IBinder;
10
import android.os.Message;
11
import android.support.v4.content.LocalBroadcastManager;
12
13
import com.hxcy.model.Person;
14
15
public class DataCenterService extends Service implements Callback {
16
private Handler msgHandler;
17
18
private MyBinder binder;
19
20
public static final int GET_PERSON_DATA = 0X001;
21
public static final int GET_COUNTRY_DATA = 0X002;
22
23
public static final String GET_PERSON_ACTION = "com.hxcy.action.person";
24
public static final String GET_COUNTRY_ACTION = "com.hxcy.action.country";
25
26
private LocalBroadcastManager mLocalBroadcastManager;
27
28
public class MyBinder extends Binder {
29
public Handler getMessageHandler() {
30
return msgHandler;
31
}
32
}
33
34
@Override
35
public IBinder onBind(Intent intent) {
36
binder = new MyBinder();
37
msgHandler = new Handler(this);
38
mLocalBroadcastManager = DataCenterApplication
39
.getLocalBroadcastManager();
40
return binder;
41
}
42
43
@Override
44
public boolean handleMessage(Message msg) {
45
switch (msg.what) {
46
case GET_PERSON_DATA:
47
new PersonDataThrea().start();
48
return true;
49
case GET_COUNTRY_DATA:
50
51
return true;
52
default:
53
return false;
54
}
55
}
56
57
class PersonDataThrea extends Thread {
58
59
@Override
60
public void run() {
61
62
Intent intent = new Intent(GET_PERSON_ACTION);
63
Bundle bundle = new Bundle();
64
Person[] persons = new Person[5];
65
for (int i = 0; i < 5; i++) {
66
Person person = new Person();
67
person.setAge(i + 20);
68
person.setName(i + " person");
69
person.setId(i + ":" + System.currentTimeMillis());
70
persons[i] = person;
71
}
72
bundle.putParcelableArray("persons", persons);
73
intent.putExtras(bundle);
74
mLocalBroadcastManager.sendBroadcast(intent);
75
76
}
77
78
}
79
}
01
package com.hxcy;
02
03
import android.app.Activity;
04
import android.content.BroadcastReceiver;
05
import android.content.ComponentName;
06
import android.content.Context;
07
import android.content.Intent;
08
import android.content.IntentFilter;
09
import android.content.ServiceConnection;
10
import android.os.Bundle;
11
import android.os.Handler;
12
import android.os.IBinder;
13
import android.support.v4.content.LocalBroadcastManager;
14
15
import com.hxcy.model.Person;
16
import com.hxcy.util.LogUtil;
17
18
public class DataCenterDemoActivity extends Activity {
19
private LocalBroadcastManager mLocalBroadcastManager;
20
private Handler msgHandler;
21
private BroadcastReceiver personDataReceiver=new BroadcastReceiver() {
22
23
@Override
24
public void onReceive(Context context, Intent intent) {
25
Bundle bundle=intent.getExtras();
26
if(bundle!=null){
27
Person[] persons=(Person[]) bundle.getParcelableArray("persons");
28
&
补充:移动开发 , Android ,