Android 检测呼叫结束并以xamarin表单生成弹出消息
伙计们,我已经在incomingCallReceiver.droid中创建了一个工作代码,它可以检测通话结束并在中显示消息 但是,我的控制台无法显示弹出窗口,因为使用AlertDialog、DisplayAlert、MessageBox.show似乎都不起作用 所以我想知道是否有办法解决这个问题:-Android 检测呼叫结束并以xamarin表单生成弹出消息,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,伙计们,我已经在incomingCallReceiver.droid中创建了一个工作代码,它可以检测通话结束并在中显示消息 但是,我的控制台无法显示弹出窗口,因为使用AlertDialog、DisplayAlert、MessageBox.show似乎都不起作用 所以我想知道是否有办法解决这个问题:- public override void OnReceive(Context context, Intent intent) { try {
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();
});
谢谢你的回复,但我试过了,它不起作用。是否需要在MainActivity.cs文件的其他位置创建引用?如果state.equalTelephonyManager.ExtraStateIdle{Console.WriteLinecall已结束;`Android.App.AlertDialog.Builder dialog=新建AlertDialog.Buildercontext;AlertDialog alert=dialog.Create;alert.SetTitletle;alert.SetMessageSimple alert;alert.SetButtonOK,c,ev=>{context.RunOnUiThread=>{alert.Show;};}}}}}`context不包含RunOnUiThread的定义,并且找不到接受context类型的第一个参数的扩展方法RunOnUiThread,因为您需要活动实例,无论如何我已经为您完成了查看