黑莓手机 - 后台应用程序,听取启动和应用前景 [英] BlackBerry - background application to listen starts and foreground app

查看:223
本文介绍了黑莓手机 - 后台应用程序,听取启动和应用前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建后台应用,这将听什么应用程序启动并移动到前台。什么

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.

感谢

推荐答案

这是你可以做什么:


  • 使用<一个href=\"http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/ApplicationManager.html#getForegroundProcessId%28%29\">ApplicationManager.getForegroundProcessId()

  • 使用<一个href=\"http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/ApplicationManager.html#getVisibleApplications%28%29\">ApplicationManager.getVisibleApplications()让所有正在运行的应用

  • 使用<一个href=\"http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/system/ApplicationManager.html#getProcessId%28net.rim.device.api.system.ApplicationDescriptor%29\">ApplicationManager.getProcessId()通过进程ID来搜索应用程序

  • 在规定的时间之后 TimerTask的做到这一点

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


这篇关于黑莓手机 - 后台应用程序,听取启动和应用前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆