gpt4 book ai didi

BlackBerry - 获取当前进程 ID

转载 作者:行者123 更新时间:2023-12-01 13:07:40 29 4
gpt4 key购买 nike

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

ApplicationManager.getApplicationManager().getForegroundProcessId()

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

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

提前致谢!

加夫

最佳答案

所以你有三个模块:应用程序、库和服务。
您需要通过模块名称获取描述符,然后获取进程 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 进行比较,然后您将知道哪个模块使用了库代码。
更新2
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();
}
}

关于BlackBerry - 获取当前进程 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887270/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com