以下是 SipDemo(voip 接听电话)的一部分:
public class WalkieTalkieActivity extends Activity {
...
public IncomingCallReceiver callReceiver;
public SipManager manager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
...
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
callReceiver = new IncomingCallReceiver();
this.registerReceiver(callReceiver, filter);
initializeManager();
}
public void initializeManager() {
initializeLocalProfile();
}
public void initializeLocalProfile() {
SipProfile.Builder builder = new SipProfile.Builder(username, domain);
builder.setPassword(password);
me = builder.build();
Intent i = new Intent();
i.setAction("android.SipDemo.INCOMING_CALL");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, Intent.FILL_IN_DATA);
manager.open(me, pi, null);
和广播接收器
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
};
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
incomingCall.answerCall(30);
...
wtActivity.call = incomingCall;
它工作得很好,但即使应用程序在后台运行,我也需要能够接听电话。所以我尝试以其他方式实现广播接收器(不是在活动中,而是在清单中)。于是删了:
IntentFilter filter = new IntentFilter();
filter.addAction("android.SipDemo.INCOMING_CALL");
callReceiver = new IncomingCallReceiver();
this.registerReceiver(callReceiver, filter);
并在清单中添加
<intent-filter>
<action android:name="android.SipDemo.INCOMING_CALL"/>
</intent-filter>
但现在它不起作用,incomingCall 始终为空。
在更改之前,BroadcastReceiver 得到了
context == com.example.android.sip.WalkieTalkieActivity@4051c5e8
和改变后
context == android.app.ReceiverRestrictedContext@4058ac48
也许这是问题,但有人知道如何解决它吗?