3

我阅读了 Blackberry - How to get the background application process id,但我不确定我是否理解正确。以下代码获取前台进程id;

ApplicationManager.getApplicationManager().getForegroundProcessId()

我有两个进程执行相同的代码来建立连接,我想记录进行调用的进程以及我所有常用的日志数据,以便更好地了解流程的工作方式。

是否可以获取当前正在运行代码的进程的 id?一个进程在前台(UI 进程),另一个在后台,但两者都使用通过运行时存储共享的相同连接库。

提前致谢!

加夫

4

1 回答 1

4

所以你有三个模块:应用程序、库和服务。
您需要通过模块名称获取描述符,然后获取进程ID。

更新1

String moduleName = "application";
int handle = CodeModuleManager.getModuleHandle(moduleName);
ApplicationDescriptor[] descriptors = CodeModuleManager
.getApplicationDescriptors(handle);
if (descriptors.length > 0 && descriptors[0] != null) {
    ApplicationManager.getApplicationManager().getProcessId(descriptors[0]);
}

然后,要记录哪个模块使用库,请使用

Application.getApplication().getProcessId();

内部库方法。我认为最好在库中实现日志记录。
当您从库代码中获得应用程序的进程 id 时,您可以将其与通过模块名称找到的 id 进行比较,然后您将知道哪个模块使用了库代码。
UPDATE2
alt text http://img138.imageshack.us/img138/23/eventlog.jpg
库模块代码:

package library;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.CodeModuleManager;
import net.rim.device.api.system.EventLogger;

public class Logger {
    // "AppLibSrvc" converted to long
    long guid = 0xd4b6b5eeea339daL;

    public Logger() {
        EventLogger.register(guid, "AppLibSrvc", EventLogger.VIEWER_STRING);
    }

    public void log(String message) {
        EventLogger.logEvent(guid, message.getBytes());
    }

    public void call() {
        log("Library is used by " + getModuleName());
    }

    private String getModuleName() {
        String moduleName = "";
        String appModuleName = "application";
        int appProcessId = getProcessIdByName(appModuleName);

        String srvcModuleName = "service";
        int srvcProcessId = getProcessIdByName(srvcModuleName);

        int processId = Application.getApplication().getProcessId();

        if (appProcessId == processId)
            moduleName = appModuleName;
        else if (srvcProcessId == processId)
            moduleName = srvcModuleName;
        return moduleName;
    }

    protected int getProcessIdByName(String moduleName) {
        int processId = -1;
        int handle = CodeModuleManager.getModuleHandle(moduleName);
        ApplicationDescriptor[] descriptors = CodeModuleManager
                .getApplicationDescriptors(handle);
        if (descriptors.length > 0 && descriptors[0] != null) {
            processId = ApplicationManager.getApplicationManager()
                    .getProcessId(descriptors[0]);
        }
        return processId;
    }
}

应用模块代码:

package application;

import java.util.Timer;
import java.util.TimerTask;

import library.Logger;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class App extends UiApplication {

    public App() {
        pushScreen(new Scr());
    }

    public static void main(String[] args) {
        App app = new App();
        app.enterEventDispatcher();
    }
}

class Scr extends MainScreen {
    public Scr() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                Logger logger = new Logger();
                logger.call();
            }
        };
        timer.schedule(task, 3000, 3000);
    }
}

服务模块代码:

package service;

import java.util.Timer;
import java.util.TimerTask;

import library.Logger;
import net.rim.device.api.system.Application;

public class App extends Application {

    public App() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                Logger logger = new Logger();
                logger.call();
            }
        };
        timer.schedule(task, 3000, 3000);
    }

    public static void main(String[] args) {
        App app = new App();
        app.enterEventDispatcher();
    }
}
于 2009-12-11T13:12:19.803 回答