0

This is my code in MauiProgram.cs but when I change airplane Mode nothing happens and 'OnReceive' is not called, is there any suggestion? this is my first app on Maui and I am not familiar with it In .net Maui android I need when airplane mode change toast a message on app

public static  class MauiProgram
{
    private static MyBroadcastReceiver myreceiver;


#if ANDROID

    private static void MyOnCreate(Activity activity, Bundle bundle)
    {
        myreceiver = new MyBroadcastReceiver();
    }
    private static void MyOnResume(Activity activity)
    {
        activity.RegisterReceiver(myreceiver, new IntentFilter(Intent.ActionAirplaneModeChanged));
    }
    private static void MyOnPause(Activity activity)
    {
        activity.UnregisterReceiver(myreceiver);
    }
#endif
    public static MauiApp CreateMauiApp()
    {


        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureLifecycleEvents(events =>
                    {
#if ANDROID
                        events.AddAndroid(android => android
                            .OnCreate((activity, bundle) => MyOnCreate(activity, bundle))
                            .OnResume((activity) => MyOnResume(activity))
                            .OnPause((activity) => MyOnPause(activity)));
#endif
                    }); ; 
        return builder.Build();
    }


    [BroadcastReceiver(Enabled = true)]
    public class MyBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent i)
        {
            Toast.MakeText(context, "Received broadcast in MyBroadcastReceiver, " +
                            " value received: " + i.GetStringExtra("key"),
                            ToastLength.Long).Show();
        }
    }
}
Navid
  • 609
  • 6
  • 6
  • Based on your code, I did a test. But I couldn't reproduce this problem on my devices. (Google Pixel android 10.0 and Mi 6x android 9). It could pop up toast on this app normally. – Jessie Zhang -MSFT Sep 19 '22 at 06:52

0 Answers0