问题
How to Exit a blackberry application from another application programatically.
e.g. In Blackberry i install two application than i need to exit on second application from first application or exit to another application from my application.
回答1:
The cleanest approach IMHO is to implement the interface GlobalEventListener
in the Application you want to close. Then, from the calling app, send a global event with some custom code of your own, and let the listening app to close itself in a controlled way.
Example:
In the app you want to close:
public class YourCustomApp extends UiApplication implements GlobalEventListener {
public static final long PID = <unique id for your app here>;
public static final int CLOSE_APP_CODE = <your code here>;
public void eventOccurred(long pid, int msgCode, int data1, Object object0, Object object1) {
if(pid == PID){
switch(msgCode){
case CLOSE_APP_CODE:
controlledClose(); //This method could be, System.exit(0) if no other action is required
break;
}
}
}
}
In the calling app:
ApplicationManager.getApplicationManager().postGlobalEvent(PID, CLOSE_APP_CODE, 0, null, null);
In the second code snippet, you should replace the int and long codes with the correct ones. The calling app should pass the correct values. You could also place the codes in a library and let both apps use the values from there. Make sure the PID is unique systemwide so that it does not not collide with other apps in the system. A good practice is to use the full qualified package name of your app and then right click -> "convert string to long" in eclipse plugin.
UPDATE:
The hacky way: You can try using EventInjection to inject from your app the key combo that can close a certain app. For instance, the Camera app closes itself when pressing ESC key. So we could try to close it whith this line:
EventInjector.invokeEvent( new EventInjector.KeyCodeEvent(EventInjector.KeyCodeEvent.KEY_DOWN,Characters.ESCAPE, 0));
This approach is extremely unreliable. Sometimes, it doesn't work, and you might need to fine tune the injection by adding short Thread.sleep
calls after each injected event. Or you might need to call the injection code from the Ui thread.
Remember to assign to your app the permissions for key injection.
来源:https://stackoverflow.com/questions/10978317/how-to-exit-a-blackberry-application-from-another-application-programatically