// service内部类,监听拨打接听电话,如果有电话呼入,则暂停播放,通话结束,继续播放 public static class PhoneStatReceiver extends BroadcastReceiver { private static boolean bPlayingFlg = false; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub // 呼出电话 if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { if (null != mMediaPlayer) { if (mMediaPlayer.isPlaying()) { bPlayingFlg = true; pause(); // 暂停播放 MusicDemo.mbSeekBarUpdateFlg = false; // 停止更新播放进度 } } } else { // 呼入电话 TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); switch (tm.getCallState()) { case TelephonyManager.CALL_STATE_RINGING: // 当前是来电 if (null != mMediaPlayer) { if (mMediaPlayer.isPlaying()) { bPlayingFlg = true; pause(); // 暂停播放 } } break; case TelephonyManager.CALL_STATE_OFFHOOK: break; case TelephonyManager.CALL_STATE_IDLE: // 挂机 Device call state: No activity. if (bPlayingFlg) { // 恢复播放 pauseToPlay(); MusicDemo.mbSeekBarUpdateFlg = true; // 更新播放进度 bPlayingFlg = false; } break; } } } }
在manifest中增加权限
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>