I am trying to make my app start when my device reboots, so I created a class called BootReceiver
extending from BroadcastReceiver
with an intent filter registered with the Android.intent.action.BOOT_COMPLETED
constant.
I created an instance of the BootReceiver
class in my Oncreate()...
and registered it with the filter specified above. I also declared the permission for receiving boot completed intents on my manifest file and requested for it programmatically. Even after doing all that, my app does not start when restart my device.
BootReceiver class
[BroadcastReceiver(Label ="BootReceiver", Enabled = true, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Launch our activity
if (intent.Action == "android.intent.action.BOOT_COMPLETED")
{
/*
Intent new_intent = new Intent(context, typeof(MainActivity));
new_intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(new_intent);
*/
Toast.MakeText(context, "trying to start Niskize app", ToastLength.Long).Show();
}
}
}
MainActivity class
[Activity(Label = "@string/app_name", Theme = "@style/AppThemeWhite", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
BootReceiver bootReceiver;
protected override void OnCreate(Bundle savedInstanceState)
{
// Request for boot completed permissions
boot_permissions();
bootReceiver = new BootReceiver();
RegisterReceiver(bootReceiver, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
}
}
How can I implement this correctly with Android Xamarin?