What you will learn: You will learn how to launch your Activity when the Emulator/Device gets started (when the Boot-Process has finished).
Difficulty: 2 of 5
What it will look like:
[align=center]
[/align]
Description:
0.) We will create a simple IntentReceiver that will receive the android.content.Intent.BOOT_COMPLETED_ACTION ( "android.intent.action.BOOT_COMPLETED" ).
We will have to add an Intent-Filter to a Receiver-Tag in the AndroidManifest.xml which will cause a MyStartupIntentReceiver we define in step 1.) to be started. This IntentReceiver will then launch the acutal Main-Activity.
So we will add the following code to the AndroidManifest.xml:
Using xml Syntax Highlighting
- <receiver class=".MyStartupIntentReceiver">
- <intent-filter>
- <action android:value="android.intent.action.BOOT_COMPLETED" />
- <category android:value="android.intent.category.HOME" />
- </intent-filter>
- </receiver>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
that it looks finally like this:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.anddev.launchonstartup">
- <application android:icon="@drawable/icon">
- <activity class=".LaunchOnStartup" android:label="@string/app_name">
- <intent-filter>
- <action android:value="android.intent.action.MAIN" />
- <category android:value="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver class=".MyStartupIntentReceiver">
- <intent-filter>
- <action android:value="android.intent.action.BOOT_COMPLETED" />
- <category android:value="android.intent.category.HOME" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
1.) This is the actual MyStartupIntentReceiver-Class:
Using java Syntax Highlighting
- package org.anddev.launchonstartup;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentReceiver;
- public class MyStartupIntentReceiver extends IntentReceiver {
- @Override
- public void onReceiveIntent(Context context, Intent intent) {
- /* Create intent which will finally start the Main-Activity. */
- Intent myStarterIntent = new Intent(context, LaunchOnStartup.class);
- /* Set the Launch-Flag to the Intent. */
- myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
- /* Send the Intent to the OS. */
- context.startActivity(myStarterIntent);
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
2.) This is the output, when you start the emulator:
[align=center]
Thats it
[/align]
Regards,
plusminus






-Button in Eclipse 