开发者

Accessing an activity instance from a BroadcastReceiver

开发者 https://www.devze.com 2023-04-05 05:46 出处:网络
Taking the sample code from http://developer.android.com/resources/samples/SipDemo/src/com/example/android/sip/IncomingCallReceiver.html:

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) {开发者_JAVA技巧
            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?


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号