How do I programmatically "restart" an Android app in.Net maui?

San Sheva Santhanaraj 0 Reputation points
2023-09-25T15:57:33.59+00:00

How do I programmatically "restart" an Android app in.Net maui?

I have been trying to restart the application when the system reboot in .net maui app

AndroidManifest.xml

BootBroadcastReceiver.cs

using global::Android.App;
using global::Android.Content;

[BroadcastReceiver(Enabled = true, DirectBootAware = true,Exported = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent i = new Intent(context, typeof(MainActivity));
i.AddFlags(ActivityFlags.NewTask);
context.StartActivity(i);
}
}
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
BootBroadcastReceiver receiver;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
receiver = new BootBroadcastReceiver();
}
}

I already tried all answers related to this in stackoverflow it is not working.

Environment: visual studio 2022 .NET MAUI version: MAUI 7.0

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,419 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.