Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring application from background to foreground while receiving incoming call via BroadcastReceiver

I am developing an cross platform application using Xamarin, for SIP calling. I have incoming and outgoing calls working.

Although, I have a problem with receiving call in when the App is running in the background.

I have tried to bring application to front, when a call is received. The code I have used follows:

In my MainActivity

private void registerReceiver()
{
    IncomingCallReceiver callReceiver = new IncomingCallReceiver();
    IntentFilter sipIntentFilter = new IntentFilter();
    sipIntentFilter.AddAction("com.NelsonApp.INCOMING_CALL"); 
    this.RegisterReceiver(callReceiver, sipIntentFilter);
}

and in my BroadcastReceiver

public override void OnReceive(Context context, Intent intent)
{  
    DialerCallListener listener = new DialerCallListener(); 
    SIPRegistration.call = SIPRegistration.sipManager.TakeAudioCall(intent, listener);

    string str = SIPRegistration.call.PeerProfile.UriString;
    char [] strArray = {':','@'};
    var value = str.Split(strArray)[1];

    Intent newIntent = new Intent(context, typeof(MainActivity));

    newIntent.AddFlags(ActivityFlags.FromBackground);
    newIntent.AddCategory(Intent.CategoryLauncher);
    context.StartActivity(newIntent);

    PlaySound myActivity = new PlaySound();
    myActivity.PlayRingtone(context);

    MainActivity.isIncomingCall = true;
    MessagingCenter.Send(string.Empty, "IncomingCall", value);
}

I tried with different ActivityFlags like NewTask, SingleTop, ReorderToFront, ReceiverForeground, FromBackground, BroughtToFront. However, noting will bring my app to the foreground.

What else can I do from here?

I have tried following this Link. Although it didn't help.

like image 809
askitanna Avatar asked Nov 01 '22 00:11

askitanna


1 Answers

var intent = new Intent(context, typeof (MainActivity));
intent.AddFlags(ActivityFlags.NewTask);
context.StartActivity(intent);

Should launch your App just fine.

Are you sure your BroadcastReceiver is called?

like image 75
Cheesebaron Avatar answered Nov 11 '22 21:11

Cheesebaron