Activity與Service通訊的方式有三種:javascript
繼承Binder類java
這個方式只有當你的Acitivity和Service處於同一個Application和進程時,才能夠用,好比你後臺有一個播放背景音樂的Service,這時就能夠用這種方式來進行通訊。web
用例子來講明其使用方法:安全
1. 來看Service的寫法:多線程
- public class LocalService extends Service {
-
- private final IBinder mBinder = new LocalBinder();
-
- private final Random mGenerator = new Random();
-
-
-
-
- public class LocalBinder extends Binder {
- LocalService getService() {
-
- return LocalService.this;
- }
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
-
-
- public int getRandomNumber() {
- return mGenerator.nextInt(100);
- }
- }
在Service裏定義一個內部類,Binder的子類,經過這個類,把Service的對象傳給Activity,這樣Activity就能夠調用Service裏的公用方法和公用屬性了,但這種方式,必定要在同一個進程和同一個Application裏。app
2. 再看相應Activity的代碼:dom
- public class BindingActivity extends Activity {
- LocalService mService;
- boolean mBound = false;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- @Override
- protected void onStart() {
- super.onStart();
-
- Intent intent = new Intent(this, LocalService.class);
- bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
- }
-
- @Override
- protected void onStop() {
- super.onStop();
-
- if (mBound) {
- unbindService(mConnection);
- mBound = false;
- }
- }
-
-
- public void onButtonClick(View v) {
- if (mBound) {
-
- int num = mService.getRandomNumber();
- Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
- }
- }
-
-
- private ServiceConnection mConnection = new ServiceConnection() {
-
- @Override
- public void onServiceConnected(ComponentName className,
- IBinder service) {
-
- LocalBinder binder = (LocalBinder) service;
- mService = binder.getService();
- mBound = true;
- }
-
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- mBound = false;
- }
- };
- }
這裏就是經過IBinder來獲得LocalService對象,再去調用其Public方法。ide
使用Messengerthis
上面的方法只能在同一個進程裏才能用,若是要與另一個進程的Service進行通訊,則能夠用Messenger。spa
其實實現IPC的方式,還有AIDL,但推薦使用Messenger,有兩點好處:
1. 使用Messenger方式比使用AIDL的方式,實現起來要簡單不少
2. 使用Messenger時,全部從Activity傳過來的消息都會排在一個隊列裏,不會同時請求Service,因此是線程安全的。若是你的程序就是要多線程去訪問Service,就能夠用AIDL,否則最好使用Messenger的方式。
不過,其實Messenger底層用的就是AIDL實現的,看一下實現方式,先看Service的代碼:
- public class MessengerService extends Service {
-
- static final int MSG_SAY_HELLO = 1;
-
-
-
-
- class IncomingHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MSG_SAY_HELLO:
- Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
- break;
- default:
- super.handleMessage(msg);
- }
- }
- }
-
-
-
-
- final Messenger mMessenger = new Messenger(new IncomingHandler());
-
-
-
-
- @Override
- public IBinder onBind(Intent intent) {
- Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
- return mMessenger.getBinder();
- }
- }
再看一下Activity的代碼:
- public class ActivityMessenger extends Activity {
-
- Messenger mService = null;
-
-
- boolean mBound;
-
- private ServiceConnection mConnection = new ServiceConnection() {
- public void onServiceConnected(ComponentName className, IBinder service) {
-
-
- mService = new Messenger(service);
- mBound = true;
- }
-
- public void onServiceDisconnected(ComponentName className) {
- mService = null;
- mBound = false;
- }
- };
-
- public void sayHello(View v) {
- if (!mBound) return;
-
- Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
- try {
- mService.send(msg);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- @Override
- protected void onStart() {
- super.onStart();
-
- bindService(new Intent(this, MessengerService.class), mConnection,
- Context.BIND_AUTO_CREATE);
- }
-
- @Override
- protected void onStop() {
- super.onStop();
-
- if (mBound) {
- unbindService(mConnection);
- mBound = false;
- }
- }
- }
注意:以上寫的代碼只能實現從Activity向Service發送消息,若是想從Service向Activity發送消息,只要把代碼反過來寫就能夠了。
使用AIDL
這個方法略,若是知道上面兩種方法,這個方法基本不多會用到。