Android使用AIDL方式實(shí)現(xiàn)播放音樂案例
本文實(shí)例為大家分享了Android使用AIDL方式實(shí)現(xiàn)播放音樂的具體代碼,供大家參考,具體內(nèi)容如下
思路:
① 新建兩個(gè)APP項(xiàng)目或者M(jìn)odule,一個(gè)作為服務(wù)端,一個(gè)作為客戶端,在服務(wù)端創(chuàng)建service
② 在兩個(gè)module的main文件中分別新建兩個(gè)aidl文件(接口),里邊定義處理音樂的方法
③ 在兩個(gè)AIDL文件定義過方法后在任務(wù)欄給他們makeproject,編譯成Java文件,才能在service和acvitity中使用
interface.Stub需要實(shí)例化,實(shí)現(xiàn)遠(yuǎn)程方法
④.Service中onbind方法,返回的是:interface.Stub。
sevice需要設(shè)置action,不然客戶端service運(yùn)行時(shí)會(huì)報(bào)空指針異常
先處理服務(wù)端:
Mainfest文件中,為intent隱式調(diào)用添加action
<service android:name=".MusicService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.work.MusicService"></action> </intent-filter>
MusicService。Java
public class MusicService extends Service { private MediaPlayer player = null; public MusicService() { } //①實(shí)現(xiàn)已經(jīng)makeproject過的adil接口,重寫里邊自定義定義的三個(gè)方法 MusicAidlInterface.Stub stub = new MusicAidlInterface.Stub() { @Override public void paly() throws RemoteException { if (player == null) { player = MediaPlayer.create(MusicService.this, R.raw.hckz); } if (player != null && !player.isPlaying()){ player.start(); } } @Override public void paus() throws RemoteException { if(player!=null&&player.isPlaying()){ player.pause(); } } @Override public void stop() throws RemoteException { if(player!=null){ player.stop(); } try { player.prepare();//重新準(zhǔn)備下次播放 } catch (IOException e) { e.printStackTrace(); } } }; @Override public IBinder onBind(Intent intent) { return stub; } //② 重寫service銷毀時(shí)的方法 @Override public void onDestroy() { super.onDestroy(); if(player!=null){ player.stop(); player.release();//釋放資源,防止失去依托溢出,發(fā)生異常 } }
處理客戶端
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_play, btn_pause, btn_stop, btn_stopservice, btn_stopacvitity; MusicAidlInterface service = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intiView(); connect(); } private void intiView() { btn_play = (Button) findViewById(R.id.btn_play); btn_pause = (Button) findViewById(R.id.btn_pause); btn_stopservice = (Button) findViewById(R.id.btn_stopservice); btn_stopacvitity = (Button) findViewById(R.id.btn_exitacvitity); btn_stop = (Button) findViewById(R.id.btn_stop); btn_play.setOnClickListener(this); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this); btn_stopservice.setOnClickListener(this); btn_stopacvitity.setOnClickListener(this); } @Override public void onClick(View v) { try { switch (v.getId()) { case R.id.btn_play: service.paly(); break; case R.id.btn_pause: service.paus(); break; case R.id.btn_stop: service.stop(); break; case R.id.btn_stopservice: if (conn != null) { unbindService(conn); } break; case R.id.btn_exitacvitity: finish(); break; } } catch (RemoteException e) { e.printStackTrace(); } } //連接service ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder iBinder) { service = MusicAidlInterface.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName name) { service = null; } }; //獲取連接BindService的方法 private void connect() { //使用intent的隱式調(diào)用方法 Intent intent = new Intent("com.work.MusicService"); //設(shè)置目標(biāo)service的包名 intent.setPackage("com.work.musicservice"); bindService(intent, conn, BIND_AUTO_CREATE); } //重寫acvitity銷毀時(shí)候解除綁定的方法 @Override protected void onDestroy() { super.onDestroy(); if (conn != null) {//解除綁定 unbindService(conn); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android AIDL實(shí)現(xiàn)兩個(gè)APP間的跨進(jìn)程通信實(shí)例
- Android使用AIDL實(shí)現(xiàn)兩個(gè)App間通信
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- 使用Android studio創(chuàng)建的AIDL編譯時(shí)找不到自定義類的解決辦法
- Android 使用【AIDL】調(diào)用外部服務(wù)的解決方法
- 基于Android AIDL進(jìn)程間通信接口使用介紹
- Android程序設(shè)計(jì)之AIDL實(shí)例詳解
- 實(shí)例講解Android中的AIDL內(nèi)部進(jìn)程通信接口使用
- Android Studio創(chuàng)建AIDL文件并實(shí)現(xiàn)進(jìn)程間通訊實(shí)例
- Android多進(jìn)程間采用AIDL方式進(jìn)行通信
相關(guān)文章
去掉RecycleView或者ListView上下滑動(dòng)陰影的方法
下面小編就為大家分享一篇去掉RecycleView或者ListView上下滑動(dòng)陰影的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01android TextView設(shè)置中文字體加粗實(shí)現(xiàn)方法
android TextView設(shè)置中文字體加粗如何實(shí)現(xiàn),接下來介紹實(shí)現(xiàn)方法,有需要的朋友可以參考下2013-01-01Flutter 自定義Drawer 滑出位置的大小實(shí)例代碼詳解
這篇文章主要介紹了Flutter 自定義Drawer 滑出位置的大小,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Flutter實(shí)現(xiàn)不同縮放動(dòng)畫效果詳解
這篇文章主要為大家詳細(xì)介紹了Flutter利用不同組件(ScaleTransition、SizeTransition、AnimatedSize和AnimatedBuilder)實(shí)現(xiàn)不同縮放動(dòng)畫效果,感興趣的可以動(dòng)手嘗試一下2022-06-06通過Jetpack Compose實(shí)現(xiàn)雙擊點(diǎn)贊動(dòng)畫效果
這篇文章主要介紹了如何利用Jetpack Compose實(shí)現(xiàn)雙擊點(diǎn)贊動(dòng)畫效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-01-01Android 處理OnItemClickListener時(shí)關(guān)于焦點(diǎn)顏色的設(shè)置問題
這篇文章主要介紹了Android 處理OnItemClickListener時(shí)關(guān)于焦點(diǎn)顏色的設(shè)置問題的相關(guān)資料,需要的朋友可以參考下2017-02-02