Hi I am facing a very simple problem but I am not exactly sure why?
I am trying to call directly from xamarin forms app, without showing dailer screen, user will see list of its contacts in a screen click a button to call, and it will make a direct call without showing dialer screen.
to achieve this I have used DependencyServic and I have used this as my base https://www.c-sharpcorner.com/UploadFile/e4bad6/code-to-start-call-in-xamarin-forms/
the only difference is this is PCL and I am using shared library
Where I am getting Problem?
My Interface
public interface IPhoneCall
{
void MakeQuickCall(string PhoneNumber);
}
My call to Dependency Service
private void makeCall_Clicked(object sender, EventArgs e)
{
try
{
DependencyService.Get<IPhoneCall>().MakeQuickCall("+xxxxxxxxx");
} catch(Exception ex)
{
DisplayAlert("Alert!!!", ex.Message, "ok");
}
}
My Dependency service call for Android:
[assembly: Dependency(typeof(PhoneCall_Droid))]
namespace MakeCall.Droid
{
public class PhoneCall_Droid : IPhoneCall
{
public void MakeQuickCall(string PhoneNumber)
{
try
{
var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
var intent = new Intent(Intent.ActionCall, uri);
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (Exception ex)
{
new AlertDialog.Builder(Android.App.Application.Context).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
}
}
This alert is returning an exception Unable to add window - token null is not valid; is your activity running?
I have looked different solutions like this
Intent ActionCall is not making phone call in xamarin forms
and this https://forums.xamarin.com/discussion/129166/unable-to-add-window-token-null-is-not-for-an-application-alertbuilder
but I am still not able to sort this, out,
also I tried this in my Main activity
internal static MainActivity Instance { get; private set; }
and then added this line in OnCreate method
Instance = this;
and changed my Android dependency service class method to this
public void MakeQuickCall(string PhoneNumber)
{
var context = MainActivity.Instance;
try
{
new AlertDialog.Builder(context ).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber)).ToString())
.SetTitle("Android Exception")
.Show();
Intent intent = new Intent(Intent.ActionCall, Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber)));
context.StartActivity(intent);
}
catch (Exception ex)
{
new AlertDialog.Builder(Android.App.Application.Context).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
and by doing this alert is showing the dailed phone number, but it is not calling and for call part is still showing same of unable to add window ..,
1条答案
按热度按时间yshpjwxd1#
我可以在我身边重现你的问题,如果你要求Android 6.0后的runtime permissions,你会解决你的问题。
首先,您可以在MainActiviy中定义一个静态变量:
然后在调用前请求权限: