从 BroadcastReceiver 访问活动实例

发布于 2024-12-05 01:54:34 字数 2128 浏览 2 评论 0原文

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();
            }
        }
    }

}

我看到他们能够以某种方式检索正在运行的实例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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-12-12 01:54:34

我现在不在我的笔记本电脑上使用 android studio,所以我面前没有完整的示例项目来检查此接收器在哪个上下文中使用。
据我所知,可以将 ReceiverRestrictedContext 转换为 Activity 的唯一有效情况是当广播接收器在该活动中注册且活动处于活动状态时。

因此,如果您在 WalkieTalkieActivity 内部有类似这样的内容:

    public class WalkieTalkieActivity
    {

    IncomingCallReceiver receiver = new IncomingCallReceiver();
    IntentFilter intentFilter = new IntentFilter(SOME_ACTION);

    @Override
    public void onResume ()
       super.onResume();
       registerReceiver (receiver, intentFilter);
    }

    @Override 
    public void onPause ()
    {
        super.onPause();
        unregisterReceiver(receiver);
    }    
}

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:

    public class WalkieTalkieActivity
    {

    IncomingCallReceiver receiver = new IncomingCallReceiver();
    IntentFilter intentFilter = new IntentFilter(SOME_ACTION);

    @Override
    public void onResume ()
       super.onResume();
       registerReceiver (receiver, intentFilter);
    }

    @Override 
    public void onPause ()
    {
        super.onPause();
        unregisterReceiver(receiver);
    }    
}
九八野马 2024-12-12 01:54:34

您可以使用以下代码

MyActivity mainActivity = (MyActivity) ((BaseApplication) context.getApplicationContext()).activity

在 BaseApplication 中定义变量活动,并在 MyAcitivity 中将该变量设置为活动上下文

BaseApplication.activity  = this

You can use following code

MyActivity mainActivity = (MyActivity) ((BaseApplication) context.getApplicationContext()).activity

Define a variable activity in your BaseApplication and in your MyAcitivity set that variable to activity context

BaseApplication.activity  = this
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文