andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

Launch Activity on System/Emulator Startup

Goto page 1, 2, 3  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Thu Jan 03, 2008 2:41 pm    Post subject: Launch Activity on System/Emulator Startup Reply with quote

Launch Activity on System/Emulator Startup


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

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

Difficulty: 2 of 5 Smile

What it will look like:


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:
XML:
       <receiver class=".MyStartupIntentReceiver">
            <intent-filter>
                 <action android:value="android.intent.action.BOOT_COMPLETED" />
                 <category android:value="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

that it looks finally like this:
XML:
<?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>


1.) This is the actual MyStartupIntentReceiver-Class:
Java:
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);
     }
}

2.) This is the output, when you start the emulator:


Thats it Smile


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
cabernet1976
Senior Developer
Senior Developer


Joined: 16 Nov 2007
Posts: 147
Location: China

PostPosted: Fri Jan 04, 2008 3:41 am    Post subject: Reply with quote

Thanks plusminus, it is very useful.

And I find there are 7 broadcast actions in google document: http://code.google.com/android/reference/android/content/Intent.html
In there search "Standard Broadcast Actions"

_________________
EveryAlbum - http://www.anddev.org/viewtopic.php?p=7117#7117
Back to top
View user's profile Send private message Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Sat Feb 09, 2008 6:12 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Sat Feb 09, 2008 11:32 am    Post subject: Reply with quote

Hello puyopuy,

you could look the setting up in a file or a database:
Java:
     @Override
     public void onReceiveIntent(Context context, Intent intent) {
          context.openDatabase(file, factory);
          context.openFileInput(name);
          if(setting_found){
               /* 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);
          }
     }


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Sat Feb 09, 2008 12:56 pm    Post subject: Reply with quote

hello plusminus,

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


Java:

     @Override
     public void onReceiveIntent(Context context, Intent intent) {
          boolean showLaunchOnStartup = false;
          
        if(showLaunchOnStartup){
             /* 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);
        }

     }


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
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Sat Feb 09, 2008 2:45 pm    Post subject: Reply with quote

Hello puyopuy,

Shocked 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

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Sun Feb 10, 2008 1:15 am    Post subject: Reply with quote

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.

Java:

public class LaunchOnStartup extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);  [b]<=break point[/b]
        setContentView(R.layout.main);
    }
}


Java:

public class MyStartupIntentReceiver extends IntentReceiver {

     @Override
     public void onReceiveIntent(Context context, Intent intent) {
          boolean showLaunchOnStartup = false;  [b]<=break point[/b]
          
        if(showLaunchOnStartup){
             /* 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);
        }

     }

}



Thanks again for your help
Laughing



options.JPG
 Description:
Debug options settings
 Filesize:  17.27 KB
 Viewed:  11846 Time(s)

options.JPG


Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Sun Feb 10, 2008 1:12 pm    Post subject: Reply with quote

Hello puyopuy,

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

What happens if you delete everything within onReceiveIntent Question

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Mon Feb 11, 2008 11:56 am    Post subject: Reply with quote

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



LaunchOnStartup.zip
 Description:
My LaunchOnStartup source code

Download
 Filename:  LaunchOnStartup.zip
 Filesize:  33.11 KB
 Downloaded:  239 Time(s)

Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Mon Feb 11, 2008 3:19 pm    Post subject: Reply with quote

Hello

I altered the code to give some debug-output:
Java:
         boolean showLaunchOnStartup = false;  
          Log.d("DEBUGTAG", "Outside: showLaunchOnStartup=" + showLaunchOnStartup);
        if(showLaunchOnStartup){
          Log.d("DEBUGTAG", "Inside: showLaunchOnStartup=" + showLaunchOnStartup);
             /* 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);
        }


And it only shows Arrow Down and therefore everything is as it should be:
Java:
D/DEBUGTAG(590): Outside: showLaunchOnStartup=false


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Tue Feb 12, 2008 8:44 am    Post subject: Reply with quote

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. Laughing

Thank you very much.
puyopuy
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Tue Feb 12, 2008 9:43 am    Post subject: Reply with quote

Hello puyopuy,

are you hitting the Eclipse Run Button -Button in Eclipse Question

That happens because of Arrow Down , of couse.
XML:
       <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>


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Tue Feb 12, 2008 12:06 pm    Post subject: Reply with quote

Hi Plusminus,

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

Regards,
Puyopuy
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2655
Location: College Park, MD

PostPosted: Tue Feb 12, 2008 1:45 pm    Post subject: Reply with quote

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:
XML:
       <receiver class=".MyStartupIntentReceiver">
            <intent-filter>
                 <action android:value="android.intent.action.BOOT_COMPLETED" />
                 <category android:value="android.intent.category.HOME" />
            </intent-filter>
        </receiver>


When you hit Eclipse Run Button this will cause your application to start directly, because of:
XML:
       <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>


Hope things are clearer now.

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
puyopuy
Junior Developer
Junior Developer


Joined: 03 Feb 2008
Posts: 17

PostPosted: Thu Feb 14, 2008 2:19 am    Post subject: Reply with quote

Thanks for your detailed explanation, I will test it again this week. Laughing
Back to top
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.