我正在构建一个音乐播放器,我可以通过音乐服务在后台播放音乐。但是,当我收到呼叫服务仍在后台运行时,问题就出现了。我想在通话期间保留我的音乐服务。
参考
Stopping & Starting music on incoming calls
我已经通过创建接收器尝试了但是出现了错误
PhoneReciever.java
public class PhoneReceiver extends BroadcastReceiver{
TelephonyManager telManager;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
this.context=context;
telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneListener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
//PAUSE
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
break;
}
case TelephonyManager.CALL_STATE_IDLE: {
//PLAY
break;
}
default: { }
}
}
catch (Exception ex) {
}
}
};
}
在Manifest.xml中添加
<receiver android:name=".PhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
logcat的: