我正在使用xamarin android构建应用程序,正在实现一项功能,我想以编程方式接听电话;我有一个需要使用Intent
的想法,但是如何使用?那就是我不知道的。
有人可以建议我吗?
最佳答案
您可以编写一个广播接收器来管理您的来电:
[BroadcastReceiver]
[IntentFilter (new [] {"android.intent.action.PHONE_STATE"})]
public class IncomingPhoneCallDetector : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Extras != null)
{
string state = intent.GetStringExtra(TelephonyManager.ExtraState);
if (state == TelephonyManager.ExtraStateRinging)
{
string telephone = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
if (!string.IsNullOrEmpty (telephone))
{
Toast.MakeText (context, "Incoming call from " + telephone + ".", ToastLength.Short).Show ();
}
else
{
Toast.MakeText (context, "Incoming call from unknown number.", ToastLength.Short).Show ();
}
Intent buttonDown = new Intent(Intent.ActionMediaButton);
buttonDown.PutExtra(Intent.ExtraKeyEvent, new KeyEvent(KeyEventActions.Up, Keycode.Headsethook));
context.SendOrderedBroadcast (buttonDown, "android.permission.CALL_PRIVILEGED");
}
else if (state == TelephonyManager.ExtraStateOffhook)
{
Toast.MakeText(context, "Incoming call answered.", ToastLength.Short).Show();
}
else if (state == TelephonyManager.ExtraStateIdle)
{
Toast.MakeText(context, "Incoming call ended.", ToastLength.Short).Show();
}
}
}
}
干杯!!