Create an application which will lock another application event

二次信任 提交于 2019-11-30 16:42:52
Maksym Gontar

Common advices

Checking Application

Have to say, there can be several processes within one application so we will perform check based on module name:

 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;
 }

Move application to Background?

Yep, there's no requestBackground() in ApplicationManager... so what you can do is requestForeground() on the next best app which is not on foreground, and this will move active app to background! You can even bring up Home Screen with 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;
 }

Global Dialog

Just to input password you can extend Dialog, it will be easier to consume result:

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();
 }
}

And password check will look like:

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;
 }

Put this all together

Sample to block Adress Book App:

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();

    }
   }
  };
 };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!