Tasked with customizing a third party app with our branding and without access to the source code, a launcher app is in order. This launcher app enables us to have a custom icon and also perform setup tasks on the device before the “real” app is invoked. It’s important to check if the application is already running before trying to create a new process instance or you will have unpredictable results. This is accomplished with the ApplicationManager by calling getVisibileApplications(). If there are no running instances, then you are free to create one using runApplication().
Enjoy the source code below!
private void launch(String applicationName) throws Exception
{
//Check if the application is already running.
ApplicationManager manager = ApplicationManager.getApplicationManager();
ApplicationDescriptor descriptors[] = manager.getVisibleApplications();
for(int i=0; i < descriptors.length; i++)
{
if(descriptors[i].getName().equals(applicationName))
{
manager.requestForeground( manager.getProcessId( descriptors[i] ));
System.exit(0);
}
}
//Find the application on the device and try to start it.
int moduleHandle = CodeModuleManager.getModuleHandle(applicationName);
ApplicationDescriptor[] appDescriptors = CodeModuleManager.getApplicationDescriptors( moduleHandle );
for(int i=0; i < appDescriptors.length; i++)
{
ApplicationDescriptor descriptor = appDescriptors[i];
try {
int processId = ApplicationManager.getApplicationManager().runApplication( descriptor );
if(processId > 0)
System.exit(0);
}
catch (ApplicationManagerException e) {
throw e;
}
}
//Couldn't find the application
throw new Exception(applicationName + " is not detected.");
}
Advertisement