正好用到整理下

首先 添加 权限

电话监听

/**

*

* @author xiaoma

* 电话监听器类

*/

private class MobliePhoneStateListener extends PhoneStateListener {

@Override

public void onCallStateChanged(int state, String incomingNumber) {

switch (state) {

case TelephonyManager.CALL_STATE_IDLE: // 挂机状态

Intent intent = new Intent(PlayerActivity.this, PlayerService.class);

playBtn.setBackgroundResource(R.drawable.play_selector);

intent.setAction("com.xiaoma.media.MUSIC_SERVICE");

intent.putExtra("MSG", AppConstant.PlayerMsg.CONTINUE_MSG);//继续播放音乐

startService(intent);

isPlaying = false;

isPause = true;

break;

case TelephonyManager.CALL_STATE_OFFHOOK://通话状态

case TelephonyManager.CALL_STATE_RINGING://响铃状态

Intent intent2 = new Intent(PlayerActivity.this, PlayerService.class);

playBtn.setBackgroundResource(R.drawable.pause_selector);

intent2.setAction("com.xiaoma.media.MUSIC_SERVICE");

intent2.putExtra("MSG", AppConstant.PlayerMsg.PAUSE_MSG);

startService(intent2);

isPlaying = true;

isPause = false;

break;

default:

break;

}

}

}最后别忘了   添加来电监听事件

TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // 获取系统服务

telManager.listen(new MobliePhoneStateListener(),

PhoneStateListener.LISTEN_CALL_STATE); ok,很简单吧   非常感谢  小巫的 简、美音乐 的参考!

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐