Coding Notes

.Net MAUI Auto Start Android App - Launch on boot- How to open the application automatically after boot?


Platforms/Android/AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application android:allowBackup="true"

               android:icon="@mipmap/appicon"

               android:roundIcon="@mipmap/appicon_round"

               android:supportsRtl="false"></application>

  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

  <uses-permission android:name="android.permission.INTERNET"/>

  <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

  <uses-permission android:name="android.permission.INSTALL_PACKAGES" />

  <uses-permission android:name="android.permission.DELETE_PACKAGES" />

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

  <uses-permission android:name="android.permission.WAKE_LOCK"/>

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

</manifest>

Platforms/Android/BootReceiver.cs


using Android.App;

using Android;

using Android.Content;

using Android.Widget;


[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]


namespace MaRadio.Platforms.Android

{

    [BroadcastReceiver(Name = "com.oskay.maradio.BootReceiver", Exported = true, Enabled = true, DirectBootAware = true)]

    [IntentFilter(new[] {  Intent.ActionReboot,

        Intent.ActionBootCompleted

        , Intent.ActionLockedBootCompleted

        , Intent.ActionMyPackageReplaced

        , Intent.ActionUserInitialize

        , "android.intent.action.QUICKBOOT_POWERON"

        , "com.htc.intent.action.QUICKBOOT_POWERON" }, Categories = new[] { Intent.CategoryDefault })]

    public class BootReceiver : BroadcastReceiver

    {

        public override void OnReceive(Context context, Intent intent)

        {

            if (intent.Action == null || (!intent.Action.Equals(Intent.ActionBootCompleted)

                                          && !intent.Action.Equals(Intent.ActionReboot)

                                          && !intent.Action.Equals(Intent.ActionLockedBootCompleted)

                                          && !intent.Action.Equals(Intent.ActionUserInitialize)

                                          && !intent.Action.Equals("android.intent.action.QUICKBOOT_POWERON")

                                          && !intent.Action.Equals("com.htc.intent.action.QUICKBOOT_POWERON"))) return;

            Toast.MakeText(context, "maRadio Starting...", ToastLength.Long)?.Show();

            var s = new Intent(context, typeof(MainActivity));

            s.AddFlags(ActivityFlags.ResetTaskIfNeeded);

            s.AddFlags(ActivityFlags.ReorderToFront);

            s.AddFlags(ActivityFlags.NewTask);

            context.StartActivity(s);

        }

    }

}

Platforms/Android/MainActivity.cs


using Android.App;

using Android.Content.PM;

using Android.OS;


namespace MaRadio;


[Activity(Theme = "@style/Maui.SplashTheme",

    Enabled = true,

    Exported = true,

    MainLauncher = true,

    LaunchMode = LaunchMode.SingleTask,

    ConfigurationChanges =  ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]

[IntentFilter(new[] { Platform.Intent.ActionAppAction }, Categories = new[] { global::Android.Content.Intent.CategoryLauncher })]

public class MainActivity : MauiAppCompatActivity

{

}


Ref: https://github.com/dotnet/maui/discussions/10414#discussioncomment-6424169 

Ref: https://developer.android.com/reference/android/Manifest.permission




.Net MAUI Keep the Screen on - Prevent from dimming or going blank


Using the KeepScreenOn Property: In MAUI, you can prevent the screen from turning off by using the KeepScreenOn property on a view derived from the Page or ContentView class. For example, you can set the KeepScreenOn property for a ContentPage as follows:

``using Microsoft.Maui.Controls;

public class MyContentPage : ContentPage
{
public MyContentPage()
{
// Set the KeepScreenOn property to true to prevent the screen from turning off
KeepScreenOn = true;
}
}
``

By doing this, when MyContentPage is used, the screen will stay on.

Platform-Specific Customizations: If you need more control and customization, you can make platform-specific customizations. For example, in Android, you can make customizations in the MainActivity.cs file, as shown in the following example:

``using Microsoft.Maui;
using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Application = Microsoft.Maui.Controls.Application;

namespace MyMauiApp
{
public class MainActivity : MauiAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

       // Platform-specific customization to prevent the screen from turning off

        this.On<Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);

        this.On<Android>().KeepScreenOn();


        // Other necessary code...

    }

}


}
``

In this example, we're using the KeepScreenOn() method in the OnCreate method of the MainActivity class to prevent the screen from turning off.

These methods are two common approaches to preventing the screen from turning off in a MAUI application using C# and .NET MAUI. Depending on your requirements, you can choose the method that best suits your application

Ref: https://github.com/dotnet/maui/discussions/10414#discussioncomment-6456786 

Changing .NET MAUI WebView width runtime 

https://dev.to/vhugogarcia/responsive-flyout-in-net-maui-4ll1