【问题标题】:BlackBerry - background application to listen starts and foreground app黑莓 - 后台应用程序监听启动和前台应用程序
【发布时间】:2010-12-30 09:38:52
【问题描述】:

我想创建一个后台应用程序,它会监听哪些应用程序已启动以及哪些应用程序被移至前台。

请回复 如果问题不清楚将再次解释。

谢谢

【问题讨论】:

    标签: blackberry integration process background-application


    【解决方案1】:

    这是你可以做的:

    • 使用ApplicationManager.getForegroundProcessId()
    • 使用ApplicationManager.getVisibleApplications() 获取所有正在运行的应用
    • 使用ApplicationManager.getProcessId()按进程ID搜索应用
    • TimerTask 中定义时间段

      public class AppListenerApp extends Application {
      int mForegroundProcessId = -1;
      
      public AppListenerApp() {
          Timer timer = new Timer();
          timer.schedule(mCheckForeground, 2000, 2000);                       
      }
      
      public static void main(String[] args) {
          AppListenerApp app = new AppListenerApp();
          app.enterEventDispatcher();
      }
      
      TimerTask mCheckForeground = new TimerTask() {
          public void run() {
              int id = getForegroungProcessID();
              if(id != mForegroundProcessId)
              {
                  mForegroundProcessId = id;
                  String name = 
                      getAppNameByProcessId(mForegroundProcessId);
                  showMessage(name);
              }
          };
      };
      
      private int getForegroungProcessID() {
          return ApplicationManager.getApplicationManager()
                  .getForegroundProcessId();
      }
      
      private String getAppNameByProcessId(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].getName();
                  break;
              }
          }
          return result;
      }
      
      private void showMessage(String message) {
          synchronized (Application.getEventLock()) {
              Dialog dlg = new Dialog(Dialog.D_OK, message, 
                              Dialog.OK, null, Manager.FIELD_HCENTER);
              Ui.getUiEngine()
                              .pushGlobalScreen(dlg, 1, UiEngine.GLOBAL_QUEUE);
          }
      }
      }
      

    【讨论】:

    • 感谢您的回复...而不是这里有任何侦听器 API 或任何类型的事件,我们将通过这些事件获取当前调用的前台应用程序。
    • 如果它是您的应用程序,您始终可以使用blackberry.com/developers/docs/4.5.0api/net/rim/device/api/… 事件...但在其他情况下,我看不到任何选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    相关资源
    最近更新 更多