2026-03-18 18:06:33 +08:00

61 lines
1.9 KiB
C#

using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Nfc;
using Android.Nfc.Tech;
using Android.OS;
using Com.Lvcheng.Lock.Shared.Nfc.Example.Java;
using NFCLockDemoV2;
using NFCLockDemoV2.Platforms.Android;
using NFCLockDemoV2.ViewModels;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
namespace NFCLockAppV2;
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density, Exported = true)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher })]
[IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = "text/plain")]
public class MainActivity : MauiAppCompatActivity
{
private NFCHelper? nfcHelper;
private NFCCallbacks? NFCCallbacks;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
private void InitializeNFC()
{
if (Microsoft.Maui.Controls.Application.Current?.Handler?.MauiContext?.Services != null)
{
NFCCallbacks = Microsoft.Maui.Controls.Application.Current.Handler.MauiContext.Services.GetService<NFCCallbacks>();
}
if (NFCCallbacks != null)
{
nfcHelper = new NFCHelper(this, NFCCallbacks);
}
}
protected override void OnResume()
{
base.OnResume();
// 确保NFC功能在每次恢复时都已初始化
InitializeNFC();
nfcHelper?.OnResume();
}
protected override void OnPause()
{
base.OnPause();
nfcHelper?.OnPause();
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
nfcHelper?.OnNewIntent(intent);
}
}