通过BroadcastReciver接收来电时,将应用程序从后台运行到前台

ask*_*nna 5 xamarin.android xamarin.forms

我正在使用Xamarin开发一个用于SIP呼叫的跨平台应用程序.我有传入和传出呼叫工作.

虽然,当应用程序在后台运行时,我遇到接收呼叫的问题.

当收到电话时,我试图将应用程序带到前面.我使用的代码如下:

在我的MainActivity中

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

在我的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);
}
Run Code Online (Sandbox Code Playgroud)

我试着用不同的ActivityFlagsNewTask,SingleTop,ReorderToFront,ReceiverForeground,FromBackground,BroughtToFront.但是,注意到会将我的应用程序带到前台.

我还能从这里做什么?

我试过这个链接.虽然没有帮助.

Che*_*ron 2

var intent = new Intent(context, typeof (MainActivity));
intent.AddFlags(ActivityFlags.NewTask);
context.StartActivity(intent);
Run Code Online (Sandbox Code Playgroud)

应该可以正常启动您的应用程序。

你确定你的BroadcastReceiver名字被调用了吗?