Android Xamarin:如何处理行动和回答活动
我试图处理来电取消或接听,但我做不到。 我尝试了以下代码:Android Xamarin:如何处理行动和回答活动,android,android-intent,xamarin,Android,Android Intent,Xamarin,我试图处理来电取消或接听,但我做不到。 我尝试了以下代码: [BroadcastReceiver(Label = "Blocking Calls")] [IntentFilter(new string[] { "android.intent.action.PHONE_STATE" })] public class MyReceiver : Android.Content.BroadcastReceiver { private const string IntentAction_Block
[BroadcastReceiver(Label = "Blocking Calls")]
[IntentFilter(new string[] { "android.intent.action.PHONE_STATE" })]
public class MyReceiver : Android.Content.BroadcastReceiver
{
private const string IntentAction_BlockingCalls = "android.intent.action.PHONE_STATE";
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == IntentAction_BlockingCalls)
{
// ensure there is information
if (intent.Extras != null)
{
// get the incoming call state
string state = intent.GetStringExtra(TelephonyManager.ExtraState);
// check the current state
if (state == TelephonyManager.ExtraStateRinging)
{
// read the incoming call telephone number
string telephoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
Intent buttonDown = new Intent(Intent.ActionMediaButton);
buttonDown.PutExtra(Intent.ActionView, new KeyEvent(KeyEventActions.Down, Keycode.Headsethook));
context.SendBroadcast(buttonDown);
Toast.MakeText(context, telephoneNumber, ToastLength.Short).Show(); // Flag 4
// check the read telephone
if (string.IsNullOrEmpty(telephoneNumber))
telephoneNumber = string.Empty;
}
else if (state == TelephonyManager.ExtraStateOffhook)
{
// Toast.MakeText(context, "The call is answered", ToastLength.Short).Show(); // Flag 5
// incoming call answer
}
else if (state == TelephonyManager.ExtraStateIdle)
{
// Toast.MakeText(context, "The call have ended", ToastLength.Short).Show(); // Flag 6
// incoming call end
}
}
}
}
}
我成功获取了传入的电话号码,但无法接听或取消。因此,我尝试使用下面的操作(ActionAnswer
)
正如android开发者网站的屏幕截图所示
输出和输入都是零!!!。
我该如何处理这种行为?或者有没有其他方法可以用来取消来电?
谢谢你的建议。
这里是android开发者的链接。
最后,我找到了解决方案,而且效果很好。 这就是我们需要的部分
// check the current state
if (state == TelephonyManager.ExtraStateRinging)
{
// read the incoming call telephone number
string telephoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
// You can use Endswith, Equals, or any other available methods
//if (telephoneNumber.EndsWith("604"))
if (telephoneNumber.Equals("01282790604"))
{
var manager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);
IntPtr TelephonyManager_getITelephony = JNIEnv.GetMethodID(
manager.Class.Handle,
"getITelephony",
"()Lcom/android/internal/telephony/ITelephony;");
IntPtr telephony = JNIEnv.CallObjectMethod(manager.Handle, TelephonyManager_getITelephony);
IntPtr ITelephony_class = JNIEnv.GetObjectClass(telephony);
IntPtr ITelephony_endCall = JNIEnv.GetMethodID(
ITelephony_class,
"endCall",
"()Z");
JNIEnv.CallBooleanMethod(telephony, ITelephony_endCall);
JNIEnv.DeleteLocalRef(telephony);
JNIEnv.DeleteLocalRef(ITelephony_class);
Toast.MakeText(context, telephoneNumber + "Is Blocked", ToastLength.Long).Show();
}
你想干什么?它应该给人们提供更多的背景,你想做什么?用于处理传入呼叫的操作。您是否尝试通过代码调用intent活动?好的,我修改了问题的主体。我认为您的代码在android 4.0上运行,但对于android 5.0,您不能这样做,请遵循以下内容。我使用的是android 4.4,不幸的是,这对我没什么帮助。这个答案是关于android使用Java的,在这个链接中还有另一个链接,他们也谈到Java,我现在尝试在Xamarin中使用C#使用它们,但失败了,我在一些函数中遇到了一些错误。
// check the current state
if (state == TelephonyManager.ExtraStateRinging)
{
// read the incoming call telephone number
string telephoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
// You can use Endswith, Equals, or any other available methods
//if (telephoneNumber.EndsWith("604"))
if (telephoneNumber.Equals("01282790604"))
{
var manager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);
IntPtr TelephonyManager_getITelephony = JNIEnv.GetMethodID(
manager.Class.Handle,
"getITelephony",
"()Lcom/android/internal/telephony/ITelephony;");
IntPtr telephony = JNIEnv.CallObjectMethod(manager.Handle, TelephonyManager_getITelephony);
IntPtr ITelephony_class = JNIEnv.GetObjectClass(telephony);
IntPtr ITelephony_endCall = JNIEnv.GetMethodID(
ITelephony_class,
"endCall",
"()Z");
JNIEnv.CallBooleanMethod(telephony, ITelephony_endCall);
JNIEnv.DeleteLocalRef(telephony);
JNIEnv.DeleteLocalRef(ITelephony_class);
Toast.MakeText(context, telephoneNumber + "Is Blocked", ToastLength.Long).Show();
}