从 BroadcastReceiver 访问活动实例
package com.example.android.sip;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.*;
import android.util.Log;
/**
* Listens for incoming SIP calls, intercepts and hands them off to WalkieTalkieActivity.
*/
public class IncomingCallReceiver extends BroadcastReceiver {
/**
* 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) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
@Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
} catch (Exception e) {
e.printStackTrace();
}
}
};
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
incomingCall.answerCall(30);
incomingCall.startAudio();
incomingCall.setSpeakerMode(true);
if(incomingCall.isMuted()) {
incomingCall.toggleMute();
}
wtActivity.call = incomingCall;
wtActivity.updateStatus(incomingCall);
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
}
}
}
我看到他们能够以某种方式检索正在运行的实例WalkieTalkieActivity 通过在广播接收器中投射上下文。这怎么可能?这是代替发送意图来访问活动的快捷方式吗?
Taking the sample code from http://developer.android.com/resources/samples/SipDemo/src/com/example/android/sip/IncomingCallReceiver.html:
package com.example.android.sip;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.sip.*;
import android.util.Log;
/**
* Listens for incoming SIP calls, intercepts and hands them off to WalkieTalkieActivity.
*/
public class IncomingCallReceiver extends BroadcastReceiver {
/**
* 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) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
@Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
} catch (Exception e) {
e.printStackTrace();
}
}
};
WalkieTalkieActivity wtActivity = (WalkieTalkieActivity) context;
incomingCall = wtActivity.manager.takeAudioCall(intent, listener);
incomingCall.answerCall(30);
incomingCall.startAudio();
incomingCall.setSpeakerMode(true);
if(incomingCall.isMuted()) {
incomingCall.toggleMute();
}
wtActivity.call = incomingCall;
wtActivity.updateStatus(incomingCall);
} catch (Exception e) {
if (incomingCall != null) {
incomingCall.close();
}
}
}
}
I see that they're somehow able to retrieve the running instance of WalkieTalkieActivity by casting context in the broadcast receiver. How is this possible? Is this a shortcut to accessing the activity in lieu of sending an intent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在不在我的笔记本电脑上使用 android studio,所以我面前没有完整的示例项目来检查此接收器在哪个上下文中使用。
据我所知,可以将 ReceiverRestrictedContext 转换为 Activity 的唯一有效情况是当广播接收器在该活动中注册且活动处于活动状态时。
因此,如果您在 WalkieTalkieActivity 内部有类似这样的内容:
I'm not right now on my laptop with android studio, so I don't have complete sample project in front of me to check in which context is this receiver used.
From what I know, the only valid case when you can cast ReceiverRestrictedContext to Activity is when broadcast receiver is registered in that activity, while activity is alive.
So if you have inside of WalkieTalkieActivity something like this:
您可以使用以下代码
在 BaseApplication 中定义变量活动,并在 MyAcitivity 中将该变量设置为活动上下文
You can use following code
Define a variable activity in your BaseApplication and in your MyAcitivity set that variable to activity context