BlackBerry应用开发2 - 调用第三方应用

BlackBerry应用开发2 - 调用第三方应用

在开发应用程序的时候,可能涉及到调用其他应用实现某些功能。

如何实现调用第三方应用呢?

可以通过 ApplicationManager 的 runApplication 方法启动相关应用

int runApplication(ApplicationDescriptor descriptor) 
          Creates a new process to run an application and ask it to foreground itself. 
abstract  int runApplication(ApplicationDescriptor descriptor, boolean grabForeground) 
          Creates a new process to run an application. 

要调用 runApplication 方法
首先获得ApplicationManager的实例

ApplicationManager myApp = ApplicationManager.getApplicationManager();

然后需要获取模块描述
参数 “module name”为模块名,即cod文件名称

int mh = CodeModuleManager.getModuleHandle("module name");
ApplicationDescriptor[] des = CodeModuleManager.getApplicationDescriptors(mh);
ApplicationDescriptor descriptor = new ApplicationDescriptor(des[0], si);

通过 ApplicationManager 启动应用,并将应用显示到前台

ApplicationManager.getApplicationManager().runApplication(descriptor);

代码示例:

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.ApplicationManagerException;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class MyScreen extends MainScreen
{
    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
        // Set the displayed title of the screen       
        setTitle("Code Module Manager Test");
        
        ButtonField b1 = new ButtonField("button1");

        b1.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				int mh = CodeModuleManager.getModuleHandle("test_global_event");
				if(mh != 0){
					String[] si = {""};
					
					ApplicationDescriptor[] des = CodeModuleManager.getApplicationDescriptors(mh);
					final ApplicationDescriptor descriptor = new ApplicationDescriptor(des[0], si);
					
					UiApplication.getUiApplication().invokeLater(new Runnable() {
						public void run() {
							try{
								ApplicationManager.getApplicationManager().runApplication(descriptor);
							} catch (ApplicationManagerException e) {
								e.printStackTrace();
							}
						}
					});
				}
			}
		});
        
        add(b1);
    }
}

你可能感兴趣的:(Module,application,Class,BlackBerry,Descriptor,behavior)