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

6
我想创建一个后台应用程序,它可以监听哪些应用程序被启动并移到前台。
请回复,如果问题不清楚我会再次解释。
谢谢。
1个回答

7

以下是您可以做的事情:

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

谢谢您的回复...除此之外,是否有任何Listener API或任何类型的事件,通过它我们将获得当前调用的前台应用程序。 - Hare-Krishna
如果这是你的应用程序,你可以随时使用http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/Application.html#activate%28%29事件...但在其他情况下,我看不到任何选择。 - Maksym Gontar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接