检测呼叫结束并在 Xamarin 窗体中生成弹出消息



伙计们,我已经在我的incomingCallReceiver.droid中创建了一个工作代码,该代码可以检测呼叫结束并在控制台中
显示消息,但是,我无法使用弹出窗口,因为使用AlertDialog,DisplayAlert,MessageBox.show((似乎都不起作用。

所以我想知道是否有办法解决这个问题:-

public override void OnReceive(Context context, Intent intent)
{
try
{
if (intent != null && intent.Action.Equals("android.intent.action.NEW_OUTGOING_CALL"))
{
//Toast.makeText(context, "Outgoign call", 1000).show();
String number = intent.GetStringExtra(Intent.ExtraPhoneNumber);
}
else
{
//get the phone state
String newPhoneState = intent.HasExtra(TelephonyManager.ExtraState) ? intent.GetStringExtra(TelephonyManager.ExtraState) : null;
Bundle bundle = intent.Extras;
if (newPhoneState != null && newPhoneState.Equals(TelephonyManager.ExtraStateRinging))
{
//read the incoming call number
String phoneNumber = bundle.GetString(TelephonyManager.ExtraIncomingNumber);
//Log.i("PHONE RECEIVER", "Telephone is now ringing " + phoneNumber);
}
else if (newPhoneState != null && newPhoneState.Equals(TelephonyManager.ExtraStateIdle))
{
//Once the call ends, phone will become idle
//Log.i("PHONE RECEIVER", "Telephone is now idle");
Console.WriteLine("call ended");
//DisplayAlert("Alert", "CALL RECIEVED", "OK");
}
else if (newPhoneState != null && newPhoneState.Equals(TelephonyManager.ExtraStateOffhook))
{
//Once you receive call, phone is busy
//Log.i("PHONE RECEIVER", "Telephone is now busy");
}
}
}
catch (Exception e)
{
//Log.i("Telephony receiver", ee.getMessage());
}
}
private void DisplayAlert(string v1, string v2, string v3)
{
throw new NotImplementedException();
}
}

您是否尝试过在 UI 线程上执行此操作?像这样:

var _activityContext = (Activity) context;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(_activityContext);  
AlertDialog alert = dialog.Create();  
alert.SetTitle("Title");  
alert.SetMessage("Simple Alert");  
alert.SetButton("OK", (c, ev) =>  
{  
// Ok button click task  
});  
_activityContext.RunOnUiThread(() => { 
alert.Show(); 
});

最新更新