我正在开发一个呼叫和接收SIP呼叫的应用程序。 我在应用程序打开时收到SIP呼叫,但在后台运行应用程序时却没有收到呼叫。
这是我的服务类,广播接收器通知来电并开始Activity
接听电话:
public class ReceiveCallService extends Service {
BroadcastReceiver mReceiver;
// use this as an inner class like here or as a top-level class
public static class MyReceiver extends BroadcastReceiver {
public SipAudioCall incomingCall;
public static MediaPlayer mPlayer;
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("com.myapp",
"com.myapp.SipCallReceiveActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mPlayer = MediaPlayer
.create(MyAppContext.getContext(), R.raw.sugar);
mPlayer.setLooping(true);
mPlayer.start();
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
@Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
Constant.incomingCall.startAudio();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onCallHeld(SipAudioCall call) {
// TODO Auto-generated method stub
super.onCallHeld(call);
}
@Override
public void onCallEstablished(SipAudioCall call) {
// TODO Auto-generated method stub
super.onCallEstablished(call);
Log.e("call Is Established", "Yes!!");
}
@Override
public void onCallBusy(SipAudioCall call) {
// TODO Auto-generated method stub
super.onCallBusy(call);
AppSettings.setSp_iscallEnded(
MyAppContext.getContext(), true);
}
@Override
public void onCallEnded(SipAudioCall call) {
// TODO Auto-generated method stub
try {
call.endCall();
Log.e("call Is Ended Service...", "Yes!!");
AppSettings.setSp_iscallEnded(
MyAppContext.getContext(), true);
mPlayer.stop();
} catch (SipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onCallEnded(call);
}
};
incomingCall = Constant.manager.takeAudioCall(intent, listener);
if (AppSettings.getSp_isOutGoingcallRunning(MyAppContext
.getContext()) == false
&& AppSettings.getSp_isInComingcallRunning(MyAppContext
.getContext()) == false) {
Constant.incomingCall = incomingCall;
Constant.updateStatus(incomingCall);
context.startActivity(i);
AppSettings.SetSp_IsIncomingCall(MyAppContext.getContext(),
true);
}
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
if (Constant.me != null) {
try {
Constant.manager.close(Constant.me.getUriString());
} catch (Exception ee) {
Log.i("WalkieTalkieActivity/InitiateCall",
"Error when trying to close manager.", ee);
ee.printStackTrace();
}
}
}
}
// constructor
public MyReceiver() {
}
}
@Override
public void onCreate() {
// get an instance of the receiver in your service
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
MyReceiver.mPlayer.stop();
}
}
现在我希望在应用程序中接收呼叫,即使它在后台。
答案 0 :(得分:0)
在Android清单中注册广播接收器,如:
<receiver android:name="com.something.CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" >
</action>
</intent-filter>
</receiver>
并在你的BroadcastReciever Class中获取来电事件。
OnReceive()方法中的
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if ((state != null) && (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
使用它。
享受快乐的编码:)