Deutsche Version   Deutsche Version


Version Modifications
1.0 (2020-04-16) Initial version
2.0 (2020-04-29) - Allows the evaluation of start and return values
- Start as an independent or dependent process
2.1 (2020-06-03) MyAppName, MyPackageName added

Motivation

App Inventor enables other activities to be started with the ActivityStarter component. Unfortunately, starting installed apps is very cumbersome. According to the instructions, the .apk of the app to be started must be downloaded and the package name found. Both are neither easy nor always possible.

This extension simplifies starting apps.


Content

Download

Usage

Package Names

Start-Modes

Start Values

Return Values

Reference

Example

Data exchange between two AI2 apps

Launch via App list

Tools

Download

The ZIP archive UrsAI2AppLauncher for Download. The archive contains the source code, the compiled binary for uploading to App Inventor and a sample application.

Usage

Package Names

You must know the package name (e.g.  "de.ullisroboterseite.ursai2applauncher") of an app to launch it. The method getNameList provides a list of the names of all apps that can be launched. There is a filter option to make a preselection. The list can, for example, be presented in a ListPicker component.

The PackageFromName method returns the package name associated with the specified app. The package name can then be used to start the designated app.

Launch modes

The app to be launched can be started in two ways:

In both cases, the started app can be launched with start values.

After starting an app, the AfterAppLaunched event is triggered. A parameter indicates whether the app was started successfully.

Start values

In Android an app is launched  via an Intent. An intent is a abstract description of an operation to be performed. The intent contains all the information needed to start an app and also contains the data to be exchanged.

Data to be exchanged between apps can be stored in the intent as so-called Extras. Extras are a list of key-value pairs. The started app can retrieve and evaluate these values.

The app to be launched can be supplied with start values via different variants of the Launch... methods.

  1. First of all, the app can be started without any additional start value (LaunchApp, LaunchAppForResult).
  2. If the app to be lauched is an AI2 app, the AI2 standard method for parameter transfer can be used. AI2 evaluates the "APP_INVENTOR_START" key and provides the associated value via the Control - getStartValue or Control - getPlainStartText method. The parameter StartValue is stored with the key "APP_INVENTOR_START" through the methods LaunchAI2App, LaunchForResultAI2App.
  3. LauchAppExtra and LauchForResultAppExtra allow the transfer of a single key-value pair.
  4. LauchAppExtraList and LauchForResultAppExtraList transfer a list of key-value pairs.

Return Values

The following section is only relevant for apps started as a dependent process. Only these can return values.

Always the called app can be canceled without returning results. In this case, the ActivityCanceled event is triggered.

If the called app was terminated regularly, the AfterActivity event is triggered and data ​​can be returned. Like the start values, these are stored internally as key-value pairs in an intent. AI2 apps can provide return values ​​via the blocks close screen with value and close screen with plain text. AI2 sets this value as a key-value pair with the key "APP_INVENTOR_RESULT" in the Intent.

The calling app can evaluate the returned Intent. The extension provides appropriate methods.

  1. The AfterActivity event has the Result parameter . If the launched app returns a value with the key "APP_INVENTOR_RESULT",the value is converted into a string and made available via the parameter Result.
  2. The getResultKeys method provides a list of all key values ​​of all key-value pairs.
  3. The getResultValue method returns the value associated with the specified key as a String.
  4. The getResultObject method returns the value associated with the specified key as an object, i.e. with the same data type as it was stored by the launched app.

Reference

Block Function Comment
App names and package names
getNameList Provides the names of the installed and launchable apps as an alphabetically sorted list.

A pre-selection can be made using the Filter parameter. Only those apps whose names contain the specified text are returned. Specify an empty text ("") for no filtering. The filter value is not case sensitive.
Filtering is done with the Java method String.contains().  Before being applied the filter value and the app name are converted to lowercase letters using String.toLowerCase().
getPackageList Provides the package names of the installed and launchable apps as an alphabetically sorted list.

A pre-selection can be made using the Filter parameter. Only those apps whose names contain the specified text are returned. Specify an empty text ("") for no filtering. The filter value is not case sensitive.
Filtering is done with the Java method String.contains(). Before being applied the filter value and the app name are converted to lowercase letters using String.toLowerCase().
PackageFromName Returns the package name of the app, whose app name was specified in the AppName parameter. An empty text ("") is returned if no package name could be determined.
MyAppName
MyPackageName
Provides the name of the current app or its package name.  
Launching Apps
LauchApp
LauchAppForResult
Launches the app with the specified package name either as an independent or as a dependent process (see above). The success of this action will be presented via the AfterAppLaunched event (see below).
LaunchAI2App
launchforresultai2app
Launches the app with the specified package name either as an independent or as a dependent process.

Information to the launched app can be transmitted via the Key/Value parameter pair. Key and Value expect a text as argument.
If parameters with another type as text (String) are supplied, AI2 converts them into text.

AI2 evaluates the "APP_INVENTOR_START" key and provides the associated value via the getStartValue getstartvalue method or. getPlainStartText getPlainStartText method.

getStartValue transfers any object type. You can use a List to transfer more than one value.
LauchAppExtraList
LauchForResultAppExtraList
Launches the app with the specified package name either as an independent or as a dependent process.

The Pairs parameter is a list of lists with two values each. The first value is interpreted as a key, the second as an associated value.
Further informations at
http://ai2.appinventor.mit.edu/eference/other/activitystarter.html
or
https://appinventor.mit.edu/explore/ai2/activity-starter.html
AfterAppLaunched Returns the result of the start attempt:
0: successful
1: package not found
2: Pairs not constructed correctly
 
Return Values
closeScreenWithValue
closeScreenWithPlainText
Specifies the return values.
AI2 transfers the specified values ​​as a key/value pair with the key "APP_INVENTOR_RESULT".
ActivityCanceled The launched app has been canceled. Return values ​​are not available.
AfterActivity The called app was terminated regularly.

Result: Return value of an AI2 app.
If the launched app returns a key/value pair with the key "APP_INVENTOR_RESULT", the value is converted into a String and presented via the parameter Result .
getResultKeys Returns a list of all keys of all key/value pairs contained in the returned Intent. The return value of the method is a List of String.
getResultValue Returns the value associated with  Key as a String. The value is converted into a String by the Java function toString.
getResultObject Returns the value associated with Key as a Object. There is no conversion. AI2 passes values ​​as a JSON strings.

Example

The download archive contains two sample apps:

Data exchange between two AI2 apps

The first example shows the data exchange between two AI2 apps.

The app is started with its name.
The result of the attempt is displayed.
The app returns a list containing text values. The list of the keys of the key/value pairs and the value of the key/value pair with the key "APP_INVENTOR_RESULT" is displayed.

 

Launching via an app list

The following example shows how to use the getNameList and PackageFromName methods to launch an app. The call of LaunchForResult results in the starting app being paused until the launched app has terminated.

  Displays the list of available apps Choosing the "Kamera" app (It's a German Android version) Launch the app   Displays termination state of the launched app

 

Tools

For developing own extensions I gathered some tips: AI2 FAQ: Develop Extensions

To Do

Start an app via ACTION

Creation of an Intent and start of an app via the Intent