生活随笔
收集整理的這篇文章主要介紹了
Activity与Service通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Activity與Service通信
Activity與Service通信的方式有三種:
?
繼承Binder類
? 這個方式只有當你的Acitivity和Service處于同一個Application和進程時,才可以用,比如你后臺有一個播放背景音樂的Service,這時就可以用這種方式來進行通信。
用例子來說明其使用方法:
? 1. 來看Service的寫法:
?
Java代碼??
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里。
? ?2. 再看相應Activity的代碼:
?
Java代碼??
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方法。
使用Messenger
? ?上面的方法只能在同一個進程里才能用,如果要與另外一個進程的Service進行通信,則可以用Messenger。
? ??其實實現IPC的方式,還有AIDL,但推薦使用Messenger,有兩點好處:
? ? ? 1. 使用Messenger方式比使用AIDL的方式,實現起來要簡單很多
? ? ? 2. 使用Messenger時,所有從Activity傳過來的消息都會排在一個隊列里,不會同時請求Service,所以是線程安全的。如果你的程序就是要多線程去訪問Service,就可以用AIDL,不然最好使用Messenger的方式。
? 不過,其實Messenger底層用的就是AIDL實現的,看一下實現方式,先看Service的代碼:
?
Java代碼??
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的代碼:
Java代碼??
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
? 這個方法略,如果知道上面兩種方法,這個方法基本很少會用到。
?
轉載于:https://www.cnblogs.com/qcjd/archive/2012/11/09/9325041.html
總結
以上是生活随笔為你收集整理的Activity与Service通信的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。