0

实际上,我想制作一个应用程序,它将 getGlobalEvent 并通过另一个自定义应用程序控制该事件。有没有办法这样做。我可以从特定应用程序获取全局事件吗?它就像一个应用程序,它将锁定您的黑莓中的自定义应用程序,如果您在该锁定应用程序列表中添加以下应用程序并输入密码以访问,那么当您尝试打开该应用程序时,它会要求您输入您在锁定应用程序中设置的密码.

4

1 回答 1

7

常见建议

检查申请

不得不说,一个应用程序中可以有多个进程,所以我们将根据模块名称进行检查:

 private String getModuleNameByProcessId(int id) {
  String result = null;
  ApplicationManager appMan = ApplicationManager.getApplicationManager();
  ApplicationDescriptor appDes[] = appMan.getVisibleApplications();
  for (int i = 0; i < appDes.length; i++) {
   if (appMan.getProcessId(appDes[i]) == id) {
    result = appDes[i].getModuleName();
    break;
   }
  }
  return result;
 }

将应用程序移至后台?

是的, ApplicationManager中没有 requestBackground() ......所以你可以做的是requestForeground()在下一个不在前台的最佳应用程序上,这会将活动应用程序移动到后台!您甚至可以使用requestForegroundForConsole() 调出主屏幕:

 protected int switchForegroundModule() {
  int id = -1;
  ApplicationManager appMan = ApplicationManager.getApplicationManager();
  ApplicationDescriptor appDes[] = appMan.getVisibleApplications();
  for (int i = 0; i < appDes.length; i++) {
   if (!appDes[i].getModuleName().equalsIgnoreCase(STR_MODULE_NAME)) {
    id = appMan.getProcessId(appDes[i]);
    appMan.requestForeground(id);

    // give a time to foreground application
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {     
     e.printStackTrace();
    }    
    break;
   }
  }
  return id;
 }

全局对话

只需输入密码,您就可以扩展 Dialog,使用结果会更容易:

class PaswordDialog extends Dialog {
 private BasicEditField mPwdField = new BasicEditField();

 public PaswordDialog() {
  super(Dialog.D_OK_CANCEL, "Enter password", Dialog.CANCEL, null,
    Dialog.FIELD_HCENTER);
  add(mPwdField);
 }

 public String getPassword() {
  return mPwdField.getText();
 }
}

密码检查将如下所示:

private boolean checkPassword() {
  boolean result = false;
  final PaswordDialog pwdDlg = new PaswordDialog();
  invokeAndWait(new Runnable() {
   public void run() {
    Ui.getUiEngine().pushGlobalScreen(pwdDlg, 0,
      UiEngine.GLOBAL_MODAL);
   }
  });
  result = ((Dialog.OK == pwdDlg.getSelectedValue()) && pwdDlg
    .getPassword().equalsIgnoreCase(STR_PASSWORD));
  return result;
 }

把这一切放在一起

阻止地址簿应用程序的示例:

public class LockMainApp extends Application {

 private static final String STR_MODULE_NAME = "net_rim_bb_addressbook_app";
 private static final String STR_PASSWORD = "12345";
 int mFGProcessId = -1;

 public LockMainApp() {
  Timer timer = new Timer();
  timer.schedule(mCheckForeground, 1000, 1000);
 }

 public static void main(String[] args) {
  LockMainApp app = new LockMainApp();
  app.enterEventDispatcher();
 }

 TimerTask mCheckForeground = new TimerTask() {
  public void run() {
   int id = ApplicationManager
       .getApplicationManager().getForegroundProcessId();
   if (id != mFGProcessId) {
    mFGProcessId= id;
    String moduleName = getModuleNameByProcessId(mFGProcessId);
    if (moduleName.equalsIgnoreCase(STR_MODULE_NAME)) {
     if (!checkPassword())
      mFGProcessId = switchForegroundModule();

    }
   }
  };
 };
}
于 2009-12-31T11:47:49.123 回答