Binder基本使用
Android開發中,Binder是一種跨進程通信方式,而使用AIDL可以實現Binder的工作。
如何使用它是了解它的第一步,本文章主要記錄使用Binder的一些步驟。(代碼思路參考《Android開發藝術探索》任玉剛 著)
1.創建兩個activity
兩個activity(OneActivity、TwoActivity),將OneActivity假設為服務端,TwoActivity假設為客戶端,分別運行在不同進程中
在AndroidManifest.xml中,為TwoActivity設置進程,這樣兩個activity就分別運行在不同的進程中了
2. 創建AIDL文件
在AIDL文件中聲明客戶端想要調用服務端的方法
interface IInfManager { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void setName(String name); String getName();}
AIDL文件聲明完,activity等文件并不能調用到IInfManager接口,需要在app的build.gradle文件中的android{}中添加
sourceSets{ main{ java.srcDirs = ['src/main/java', 'src/main/aidl'] }}
然后點擊sync now按鈕,activity文件就可以調用到IInfManager接口了,可以在app\build\generated\source\aidl\debug文件下找到自動生成的IInfManager.java文件。
3.創建Service
Service中創建Binder對象,在onBind方法中返回這個對象,Binder對象中具體實現了IInfManager接口中的方法。Service需要在AndroidManifest.xml中注冊。
public class InfManageService extends Service{ private String name; @Override public int onStartCommand(Intent intent, int flags, int startId) { name = intent.getStringExtra("name"); return super.onStartCommand(intent, flags, startId); } @Nullable @Override public IBinder onBind(Intent intent) { return binder; } private Binder binder = new IInfManager.Stub() { @Override public void setName(String mName) throws RemoteException { name = mName; } @Override public String getName() throws RemoteException { return name; } };}
4.服務端OneActivity
OneActivity中設置按鈕跳轉至TwoActivity,這里為了簡單,使用startService可以為InfManageService中name變量初始化"zhangsan"的值。也可以與客戶端TwoActivity中一樣,綁定service,建立連接,來設置name的值(具體參考下一步客戶端的用法)。
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_one); Intent intent = new Intent(OneActivity.this, InfManageService.class); intent.putExtra("name", "zhangsan"); startService(intent); btn_one_gototwo = (Button) findViewById(R.id.btn_one_gototwo); btn_one_gototwo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(OneActivity.this, TwoActivity.class); startActivity(intent); } });}
5.客戶端TwoActivity
首先綁定InfManageService服務,建立連接,連接成功后通過返回的IBinder對象可以獲得IInfManager接口,可以通過這個接口去使用服務端的方法。
private TextView tv_two_name;private Button btn_two_change; private IInfManager iInfManager; private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iInfManager = IInfManager.Stub.asInterface(service); try { tv_two_name.setText(iInfManager.getName()); Log.i("TwoActivity","first:" + iInfManager.getName()); iInfManager.setName("lisi"); Log.i("TwoActivity","next:" + iInfManager.getName()); }catch (RemoteException e){ } } @Override public void onServiceDisconnected(ComponentName name) { }}; @Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); tv_two_name = (TextView) findViewById(R.id.tv_two_name); btn_two_change = (Button) findViewById(R.id.btn_two_change); Intent intent = new Intent(TwoActivity.this, InfManageService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); btn_two_change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { iInfManager.setName("wangmazi"); tv_two_name.setText(iInfManager.getName()); } catch (RemoteException e) { e.printStackTrace(); } } });} @Overrideprotected void onDestroy() { super.onDestroy(); unbindService(connection);}
上面代碼onServiceConnected方法中,首先在TwoActivity界面中顯示了服務端的name變量內容"zhangsan"
推薦閱讀:相機怎么調曝光
相關閱讀
懷化熱線版權與免責聲明:
一、凡本站中注明“來源:懷化熱線”的所有文字、圖片和音視頻,版權均屬懷化熱線所有,轉載時必須注明“來源:懷化熱線”,并附上原文鏈接。
二、凡來源非懷化熱線的(作品)只代表本網傳播該消息,并不代表贊同其觀點。
如因作品內容、版權和其它問題需要同本網聯系的,請在見網后30日內進行聯系。