Android後台進程與前台線程間的區別使用

來源:互聯網
上載者:User

標籤:多線程   aidl   service   子進程   


部落格出自:http://blog.csdn.net/liuxian13183,轉載註明出處! All Rights Reserved !


很早就翻譯過Android API的一篇文章Android進階開發第四講--API之Service,今天主要講些實戰的東西

比如Service的啟動方式有兩種,首先在AndroidManifest.xml中配置Service,然後通過bindService和startService來啟動,

不同點在於:

執行方式:前者啟動時執行onCreate-->onBind方法而且只執行一次;後者執行onCreate-->onStartCommand方法,onStartCommand方法卻可以執行多次

通訊方式:前者只能通過onBind傳遞一次值,然後拿到一個Bind類的引用來全傳值

ForeService

public int last(int index) {if (index == 0) {index = 4;} else {index -= 1;}sImageId = index % 5;return sImageId;}public class MyBinder extends Binder {public ForeService getService() {return ForeService.this;}}

調用類

foreService = new Intent(this, ForeService.class);// 建立串連foreConn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubmyBinder = (MyBinder) service;transPortFore();}};
後者可以每次都可以通過onStartCommand方法裡的intent對象傳值,也可以通過上述方法傳值。

以上是啟動方式。當然不可能就講這麼一點知識,接下來的東西比較重要。

由於Android是根據進程來分配記憶體的,而我們的App運行在一塊虛擬機器上,也就是一個主進程;那麼如果執行邏輯比較複雜,即使再多的線程(而且有限制),方法來了,給主進程開一個子進程,用什麼來開呢?對,就是Service!因為今天只講它。

首先在AndroidManifest.xml(如果一直讀我部落格,請記得以後它稱為主設定檔),先看它,註冊Service

<service android:name="com.liuxian13183.service.BkService" android:process=".core" /><service android:name="com.liuxian13183.service.ForeService" />
我們可以看到命名為BkService的類,比下面ForeService多一個參數android:process,對就是這個參數,如果你不設就預設跟主進程同一個進程,如果設了不論點.還是冒號:,都代表新啟動一個進程,而子進程跟主進程之間只能通過AIDL來通訊。

其次,定義一個AIDL檔案,傳回值可以使用基礎資料類型+List和Map,主要用來傳值溝通

例:

package com.liuxian13183.service;interface INext{ int next(int index);}


最後定義Service,在前端調用

Service定義:

public class BkService extends Service {private int sImageId = 0;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubsImageId = intent.getIntExtra("key", 0);return new Next();}public class Next extends INext.Stub {@Overridepublic int next(int index) throws RemoteException {// TODO Auto-generated method stubindex += 1;sImageId = index % 5;return sImageId;}}}
前端調用

backService = new Intent(this, BkService.class);backConn = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubLog.i("com.liuxian13183.service", "");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubiNext = INext.Stub.asInterface(service);transPortBack();}};

if (iNext != null) {transPortBack();} else {// getConnectionbackService.putExtra("key", sImageId);startService(backService);bindService(backService, backConn, BIND_AUTO_CREATE);// 回調}

private void transPortBack() {try {sImageId = iNext.next(sImageId);} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}showAlbum();}private void showAlbum() {InputStream rawImageStream = getResources().openRawResource(sImageArray[sImageId]);byte[] data = WebpUtils.streamToBytes(rawImageStream);final Bitmap webpBitmap = WebpUtils.webpToBitmap(data);imageView.setImageBitmap(webpBitmap);}

效果如下:


這你最想要的源碼!Android Service和webp講解源碼 

Android後台進程與前台線程間的區別使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.