android音樂播放器監(jiān)聽電話狀態(tài)實(shí)現(xiàn)代碼
如下代碼是監(jiān)聽電話的狀態(tài),代碼簡單不做介紹直接看代碼:
private boolean mResumeAfterCall = false;
private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int ringvolume = audioManager
.getStreamVolume(AudioManager.STREAM_RING);
if (ringvolume > 0) {
mResumeAfterCall = (player.isPlaying() || mResumeAfterCall);
try {
stub.pause();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
// pause the music while a conversation is in progress
mResumeAfterCall = (player.isPlaying() || mResumeAfterCall);
try {
stub.pause();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (state == TelephonyManager.CALL_STATE_IDLE) {
// start playing again
if (mResumeAfterCall) {
// resume playback only if music was playing
// when the call was answered
try {
stub.resume();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mResumeAfterCall = false;
}
}
}
};
使用的時(shí)候呢,就在service 的oncreate 里面添加,如下:
public void onCreate() {
player = new MediaPlayer();
super.onCreate();
TelephonyManager tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tmgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter filter = new IntentFilter();
filter.addAction(SERVICECMD);
filter.addAction(PAUSE_ACTION);
receiver = new MusicServiceReceiver();
registerReceiver(receiver, filter);
}
在ondestroy 里面刪除就可以了
public void onDestroy() {
// Log.e(TAG, "onDestroy" +player.isPlaying() );
// System.out.println("onDestroy");
unregisterReceiver(receiver);
player.release();
player = null;
TelephonyManager tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
tmgr.listen(mPhoneStateListener, 0);
super.onDestroy();
}
- 教你輕松制作Android音樂播放器
- android暫停或停止其他音樂播放器的播放實(shí)現(xiàn)代碼
- Android編程開發(fā)音樂播放器實(shí)例
- Android簡易音樂播放器實(shí)現(xiàn)代碼
- Android MediaPlayer實(shí)現(xiàn)音樂播放器實(shí)例代碼
- Android音樂播放器制作 掃描本地音樂顯示在手機(jī)(一)
- Android實(shí)現(xiàn)簡單音樂播放器(MediaPlayer)
- Android 音樂播放器的開發(fā)實(shí)例詳解
- 簡單實(shí)現(xiàn)Android本地音樂播放器
- Android利用代碼控制設(shè)備上其他音樂播放器的方法
相關(guān)文章
Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法
這篇文章主要介紹了Android design包自定義tablayout的底部導(dǎo)航欄的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01AndroidGUI27中findViewById返回null的快速解決辦法
這篇文章主要介紹了AndroidGUI27中findViewById返回null的快速解決辦法的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06android實(shí)現(xiàn)音樂跳動效果的示例代碼
這篇文章主要介紹了android實(shí)現(xiàn)音樂跳動效果的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android自定義控件實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android實(shí)現(xiàn)Bitmap位圖旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)Bitmap位圖旋轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android編程開發(fā)之seekBar采用handler消息處理操作的方法
這篇文章主要介紹了Android編程開發(fā)之seekBar采用handler消息處理操作的方法,結(jié)合實(shí)例分析了Android實(shí)現(xiàn)進(jìn)度條功能的相關(guān)技巧,需要的朋友可以參考下2015-12-12react native android6+拍照閃退或重啟的解決方案
android 6+權(quán)限使用的時(shí)候需要動態(tài)申請,那么在使用rn的時(shí)候要怎么處理拍照權(quán)限問題呢?本文提供的是一攬子rn操作相冊、拍照的解決方案,需要的朋友可以參考下2017-11-11android SectorMenuView底部導(dǎo)航扇形菜單的實(shí)現(xiàn)代碼
這篇文章主要介紹了android SectorMenuView底部導(dǎo)航扇形菜單的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02