如何仅在Xamarin中检测紧急呼叫?

时间:2016-02-22 18:14:25

标签: xamarin call

有没有办法检测手机在通话时是否只能拨打紧急电话(使用Xamarin)?

2 个答案:

答案 0 :(得分:0)

这是特定于平台的东西。难以理解我不知道任何插件,所以你必须在Xamarin中使用本机API。

在Android上,它是TelephonyManager,如下所示:https://stackoverflow.com/a/14355706/1489968它是Java,但可以很容易地转换为C#:

public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);

        var telMng = (TelephonyManager) GetSystemService(TelephonyService);
        var myPhoneStateListener = new MyPhoneStateListener();
        myPhoneStateListener.ServiceStateChanged += (s, e) => Console.WriteLine("State: {0}", e);
        telMng.Listen(myPhoneStateListener, PhoneStateListenerFlags.ServiceState);
    }
}

public class MyPhoneStateListener : PhoneStateListener
{
    public event EventHandler<ServiceState> ServiceStateChanged;
    public override void OnServiceStateChanged(ServiceState serviceState)
    {
        base.OnServiceStateChanged(serviceState);
        ServiceStateChanged?.Invoke(this, serviceState);
    }
}

答案 1 :(得分:0)

在iOS上:不确定该信息是否可用,从未通过SDK公开过... ...(可能在此问题中添加iOS标记,或者询问iOS only问题,答案可能在ObjC / Swift但你可以翻译它)

在Android上:您要查找的信息包含在手机的ServiceState中:

var callState = new ServiceState ();
switch (callState.State) {
case PhoneState.InService:
    {
        var uri = Android.Net.Uri.Parse ("tel:555-2368"); // Jim Rockford's number ;-)
        var intent = new Intent (Intent.ActionDial, uri);
        StartActivity (intent);
        break;
    }
case PhoneState.EmergencyOnly:
    {
        Toast.MakeText (this, "Emergency Calls Only", ToastLength.Long).Show();
        break;
    }
case PhoneState.OutOfService:
    {
        Toast.MakeText (this, "Out of Service", ToastLength.Long).Show();
        break;
    }
case PhoneState.PowerOff:
    {
        Toast.MakeText (this, "Cell/Modem Power Off", ToastLength.Long).Show();
        break;
    }
default:
    {
        Toast.MakeText (this, "Should never be shown on a real device", ToastLength.Long).Show();
        break;
    }
}

参考:http://developer.android.com/reference/android/telephony/ServiceState.html

要在模拟器上进行测试,您可以通过denied shell将状态设置为adb

voice denied
data denied

参考:https://developer.android.com/tools/devices/emulator.html