Launch Activity on System/Emulator Startup

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Launch Activity on System/Emulator Startup

Postby plusminus » Thu Jan 03, 2008 2:41 pm

[align=center]Launch Activity on System/Emulator Startup[/align]

What you will learn: You will learn how to launch your Activity when the Emulator/Device gets started (when the Boot-Process has finished).

:?: Problems/Questions: Write it right below...

Difficulty: 2 of 5 :)

What it will look like:
[align=center]Image[/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:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <receiver class=".MyStartupIntentReceiver">
  2.  
  3.             <intent-filter>
  4.  
  5.                  <action android:value="android.intent.action.BOOT_COMPLETED" />
  6.  
  7.                  <category android:value="android.intent.category.HOME" />
  8.  
  9.             </intent-filter>
  10.  
  11.         </receiver>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

that it looks finally like this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="org.anddev.launchonstartup">
  6.  
  7.     <application android:icon="@drawable/icon">
  8.  
  9.         <activity class=".LaunchOnStartup" android:label="@string/app_name">
  10.  
  11.             <intent-filter>
  12.  
  13.                 <action android:value="android.intent.action.MAIN" />
  14.  
  15.                 <category android:value="android.intent.category.LAUNCHER" />
  16.  
  17.             </intent-filter>
  18.  
  19.         </activity>
  20.  
  21.         <receiver class=".MyStartupIntentReceiver">
  22.  
  23.             <intent-filter>
  24.  
  25.                  <action android:value="android.intent.action.BOOT_COMPLETED" />
  26.  
  27.                  <category android:value="android.intent.category.HOME" />
  28.  
  29.             </intent-filter>
  30.  
  31.         </receiver>
  32.  
  33.     </application>
  34.  
  35. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


1.) This is the actual MyStartupIntentReceiver-Class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.launchonstartup;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.content.Intent;
  8.  
  9. import android.content.IntentReceiver;
  10.  
  11.  
  12.  
  13. public class MyStartupIntentReceiver extends IntentReceiver {
  14.  
  15.  
  16.  
  17.         @Override
  18.  
  19.         public void onReceiveIntent(Context context, Intent intent) {
  20.  
  21.                 /* Create intent which will finally start the Main-Activity. */
  22.  
  23.                 Intent myStarterIntent = new Intent(context, LaunchOnStartup.class);
  24.  
  25.                 /* Set the Launch-Flag to the Intent. */
  26.  
  27.                 myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  28.  
  29.                 /* Send the Intent to the OS. */
  30.  
  31.                 context.startActivity(myStarterIntent);
  32.  
  33.         }
  34.  
  35. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

2.) This is the output, when you start the emulator:
[align=center]Image

Thats it :)[/align]

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby cabernet1976 » Fri Jan 04, 2008 3:41 am

Thanks plusminus, it is very useful.

And I find there are 7 broadcast actions in google document: http://code.google.com/android/referenc ... ntent.html
In there search "Standard Broadcast Actions"
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby puyopuy » Sat Feb 09, 2008 6:12 am

Hi plusminus,

I'm modifying this tutorial to check user's Preferences after the boot process has finished. If the user set showLaunchOnStartup to True in Preferences, it will show LaunchOnStartup Activity, otherwise it won't show that screen. Any idea how can I do it?

Thank you very much in advance.
puyopuy
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Postby plusminus » Sat Feb 09, 2008 11:32 am

Hello puyopuy,

you could look the setting up in a file or a database:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         public void onReceiveIntent(Context context, Intent intent) {
  4.  
  5.                 context.openDatabase(file, factory);
  6.  
  7.                 context.openFileInput(name);
  8.  
  9.                 if(setting_found){
  10.  
  11.                         /* Create intent which will finally start the Main-Activity. */
  12.  
  13.                         Intent myStarterIntent = new Intent(context, LaunchOnStartup.class);
  14.  
  15.                         /* Set the Launch-Flag to the Intent. */
  16.  
  17.                         myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  18.  
  19.                         /* Send the Intent to the OS. */
  20.  
  21.                         context.startActivity(myStarterIntent);
  22.  
  23.                 }
  24.  
  25.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby puyopuy » Sat Feb 09, 2008 12:56 pm

hello plusminus,

Thank you for quick reply. I don't know why after I changed the codes like this:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         @Override
  3.  
  4.         public void onReceiveIntent(Context context, Intent intent) {
  5.  
  6.                 boolean showLaunchOnStartup = false;
  7.  
  8.                
  9.  
  10.         if(showLaunchOnStartup){
  11.  
  12.              /* Create intent which will finally start the Main-Activity. */
  13.  
  14.              Intent myStarterIntent = new Intent(context, LaunchOnStartup.class);
  15.  
  16.              /* Set the Launch-Flag to the Intent. */
  17.  
  18.              myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  19.  
  20.              /* Send the Intent to the OS. */
  21.  
  22.              context.startActivity(myStarterIntent);
  23.  
  24.         }
  25.  
  26.  
  27.  
  28.         }
  29.  
  30.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Hello Word, LaunchOnStartup screen still showing, I expect that it will run at normal startup with out showing any window.

Am I did something wrong?

Thanks
puyopuy
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Postby plusminus » Sat Feb 09, 2008 2:45 pm

Hello puyopuy,

:shock: I definitely think the reason for this weird behavior is, that your application(the intent-receiver) got registered with the system and not really updated with the following builds/pushes.

Try running your app after running the emulator-image once with "-wipe-data" under your projects Run-Configurations in Eclipses. That should fix it.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby puyopuy » Sun Feb 10, 2008 1:15 am

Hello plusminus,

I'm not sure is that what you mean. I put -wipe-data in command line options in Debug Configurations window. But the result still the same. I tried to put two break points one in LaunchOnStartup and the other in MyStartupIntentReceiver. I can see that LaunchOnStartup also called before MyStartupIntentReceiver.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class LaunchOnStartup extends Activity {
  3.  
  4.     /** Called when the activity is first created. */
  5.  
  6.     @Override
  7.  
  8.     public void onCreate(Bundle icicle) {
  9.  
  10.         super.onCreate(icicle);  <span style="font-weight: bold"><=break point</span>
  11.  
  12.         setContentView(R.layout.main);
  13.  
  14.     }
  15.  
  16. }
  17.  
  18.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class MyStartupIntentReceiver extends IntentReceiver {
  3.  
  4.  
  5.  
  6.         @Override
  7.  
  8.         public void onReceiveIntent(Context context, Intent intent) {
  9.  
  10.                 boolean showLaunchOnStartup = false;  <span style="font-weight: bold"><=break point</span>
  11.  
  12.                
  13.  
  14.         if(showLaunchOnStartup){
  15.  
  16.              /* Create intent which will finally start the Main-Activity. */
  17.  
  18.              Intent myStarterIntent = new Intent(context, LaunchOnStartup.class);
  19.  
  20.              /* Set the Launch-Flag to the Intent. */
  21.  
  22.              myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  23.  
  24.              /* Send the Intent to the OS. */
  25.  
  26.              context.startActivity(myStarterIntent);
  27.  
  28.         }
  29.  
  30.  
  31.  
  32.         }
  33.  
  34.  
  35.  
  36. }
  37.  
  38.  
  39.  
  40.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Thanks again for your help
:lol:
Attachments
options.JPG
Debug options settings
options.JPG (17.27 KiB) Viewed 43049 times
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Postby plusminus » Sun Feb 10, 2008 1:12 pm

Hello puyopuy,

yes, that was what I was talking about. But even more weird :shock: that it still appears. Perhaps use the Logger (Log.d(..,..)) to ensure that the emulator is running the code you posted :?

What happens if you delete everything within onReceiveIntent :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby puyopuy » Mon Feb 11, 2008 11:56 am

Hello plusminus,

I tested this application in a new VM image, without any Eclipse or Android SDK install. The result still the same. Can you duplicate this problem in your enviroment? Attached my source code that I used in my environment.

Thanks
Puyopuy
Attachments
LaunchOnStartup.zip
My LaunchOnStartup source code
(33.11 KiB) Downloaded 754 times
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Postby plusminus » Mon Feb 11, 2008 3:19 pm

Hello

I altered the code to give some debug-output:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           boolean showLaunchOnStartup = false;  
  2.  
  3.           Log.d("DEBUGTAG", "Outside: showLaunchOnStartup=" + showLaunchOnStartup);
  4.  
  5.         if(showLaunchOnStartup){
  6.  
  7.                 Log.d("DEBUGTAG", "Inside: showLaunchOnStartup=" + showLaunchOnStartup);
  8.  
  9.              /* Create intent which will finally start the Main-Activity. */
  10.  
  11.              Intent myStarterIntent = new Intent(context, LaunchOnStartup.class);
  12.  
  13.              /* Set the Launch-Flag to the Intent. */
  14.  
  15.              myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  16.  
  17.              /* Send the Intent to the OS. */
  18.  
  19.              context.startActivity(myStarterIntent);
  20.  
  21.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


And it only shows :darrow: and therefore everything is as it should be:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. D/DEBUGTAG(590): Outside: showLaunchOnStartup=false
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby puyopuy » Tue Feb 12, 2008 8:44 am

Hi Plusminus,

The log only shown "Outside: showLaunchOnStartup=false", the problem is the main Activity(LaunchOnStartup) already created before onReceiveIntent. As I told you in Sun Feb 10, LaunchOnStartup's onCreate always called before onReceiveIntent. Just want to know do you have the same problem in your environment. :lol:

Thank you very much.
puyopuy
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Postby plusminus » Tue Feb 12, 2008 9:43 am

Hello puyopuy,

are you hitting the :run: -Button in Eclipse :?:

That happens because of :darrow: , of couse.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <activity class=".LaunchOnStartup" android:label="@string/app_name">
  2.  
  3.             <intent-filter>
  4.  
  5.                 <action android:value="android.intent.action.MAIN" />
  6.  
  7.                 <category android:value="android.intent.category.LAUNCHER" />
  8.  
  9.             </intent-filter>
  10.  
  11.         </activity>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby puyopuy » Tue Feb 12, 2008 12:06 pm

Hi Plusminus,

I tried Run and Debug buttons they all the same. May I ask which button should I use?

Regards,
Puyopuy
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Postby plusminus » Tue Feb 12, 2008 1:45 pm

Hello puyopuy,

you implemented a IntentReceiver that reacts to the BOOT_COMPLETED_ACTION, which will cause your application to be started when your Emulator is started. The reason for that is the following part in AndroidManifest.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <receiver class=".MyStartupIntentReceiver">
  2.  
  3.             <intent-filter>
  4.  
  5.                  <action android:value="android.intent.action.BOOT_COMPLETED" />
  6.  
  7.                  <category android:value="android.intent.category.HOME" />
  8.  
  9.             </intent-filter>
  10.  
  11.         </receiver>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


When you hit :run: this will cause your application to start directly, because of:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <activity class=".LaunchOnStartup" android:label="@string/app_name">
  2.  
  3.             <intent-filter>
  4.  
  5.                 <action android:value="android.intent.action.MAIN" />
  6.  
  7.                 <category android:value="android.intent.category.LAUNCHER" />
  8.  
  9.             </intent-filter>
  10.  
  11.         </activity>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Hope things are clearer now.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby puyopuy » Thu Feb 14, 2008 2:19 am

Thanks for your detailed explanation, I will test it again this week. :lol:
puyopuy
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Feb 03, 2008 4:46 am

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests