黑莓 - 后台应用程序监听启动和前台应用程序

Har*_*hna 6 integration blackberry process background-application

我想创建后台应用程序,它将监听启动的应用程序和移动到前台的内容.

请回复如果问题不明确将再次解释.

谢谢

Mak*_*tar 7

这是你可以做的:

  • 使用ApplicationManager.getForegroundProcessId()
  • 使用ApplicationManager.getVisibleApplications()来获取所有正在运行的应用程序
  • 使用ApplicationManager.getProcessId()按进程ID搜索app
  • 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);
        }
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)