【发布时间】:2011-02-06 23:05:22
【问题描述】:
我目前在 Android 中有一个 Service,它是一个示例 VOIP 客户端,因此它会侦听 SIP 消息,如果收到一条消息,它会启动一个带有 UI 组件的 Activity 屏幕。
然后以下 SIP 消息确定 Activity 在屏幕上显示的内容。 例如,如果它是一个来电,它将显示应答或拒绝或一个去电,它将显示一个拨号屏幕。
此刻我使用 Intents 让 Activity 知道它应该显示什么状态。
一个例子如下:
Intent i = new Intent();
i.setAction(SIPEngine.SIP_TRYING_INTENT);
i.putExtra("com.net.INCOMING", true);
sendBroadcast(i);
Intent x = new Intent();
x.setAction(CallManager.SIP_INCOMING_CALL_INTENT);
sendBroadcast(x);
Log.d("INTENT SENT", "INTENT SENT INCOMING CALL AFTER PROCESSINVITE");
因此,Activity 将为这些 Intent 注册一个广播接收器,并将根据它收到的最后一个 Intent 切换其状态。
示例代码如下:
SipCallListener = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(SIPEngine.SIP_RINGING_INTENT.equals(action)){
Log.d("cda ", "Got RINGING action SIPENGINE");
ringingSetup();
}
if(CallManager.SIP_INCOMING_CALL_INTENT.equals(action)){
Log.d("cda ", "Got PHONE RINGING action");
incomingCallSetup();
}
}
};
IntentFilter filter = new IntentFilter(CallManager.SIP_INCOMING_CALL_INTENT);
filter.addAction(CallManager.SIP_RINGING_CALL_INTENT);
registerReceiver(SipCallListener, filter);
这可行,但它似乎效率不高,Intent 将在整个系统范围内广播,并且 Intent 必须针对不同的状态触发似乎效率越低,我必须包含的越多以及增加复杂性。
所以我想知道是否有其他更有效和更清洁的方法来做到这一点?
有没有办法让 Intent 只在应用程序内广播?
回调会更好吗?如果是这样,为什么以及以什么方式实施它们?
【问题讨论】:
标签: android user-interface service performance android-intent