正如標題所示,我想禁用/隱藏與 NFC 發現相關的所有事件。
我的應用不使用 NFC,我的目標是隱藏自動彈出視窗,例如 Google Pay。
我在這里找到這些執行緒可以幫助我找到解決方法
1:Android 應用只為一個 Activity 啟用 NFC
2:Android 應用只為一個 Activity 啟用 NFC
這是我的主要活動:
[Activity(Label = "Title", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//CrossNFC.Init(this);
var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Mutable);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnResume()
{
base.OnResume();
NfcAdapter nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Mutable);
nfcAdapter.EnableForegroundDispatch(this, pendingIntent, null, null);
//CrossNFC.OnResume();
}
protected override void OnPause()
{
base.OnPause();
NfcAdapter nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
nfcAdapter.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (NfcAdapter.ActionTagDiscovered.Equals(intent.Action))
return;
}
}
但是當我嘗試它(我有兩個用于測驗的 NFC 標簽和 1 個類似于 NFC 卡仿真的應用程式)時,我可以捕獲事件 onNewIntent 但其他兩個甚至沒有顯示。
如果我使用 Plugin.NFC nugget,我可以檢測到我的 3 個標簽中的 2 個。
難道我做錯了什么?
謝謝
uj5u.com熱心網友回復:
首先,我不會EnableForegroundDispatch嘗試捕獲和隱藏 NFC 事件。即使您設定為獲取系統 NFC 服務,當它看到標簽時仍會發??出聲音,然后將其發送給您忽略。
它也不會禁用 Android Beam 功能(如果在較舊的 Android 版本上可用),如果您在拾取標簽之前在 Android 手機上模擬標簽,這可能會被拾取。
我建議使用更新更好的enableReaderModeAPI,因為這會禁用 Android Beam,并且還會禁用系統 NFC 服務聲音。
enableReaderMode的 Java 示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487692.html
標籤:xamarin xamarin.android NFC
