我需要为我的应用程序创建注册。我创建了短信发送服务器,所以我可以:
EditText
元素。 但现在我需要将此令牌自动从SMS放入应用程序的文本框中。
EditText
。 我尝试使用 BroadcastReceiver ,它适用于短信。我可以像下面这样显示文字:
using Android.App;
using Android.Content;
using Android.Telephony;
using Android.Widget;
namespace ReceiveMsg
{
[BroadcastReceiver]
[IntentFilter(
new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority =(int)IntentFilterPriority.HighPriority)]
public class SmsReceiver : BroadcastReceiver
{
public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
protected string message, address = "";
public override void OnReceive(Context context, Intent intent)
{
if(intent.HasExtra("pdus"))
{
var smsArray = (Java.Lang.Object[])intent.Extras.Get("pdus");
foreach(var item in smsArray)
{
var sms = SmsMessage.CreateFromPdu((byte[])item);
address = sms.OriginatingAddress;
message = sms.MessageBody;
Toast.MakeText(context, "Number: " + address + "Message: " + message, ToastLength.Short).Show();
}
}
}
}
}
但是如何将来自SMS的令牌自动放入令牌位置?
我首先需要向用户显示权限管理窗口以进行确认,对吧?
所以我在AndroidManifest中添加了这个:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
用户允许使用此短信 从SMS解析的令牌将存储在变量中,并将显示在文本框内。
viewmodel.cs中有一个令牌变量。
所以我在这里添加public interface SmsListener
:
private string _token;
private bool _isSuccessed, _isInvalid;
private readonly IMvxNavigationService _navigationService;
public bool IsSucceeded
{
get
{
return _isSuccessed;
}
set
{
_isSuccessed = value;
RaisePropertyChanged(() => IsSucceeded);
}
}
public bool IsInvalid
{
get
{
return _isInvalid;
}
set
{
_isInvalid = value;
RaisePropertyChanged(() => IsInvalid);
}
}
public string Token
{
get
{
return _token;
}
set
{
_token = value;
RaisePropertyChanged(() => Token);
}
}
public VerificationViewModel(IMvxNavigationService navigationService)
{
_navigationService = navigationService;
VerifyCommand = new MvxAsyncCommand(async () => {
IsSucceeded = await RegistrationService.VerifyToken(Token);
IsInvalid = !IsSucceeded;
});
}
public IMvxAsyncCommand VerifyCommand { get; private set; }
public interface SmsListener
{
void MessageReceived(string messageText);
}
但我仍然不了解如何在SmsReceiver中使用监听器
我该如何总结一下?按顺序,用户将能够允许使用此SMS并信任自动化过程吗?
答案 0 :(得分:1)
我首先需要向用户显示确认权限管理器窗口,对吗?
是的,如果目标版本> 6.0,则需要Request App Permissions。
如何将来自SMS的令牌自动放入令牌位置?
您可以使用接口来实现它。请参阅this article。
1)您需要定义一个界面,例如SmsListener
:
public interface SmsListener {
public void messageReceived(string messageText);
}
2)在SmsReceiver
中,您需要在获得MessageBody
时调用它;
3)在您的Activity中实现包含EditText
的界面,并在界面的messageReceived
方法中更新EditText
的文字。< / p>
不要忘记使用bindListener
方法或其他方法来实例化您的界面。
SmsReceiver
:
namespace ReceiveMsg
{
[BroadcastReceiver]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority =(int) IntentFilterPriority.HighPriority)]
public class SmsReceiver : BroadcastReceiver
{
public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
protected string message, address = "";
private OnReceiveSMSListener mOnReceiveSMSListener;//add interface
public override void OnReceive(Context context, Intent intent)
{
if (intent.HasExtra("pdus"))
{
var smsArray = (Java.Lang.Object[])intent.Extras.Get("pdus");
foreach (var item in smsArray)
{
var sms = SmsMessage.CreateFromPdu((byte[])item);
address = sms.OriginatingAddress;
message = sms.MessageBody;
Toast.MakeText(context, "Number: " + address + "Message: " + message, ToastLength.Short).Show();
// call the interface if you get the message
mOnReceiveSMSListener.onReceived(message);
}
}
}
// interface
public interface OnReceiveSMSListener
{
void onReceived(String message);
}
//used in activity
public void setOnReceiveSMSListener(OnReceiveSMSListener onReceiveSMSListener)
{
mOnReceiveSMSListener = onReceiveSMSListener;
}
}
}
Activity
:
public class MainActivity : Activity,SmsReceiver.OnReceiveSMSListener
{
private SmsReceiver mSMSBroadcastReceiver = new SmsReceiver();
private EditText mEt_code;
public void onReceived(string message)
{
mEt_code.Text=message + "";
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mEt_code = (EditText)FindViewById(Resource.Id.et_code);
mSMSBroadcastReceiver.setOnReceiveSMSListener(this);
}
}
Manifest
:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>