我正在使用Android SIP STACK开发SIP应用程序。 当应用程序打开时,我可以拨打和接听电话并获得电话铃声。但是,当应用关闭时,不会收到任何电话。如何实现这一目标? 我的代码如下:
在活动中注册广播
IntentFilter filter = new IntentFilter();
filter.addAction("android.MakeCallActivity.INCOMING_CALL");
callReceiver = new IncomingCallReceiver();
this.registerReceiver(callReceiver, filter);
我的来电接收方
/**
* Listens for incoming SIP calls, intercepts and hands them off to
* WalkieTalkieActivity.
*/
public class IncomingCallReceiver extends BroadcastReceiver {
static SipAudioCall incomingCall = null;
private static MediaPlayer mediaPlayer;
/**
* Processes the incoming call, answers it, and hands it over to the
* WalkieTalkieActivity.
*
* @param context
* The context under which the receiver is running.
* @param intent
* The intent being received.
*/
@Override
public void onReceive(Context context, Intent intent) {
final MakeCallActivity siActivity = (MakeCallActivity) context;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
@Override
public void onRinging(SipAudioCall call, SipProfile caller) {
// call.answerCall(10);
}
@Override
public void onCallEstablished(SipAudioCall call) {
// Call picked UI
}
@Override
public void onCallEnded(SipAudioCall call) {
// Call ended. Back to normal UI
}
};
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(context, RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mediaPlayer.prepare();
mediaPlayer.start();
incomingCall = siActivity.manager.takeAudioCall(intent, listener);
showIncomingCallGui(intent, context);
siActivity.call = incomingCall;
// incomingCall = siActivity.manager.takeAudioCall(intent,
// listener);
// incomingCall.answerCall(10);
// incomingCall.startAudio();
//
// siActivity.call = incomingCall;
siActivity.updateStatus(incomingCall);
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
}
}
}
清单
<receiver android:name=".sip.core.IncomingCallReceiver" android:label="Call Receiver"/>
我也试过服务但没有运气
我的服务
public class IncomingCallService extends Service {
private static IncomingCallReceiver m_CallReceiver;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
registerCallOffReceiver();
}
@Override
public void onDestroy() {
unregisterReceiver(m_CallReceiver);
m_CallReceiver = null;
}
private void registerCallOffReceiver() {
m_CallReceiver = new IncomingCallReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Service Para Llamadas", "android.call.INCOMING_CALL");
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter("android.call.INCOMING_CALL");
registerReceiver(m_CallReceiver, filter);
}
}
并显示
<service
android:name=".sip.core.IncomingCallService"
android:enabled="true" />
欢迎任何帮助