Sunday, December 23, 2012

On Boot Receiver in MonoDroid.



Sometimes your samples, for the technology you are using don't have a great example for what you need to accomplish.

Here is a working example for a class that uses a BroadcastReceiver to receive the On Boot Completed Event, and then start an Activity.

Get into your solution.  Right click on your Project, select Options. Go to Mono for Android Applications :



This is your class. Feel free to tweak the Intent to your own Activity class that you wish to start.

    [BroadcastReceiver] 
    [IntentFilter(new string[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
    public class BootBroadcastReceiver : BroadcastReceiver {

        public override void OnReceive(Context context, Intent intent) {
            Intent startServiceIntent = new Intent();

            startServiceIntent.SetClass(context, typeof(Login));
            startServiceIntent.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(startServiceIntent);
        }
    }

I thought I needed to modify the AndroidManifest.xml.  Boy, was that wrong.  Just use the Attributes.  They will make your broadcast receiver automatically respond to the event.

A good link to check out. Blue Stacks. Android on PC and Mac.

2 comments: