BlackBerry - 用于监听启动的后台应用程序和前台应用程序

发布于 2024-08-16 04:11:35 字数 82 浏览 2 评论 0 原文

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

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

谢谢

I would like to create background application which will listen to what applications are started and what are moved to foreground.

Please reply
If question is not clear will explain again.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-08-23 04:11:36

这是您可以执行的操作:

  • 使用 ApplicationManager.getForegroundProcessId()
  • 使用 ApplicationManager.getVisibleApplications() 让所有正在运行的应用程序
  • 使用 ApplicationManager.getProcessId() 到按进程 ID 搜索应用程序
  • TimerTask 中执行此操作 有规定的期限

    public class AppListenerApp 扩展应用程序 {
    int mForegroundProcessId = -1;
    
    公共AppListenerApp(){
        计时器计时器 = new Timer();
        计时器.schedule(mCheckForeground, 2000, 2000);                       
    }
    
    公共静态无效主(字符串[] args){
        AppListenerApp 应用 = new AppListenerApp();
        app.enterEventDispatcher();
    }
    
    TimerTask mCheckForeground = new TimerTask() {
        公共无效运行(){
            int id = getForegroungProcessID();
            if(id != mForegroundProcessId)
            {
                mForegroundProcessId = id;
                字符串名称 = 
                    getAppNameByProcessId(mForegroundProcessId);
                显示消息(名称);
            }
        };
    };
    
    私有 int getForegroungProcessID() {
        返回ApplicationManager.getApplicationManager()
                .getForegroundProcessId();
    }
    
    私有字符串 getAppNameByProcessId(int id) {
        字符串结果=空;
        应用程序管理器appMan = 
                    ApplicationManager.getApplicationManager();
        应用程序描述符 appDes[] = 
                    appMan.getVisibleApplications();
        for (int i = 0; i < appDes.length; i++) {
            if (appMan.getProcessId(appDes[i]) == id) {
                结果 = appDes[i].getName();
                休息;
            }
        }
        返回结果;
    }
    
    私人无效showMessage(字符串消息){
        同步(Application.getEventLock()){
            对话框 dlg = 新对话框(Dialog.D_OK, 消息, 
                            Dialog.OK, null, Manager.FIELD_HCENTER);
            Ui.getUiEngine()
                            .pushGlobalScreen(dlg, 1, UiEngine.GLOBAL_QUEUE);
        }
    }
    }
    

This is what you can do:

  • use ApplicationManager.getForegroundProcessId()
  • use ApplicationManager.getVisibleApplications() to get all running apps
  • use ApplicationManager.getProcessId() to search for app by process id
  • do this in TimerTask with defined period

    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);
        }
    }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文