Just An Application

August 22, 2009

A Standalone Android Runtime: Launching HelloWorld The Hard Way

Filed under: Android, Java, Mobile Java, Standalone Android Runtime — Tags: , , — Simon Lewis @ 8:11 am

So how do we launch our newly installed HelloWorld application ? Easy, we just invoke startActivity() on the ActivityManager with the appropriate Intent.

If we were doing it from an Android application the code would look something like this.

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

...

    void launch(Context theContext, String thePackage, String theClassName)
    {
        theContext.
            startActivity(
                new Intent(
                    Intent.ACTION_MAIN).
                setComponent(
                    new ComponentName(
                        thePackage, 
                        theClassName)).
                addFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK));
    }

...

But of course we can’t do that because to run it we would have to launch the application containing the code to do the launching which is what we are trying to do in the first place.

Still we have a functioning IPC mechanism which we can use to get a Binder for the ActivityManager and which we can subsequently use to do the invocation so we can launch the application from outside so to speak. Except that we can’t. The ActivityManager, as you might expect, does not take kindly to applications being launched by things Android knows nothing about. Here is part of the code from the ActivityManagerService method startActivityLocked(). Its the variant with rather too many parameters for comfort starting at line 3003.


...

        ProcessRecord callerApp = null;
        if (err == START_SUCCESS && caller != null) {
            callerApp = getRecordForAppLocked(caller);
            if (callerApp != null) {
                callingPid = callerApp.pid;
                callingUid = callerApp.info.uid;
            } else {
                Log.w(TAG, "Unable to find app for caller " + caller
                      + " (pid=" + callingPid + ") when starting: "
                      + intent.toString());
                err = START_PERMISSION_DENIED;
            }
        }

...

This is of course exactly the right thing to do but we have an application to launch so we will simply elide that check for now,

Following this sleight of hand the application duly lauches and the main log shows the following

21-08-09 15:55:56: 22001: INFO: ActivityManager Starting activity: Intent { ... }

The Intent is

     {
         action = android.intent.action.MAIN 
         type   = 
         flags  = 0x10000000 
         comp   = {standalone.helloworld/standalone.helloworld.HelloWorldActivity}
     }

The value of flags is actually Intent.FLAG_ACTIVITY_NEW_TASK

21-08-09 15:56:01: 22001: INFO: ActivityManager Start proc standalone.helloworld for activity standalone.helloworld/.HelloWorldActivity: pid=25001 uid=10000 gids={}
21-08-09 15:56:04: 22001: INFO: ActivityManager Displayed activity standalone.helloworld/.HelloWorldActivity: 2583 ms

And the Event log shows this

21-08-09 15:56:01: 22001 2668988 am_create_task[30004]	      Task ID 2 
21-08-09 15:56:01: 22001 2668988 am_create_activity[30005]    Token 5975831 Task ID 2 Component Name standalone.helloworld/.HelloWorldActivity Action android.intent.action.MAIN MIME Type  URI  Flags 268435456 
21-08-09 15:56:01: 22001 2668988 am_proc_start[30014]	      PID 25001 UID 10001 Process Name standalone.helloworld Type activity Component standalone.helloworld/.HelloWorldActivity 
21-08-09 15:56:02: 22001 16490550 am_proc_bound[30010]        PID 25001 Process Name standalone.helloworld 
21-08-09 15:56:02: 22001 16490550 am_restart_activity[30006]  Token 5975831 Task ID 2 Component Name standalone.helloworld/.HelloWorldActivity 
21-08-09 15:56:04: 25001 7161559 am_on_resume_called[30022]   Component Name standalone.helloworld.HelloWorldActivity 
21-08-09 15:56:04: 22001 10621833 activity_launch_time[30009] Token 5975831 Component Name standalone.helloworld/.HelloWorldActivity time 2583 milliseconds 

Copyright (c) 2009 By Simon Lewis. All Rights Reserved.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: