如何在Xamarin.Form参考此论坛中使用Broadcast Reciever http://forums.xamarin.com/discussion/7070/how-to-prevent-sms-going-to-inbox
班级
public class SmsReceiver : BroadcastReceiver
{
public static readonly string IntentAction = "android.provider.Telephony.SMS_RECEIVED";
public override void OnReceive(Context context, Intent intent)
{
InvokeAbortBroadcast();
try
{
if (intent.Action != IntentAction) return;
var bundle = intent.Extras;
if (bundle == null) return;
var pdus = bundle.Get("pdus");
var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);
var msgs = new SmsMessage[castedPdus.Length];
var sb = new StringBuilder();
String sender = null;
for (var i = 0; i < msgs.Length; i++)
{
var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);
msgs[i] = SmsMessage.CreateFromPdu(bytes);
if (sender == null) sender = msgs[i].OriginatingAddress;
sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress,
System.Environment.NewLine, msgs[i].MessageBody));
}
if (sender != null && sender.EndsWith("09068100820"))
{
// Process our sms...
// SMS.updateMessageBox("\nFrom: " + msg.getOriginatingAddress() + "\n" +
//"Message: " + msg.getMessageBody() + "\n");
/*((SMS) context).delete();*/
Toast.MakeText(context, "IsOrderedBroadcast :" + IsOrderedBroadcast.ToString() + "\n" + sb.ToString(), ToastLength.Long).Show();
}
else
{
ClearAbortBroadcast();
}
}
catch (Exception ex)
{
Toast.MakeText(context, ex.Message, ToastLength.Long).Show();
}
}
}
如何在Xamarin.Form中实现此类并获取传入的短信,提前致谢并祝您好日子:D
答案 0 :(得分:1)
来自Android 4.4
,您不能对短信进行任何操作,只需在您的应用不是默认短信应用时阅读即可。
如果您的应用是默认短信应用,并且您想阻止发件人或其他任何内容,请将SmsReceiver
放入Android Project
并在Application
课程中注册。
我认为你不需要在Forms Project
中做任何事情。