How to build a clear-structured Android Program

Source: Internet
Author: User

As the number of functional modules of Android programs increases, interactions between modules become more common. A system with a well-designed structure will not be copied and pasted in large quantities due to the above reasons, if a large number of copies and pastes occur, the structure design of the system fails. In the past, when writing an online e-commerce application, each Activity had an AsyncTask asynchronous thread to execute online download data and parse data, when a function module is added, a large amount of code is copied and pasted. The program is difficult to maintain. Therefore, for these applications that need to process a large amount of time-consuming work, a data center is responsible for receiving data requests. After processing the data, the data is returned asynchronously.

The initial assumption is that the data center is placed in the Service, and then through binderService, every Activity that wants to communicate with the Service can be connected to the Service. Then, the Handler sends a data request to the Service. The Service processes the data according to the request, and then returns the data to the object interested in the data through the BroadcastReceiver mechanism.

The following is the key code for implementation:

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
For (int j = persons. length-1; j> = 0; j --){
29
LogUtil. d (getApplicationContext (), persons [j] + "");
30
}
31

32
}
33
}
34
};
35

36
Private ServiceConnection conn = new ServiceConnection (){
37

38
@ Override
39
Public void onServiceDisconnected (ComponentName name ){
40
// TODO Auto-generated method stub
41

42
}
43

44
@ Override
45
Public void onServiceConnected (ComponentName name, IBinder service ){
46
MsgHandler = (DataCenterService. MyBinder) service). getMessageHandler ();
47

48
MsgHandler. postDelayed (refreshPersonData, 3000 );
49
}
50
};
51
@ Override
52
Public void onCreate (Bundle savedInstanceState ){
53
Super. onCreate (savedInstanceState );
54
SetContentView (R. layout. main );
55
MLocalBroadcastManager = DataCenterApplication. getLocalBroadcastManager ();
56

57
MLocalBroadcastManager. registerReceiver (personDataReceiver, new IntentFilter (DataCenterService. GET_PERSON_ACTION ));
58
BindService (new Intent (this, DataCenterService. class), conn, BIND_AUTO_CREATE );
59
}
60

61
Runnable refreshPersonData = new Runnable (){
62

63
@ Override
64
Public void run (){
65
MsgHandler. sendEmptyMessage (DataCenterService. GET_PERSON_DATA );
66
MsgHandler. postDelayed (this, 3000 );
67
}
68
};
69
}

Package com. hxcy;

Import android. app. Application;
Import android. support. v4.content. LocalBroadcastManager;

Public class DataCenterApplication extends Application {
Private static LocalBroadcastManager mLocalBroadcastManager;
@ Override
Public void onCreate (){
Super. onCreate ();
MLocalBroadcastManager = LocalBroadcastManager. getInstance (this );
}

Public static LocalBroadcastManager getLocalBroadcastManager (){
Return mLocalBroadcastManager;
}
}

Person class:

View sourceprint?
01
Package com. hxcy. model;
02
 
03
Import android. OS. Parcel;
04
Import android. OS. Parcelable;
05
 
06
Public class Person implements Parcelable {
07
Private String name;
08
Private int age;
09
Private String id;
10

11

12
Public String getName (){
13
Return name;
14
}
15
 
16
Public void setName (String name ){
17
This. name = name;
18
}
19
 
20
Public int getAge (){
21
Return age;
22
}
23
 
24
Public void setAge (int age ){
25
This. age = age;
26
}
27
 
28
Public String getId (){
29
Return id;
30
}
31
 
32
Public void setId (String id ){
33
This. id = id;
34
}
35
 
36
@ Override
37
Public int describeContents (){
38
Return 0;
39
}
40
 
41
@ Override
42
Public String toString (){
43
Return "Person [name =" + name + ", age =" + age + ", id =" + id + "]";
44
}
45
 
46
@ Override
47
Public void writeToParcel (Parcel dest, int flags ){
48
Dest. writeString (id );
49
Dest. writeString (name );
50
Dest. writeInt (age );
51
 
52
}
53

54
Public static final Parcelable. Creator <Person> CREATOR = new Parcelable. Creator <Person> (){
55
 
56
@ Override
57
Public Person createFromParcel (Parcel source ){
58
// TODO Auto-generated method stub
59www.2cto.com
Person person = new Person ();
60
Person. setId (source. readString ());
61
Person. setName (source. readString ());
62
Person. setAge (source. readInt ());
63
Return person;
64
}
65
 
66
@ Override
67
Public Person [] newArray (int size ){
68

69
Return new Person [size];
70
}
71

72
};
73
}


Author: fneg

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.