在Xamarin中获取电话状态 [英] Getting phone state in Xamarin
问题描述
我有以下代码:
public class StateListener : PhoneStateListener
{
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager = (TelephonyManager)GetSystemService(Context.TelephonyService);
telephonyManager.Listen(phoneStateListender, PhoneStateListenerFlags.CallState);
public void onCallStateChanged(CallState state, String incomingNumber)
{
base.OnCallStateChanged(state, incomingNumber);
switch (state)
{
case CallState.Ringing:
break;
case CallState.Offhook:
break;
case CallState.Idle:
break;
}
}
}
这是一个内部类,因为我的基类是"MainActivity:Activity".上面的代码来自Java,因此我尝试将其转换为C#.但是,在这种情况下,我会犯错误:
This is an inner class because my base class is "MainActivity : Activity". The above code was from Java, so I tried to convert it to C#. However, I get erros in this case:
(TelephonyManager)GetSystemService(Context.TelephonyService);
引发错误,指出字段初始值设定项必须为静态且
throws an error that field initializers must be static and
telephonyManager.Listen(phoneStateListender, PhoneStateListenerFlags.CallState);
在这种情况下不可用.我什至不知道上面的代码是否还能工作.如何在Xamarin中实现我可以得到拨出电话的状态?
is not available in this context. I don't even know if the code above would even work. How to achieve in Xamarin that I can get the state of an outgoing call?
推荐答案
您必须将创建代码移到Activity的 OnCreate
方法中.
You have to move the creation code into the OnCreate
method of your Activity.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonyManager = (TelephonyManager)GetSystemService(Context.TelephonyService);
telephonyManager.Listen(phoneStateListener, PhoneStateListenerFlags.CallState);
}
然后您可以创建类:
public class StateListener : PhoneStateListener
{
public override void OnCallStateChanged(CallState state, string incomingNumber)
{
base.OnCallStateChanged(state, incomingNumber);
switch (state)
{
case CallState.Ringing:
break;
case CallState.Offhook:
break;
case CallState.Idle:
break;
}
}
}
如果要在 OnCallStateChanged
之后对活动进行某些操作,则必须通过该活动(例如,在构造函数中):
If you want to do something on you activity after OnCallStateChanged
you have to pass the activity (e.g. in the constructor):
public class StateListener : PhoneStateListener
{
private readonly MainActivity _activity;
public StateListener(MainActivity activity)
{
_activity = activity;
}
public override void OnCallStateChanged(CallState state, string incomingNumber)
{
base.OnCallStateChanged(state, incomingNumber);
_activity.UpdateCallState(state, incomingNumber);
}
}
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
StateListener phoneStateListener = new StateListener(this);
TelephonyManager telephonyManager = (TelephonyManager)GetSystemService(Context.TelephonyService);
telephonyManager.Listen(phoneStateListener, PhoneStateListenerFlags.CallState);
}
public void UpdateCallState(CallState state, string incomingNumber)
{
// numberLabel.Text = ...
}
}
这篇关于在Xamarin中获取电话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!