Start Xamarin application on Android Boot or Start up

Doing development  in Xamarin Studio, I think about how we can start Xamarin application on Android Boot or Start up, after doing a big research on that i find something interesting so i am posting this.

your Android Manifest should be like this

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android” android:versionCode=”1″ android:versionName=”1.0″ package=”webopenapp.webopenapp”>
<uses-sdk />
<application android:label=”webopenapp”>
</application>

<uses-sdk android:minSdkVersion=”4″ />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />
</manifest>

Now add a Android class named it ReceiveBoot.cs  and add BroadcastReceiver

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace webopenapp
{
[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted },
Categories = new[] { Android.Content.Intent.CategoryDefault }
)]
public class ReceiveBoot: BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
//Toast.MakeText(context, “Received intent!”, ToastLength.Short).Show();

if ((intent.Action != null) &&
(intent.Action ==
Android.Content.Intent.ActionBootCompleted))
{ // Start the service or activity
//context.ApplicationContext.StartService(new Intent(context, typeof(MainActivity)));

Android.Content.Intent start = new Android.Content.Intent(context, typeof(MainActivity));

// my activity name is MainActivity replace it with yours
start.AddFlags(ActivityFlags.NewTask);
context.ApplicationContext.StartActivity(start);
}
}
}
}

 

start.AddFlags(ActivityFlags.NewTask);

Add this flag so that you are able to run you android application on start up of your android device.

 

Hope that’s what you looking for

Leave a Reply