我正在开发xamarin跨平台应用程序。 我需要在登录确认页面上通过SMS自动获得激活码。如果我在MainActivity中实例化SmsReceiver,则此answer可以使用,但是如何仅在登录确认页面上实例化SmsReceiver类呢? SMS激活仅在一页上使用一次,我不希望它在我的应用程序的生命期内一直处于激活状态。这将是浪费。 我希望我的应用仅在登录确认页面处于活动状态时才读取传入消息。我该怎么办?
答案 0 :(得分:0)
如果您不希望您的<div class="x">
<div class="y">PC:</div>lkj sdflkj sdflkj sdflkj sdlfkj lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf lkj sdflkj sdflkj sdflkj sdlfkj lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf lkj sdflkj sdflkj sdflkj sdlfkj
lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf </div>
<div class="x">
<div class="y">MAC:</div>lkj sdflkj sdflkj sdflkj sdlfkj lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf lkj sdflkj sdflkj sdflkj sdlfkj lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf lkj sdflkj sdflkj sdflkj sdlfkj
lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf lkj sdflkj sdflkj sdflkj sdlfkj lsdkfj lksdjf lkjsd flkj sdflkj sdlfkj sdlfkj sdlfkj sldkfj lsdkjf lskdjf </div>
总是被“监听”,您只需要在声明中将其标记为BroadcastReceiver
,这将防止BrodcastReceiver在清单中静态注册。 .xml。
Enabled=false
稍后在您要收听的活动上,您需要致电
[BroadcastReceiver(Enabled = false, Label = "SMS Receiver")]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED", Intent.CategoryDefault })]
public class SmsReceiver : BroadcastReceiver
{
// Code omitted for simplicity
....
}
传递RegisterReceiver()
和BroadcastReceiver
的实例。
类似:
IntentFilter
如您所见,当我不再需要/不想再听时,我也在注销。
这是通过以下方法完成的:
[Activity(Label = "MainActivity", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity: Activity
{
private SmsReceiver _smsReceiver;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
_smsReceiver = new SmsReceiver()
}
protected override OnResume()
{
base.OnResume();
RegisterReceiver(_smsReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
protected override OnPause()
{
UnregisterReceiver(_smsReceiver);
base.OnPause();
}
}
希望这会有所帮助。