Android總結篇系列:Android Service
阿新 • • 發佈:2018-12-30
1 public class CActivity extends Activity {
2
3 public static final String TAG = "CActivity";
4
5 private Button bindServiceBtn;
6 private Button unbindServiceBtn;
7 private Button sendMsgToServerBtn;
8
9 private ServiceConnection sc = new MyServiceConnection();
10 private boolean mBound;
11
12 private Messenger mServerMessenger;
13
14 private Handler mClientHandler = new MyClientHandler();
15 private Messenger mClientMessenger = new Messenger(mClientHandler);
16
17 private class MyClientHandler extends Handler {
18 @Override
19 public void handleMessage(Message msg) {
20 if (msg.what == MyMessengerService.MSG_FROM_SERVER_TO_CLIENT) {
21 Log.w(TAG, "reveive msg from server");
22 }
23 }
24 }
25
26 private class MyServiceConnection implements ServiceConnection {
27
28 @Override
29 public void onServiceConnected(ComponentName name, IBinder binder) {
30 Log.w(TAG, "in MyServiceConnection onServiceConnected");
31 mServerMessenger = new Messenger(binder);
32
33 mBound = true;
34 }
35
36 @Override
37 public void onServiceDisconnected(ComponentName name) {
38 // This is called when the connection with the service has been
39 // unexpectedly disconnected -- that is, its process crashed.
40 Log.w(TAG, "in MyServiceConnection onServiceDisconnected");
41
42 mBound = false;
43 }
44 }
45
46 @Override
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.c);
50
51 bindServiceBtn = (Button) findViewById(R.id.bind_service);
52 unbindServiceBtn = (Button) findViewById(R.id.unbind_service);
53 sendMsgToServerBtn = (Button) findViewById(R.id.send_msg_to_server);
54
55 bindServiceBtn.setOnClickListener(new View.OnClickListener() {
56 @Override
57 public void onClick(View v) {
58 Intent intent = new Intent(CActivity.this, MyMessengerService.class);
59 bindService(intent, sc, Context.BIND_AUTO_CREATE);
60 }
61 });
62
63 unbindServiceBtn.setOnClickListener(new View.OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 excuteUnbindService();
67 }
68 });
69
70 sendMsgToServerBtn.setOnClickListener(new View.OnClickListener() {
71 @Override
72 public void onClick(View v) {
73 sayHello();
74 }
75 });
76
77 new Handler().postDelayed(new Runnable() {
78 @Override
79 public void run() {
80 Intent intent = new Intent(CActivity.this, MyAlarmBroadcastReceiver.class);
81 sendBroadcast(intent);
82 }
83 }, 3 * 1000);
84
85 }
86
87 public void sayHello() {
88 if (!mBound)
89 return;
90 // Create and send a message to the service, using a supported 'what' value
91 Message msg = Message.obtain(null, MyMessengerService.MSG_FROM_CLIENT_TO_SERVER, 0, 0);
92 // 通過replyTo把client端的Messenger(信使)傳遞給service
93 msg.replyTo = mClientMessenger;
94 try {
95 mServerMessenger.send(msg);
96 } catch (RemoteException e) {
97 e.printStackTrace();
98 }
99 }
100
101 private void excuteUnbindService() {
102 if (mBound) {
103 unbindService(sc);
104 mBound = false;
105 }
106 }
107
108 @Override
109 protected void onDestroy() {
110 super.onDestroy();
111 Log.w(TAG, "in onDestroy");
112 excuteUnbindService();
113 }
114 }