亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Android IPC機制ACtivity綁定Service通信代碼實例

瀏覽:12日期:2022-09-22 11:53:02

Binder通信過程類似于TCP/IP服務(wù)連接過程binder四大架構(gòu)Server(服務(wù)器),Client(客戶端),ServiceManager(DNS)以及Binder驅(qū)動(路由器)

其中Server,Client,ServiceManager運行于用戶空間,驅(qū)動運行于內(nèi)核空間。這四個角色的關(guān)系和互聯(lián)網(wǎng)類似:Server是服務(wù)器,Client是客戶終端,SMgr是域名服務(wù)器(DNS),驅(qū)動是路由器。

book.java

package com.example.android_binder_testservice;import android.os.Parcel;import android.os.Parcelable;public class Book implements Parcelable { private String bookName; private String author; private int publishDate; public Book() { } public Book(String bookName, String author, int publishDate) { super(); this.bookName = bookName; this.author = author; this.publishDate = publishDate; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPublishDate() { return publishDate; } public void setPublishDate(int publishDate) { this.publishDate = publishDate; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel out, int flags) { out.writeString(bookName); out.writeString(author); out.writeInt(publishDate); }public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() { @Override public Book[] newArray(int size) { return new Book[size]; } @Override public Book createFromParcel(android.os.Parcel source) { return new Book(source); } }; public Book(Parcel in) { bookName = in.readString(); author = in.readString(); publishDate = in.readInt(); }}

上面是一個 實現(xiàn)了parcelable的實體類,就是將book序列化,在putExtra到Service時會被寫入內(nèi)存加快程序速度

mainActivity.java

package com.example.android_binder_testservice;import android.os.Bundle;import android.util.Log;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { Button startServiceButton;// 啟動服務(wù)按鈕 Button shutDownServiceButton;// 關(guān)閉服務(wù)按鈕 Button startBindServiceButton;// 啟動綁定服務(wù)按鈕 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWidget(); regiestListener(); } public void getWidget(){ startServiceButton = (Button) findViewById(R.id.startService); startBindServiceButton = (Button) findViewById(R.id.bindService); shutDownServiceButton = (Button) findViewById(R.id.stopService); } public void regiestListener() { startServiceButton.setOnClickListener(startService); shutDownServiceButton.setOnClickListener(shutdownService); startBindServiceButton.setOnClickListener(startBinderService); } /** 啟動服務(wù)的事件監(jiān)聽 */ public Button.OnClickListener startService = new Button.OnClickListener() { public void onClick(View view) { /** 單擊按鈕時啟動服務(wù) */ Intent intent = new Intent(MainActivity.this, CountService.class); startService(intent); Log.v('MainStadyServics', 'start Service'); } }; /** 關(guān)閉服務(wù) */ public Button.OnClickListener shutdownService = new Button.OnClickListener() { public void onClick(View view) { /** 單擊按鈕時啟動服務(wù) */ Intent intent = new Intent(MainActivity.this, CountService.class); /** 退出Activity是,停止服務(wù) */ stopService(intent); Log.v('MainStadyServics', 'shutDown serveice'); } }; /** 打開綁定服務(wù)的Activity */ public Button.OnClickListener startBinderService = new Button.OnClickListener() { public void onClick(View view) { /** 單擊按鈕時啟動服務(wù) */ Intent intent = new Intent(MainActivity.this, UseBrider.class); startActivity(intent); Log.v('MainStadyServics', 'start Binder Service'); } };}

mainActivity中當(dāng)使用startService()啟動Service時會調(diào)用Service的onStartCommand()

當(dāng)使用bindService()則會調(diào)用onBind()方法,可能會覺了看的又看怎么沒看到bindService()這個方法呢

重點在

Intent intent = new Intent(MainActivity.this, UseBrider.class);startActivity(intent);

繼續(xù)上代碼

UseBrider.java

/** 通過bindService和unBindSerivce的方式啟動和結(jié)束服務(wù) */public class UseBrider extends FragmentActivity { /** 參數(shù)設(shè)置 */ CountService countService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new UseBriderFace(this)); Intent intent = new Intent(UseBrider.this, CountService.class); intent.putExtra('book', new Book('name', 'an', 1999));/** 進入Activity開始服務(wù) * conn */ bindService(intent, conn, Context.BIND_AUTO_CREATE); } private ServiceConnection conn = new ServiceConnection() { /* * 這個方法會獲取到CountService的onBind方法中返回的Binder對象 * 然后就可以對服務(wù)進行某種操作了 */ public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub countService = ((CountService.ServiceBinder) service).getService(); countService.callBack(); } /** 無法獲取到服務(wù)對象時的操作 */ public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub countService = null; } }; protected void onDestroy() { super.onDestroy(); this.unbindService(conn); Log.v('MainStadyServics', 'out'); }}

UseBriderFace.java

public class UseBriderFace extends View{ /**創(chuàng)建參數(shù)*/ public UseBriderFace(Context context){ super(context); } public void onDraw(Canvas canvas){ canvas.drawColor(Color.WHITE);//畫白色背景/**繪制文字*/ Paint textPaint = new Paint(); textPaint.setColor(Color.RED); textPaint.setTextSize(30); canvas.drawText('使用綁定服務(wù)', 10, 30, textPaint); textPaint.setColor(Color.GREEN); textPaint.setTextSize(18); canvas.drawText('使用綁定服務(wù)后,這個Activity關(guān)閉后', 20, 60, textPaint); canvas.drawText('綁定的服務(wù)也會關(guān)閉', 5, 80, textPaint); } }

UseBriderFace.java類其實就是用java定義的布局可以用xml文件代替

countService.java

package com.example.android_binder_testservice;/**引入包*/import android.app.Service;// 服務(wù)的類import android.os.IBinder;import android.os.Parcel;import android.os.RemoteException;import android.os.Binder;import android.content.Intent;import android.util.Log;/** 計數(shù)的服務(wù) */public class CountService extends Service { private String TAG = CountService.class.getSimpleName(); /** 創(chuàng)建參數(shù) */ boolean threadDisable; int count; Book book;/* * 當(dāng)通過bindService()啟動CountService時會調(diào)用這個方法并返回一個ServiceBinder對象 * 這個Binder對象封裝著一個CountService實例, * 客戶端就可以通過ServiceBinder對服務(wù)端進行一些操作 */ public IBinder onBind(Intent intent) { Log.i(TAG, 'onBind'); book = intent.getParcelableExtra('book'); return new ServiceBinder(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, 'onStartCommand'); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { Log.i(TAG, 'onUnbind'); return super.onUnbind(intent); } @Override public void onRebind(Intent intent) { Log.i(TAG, 'onRebind'); super.onRebind(intent); } public void onCreate() { super.onCreate(); /** 創(chuàng)建一個線程,每秒計數(shù)器加一,并在控制臺進行Log輸出 */ new Thread(new Runnable() { public void run() {while (!threadDisable) { try { Thread.sleep(1000); } catch (InterruptedException e) { } count++; Log.v('CountService', 'Count is' + count);} } }).start(); Log.i(TAG, 'onCreate'); } public void onDestroy() { super.onDestroy(); /** 服務(wù)停止時,終止計數(shù)進程 */ this.threadDisable = true; Log.i(TAG, 'onDestroy'); } public int getConunt() { return count; } public void callBack(){ Log.i(TAG, 'hello,i am a method of CountService'); } class ServiceBinder extends Binder { public CountService getService() { return CountService.this; } }}

代碼解釋有了,想不出來了

源碼下載地址:http://git.oschina.net/zwh_9527/Binder

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 在线免费一级片 | 你懂的国产 | 黑人插 | 黄色aaa| 天天色综合色 | 色噜噜人体337p处破 | 国产精品999在线 | 日韩一级特黄 | 国产一区二区在线播放 | 伊人久久综合影院 | 国产尤物视频在线 | 国产不卡在线播放 | 国产精品大全国产精品 | 91啦中文成人 | 欧美日韩国产免费一区二区三区 | 中文字幕在线播放量 | 欧美洲久久日韩欧美 | 免费jizz在线播放视频高清版 | 综合网视频 | 逼逼毛片 | 伊人热人久久中文字幕 | 精品久久一区 | 亚洲精品一区二区三区r | 黄色片日本网站 | 伊人色综合网一区二区三区 | 最新更新国内自拍视频 | 韩国一级做a爱性色毛片 | 久久大胆视频 | 亚洲欧美日韩激情在线观看 | 99久久99久久精品免费看蜜桃 | 免费大片黄在线观看日本 | 鲁大师视频在线www观看 | 2022中文字字幕久亚洲 | 一级亚洲 | 中文字幕日韩一区二区 | 狠狠色成人综合网图片区 | 中文字幕在线第一页 | 欧美精品三区 | 2021精品国夜夜天天拍拍 | 亚洲国产精品国产自在在线 | 久久精品国产免费中文 |