我有一些 Android 应用程序应该在启动时加载一些数据并尽快处理它。为了在 native 代码中完成更好的性能数据处理。 但有时数据量增加,处理时会阻塞 UI。奇怪的是,我从后台线程启动 JNI 调用,并且我相信 native 代码也应该在后台工作。 那么几个问题:
- 如何检测 native 代码是否在主线程中运行?
- 如何检测谁阻止了 UI?
一些代码示例:
// java
new Thread(new Runnable() {
@Override
public void run() {
ResourceLoader.loadListsAndParse(); // native call
}
}).start();
// c++
void RulesManager::loadRules() {
if (!rules_loaded_) {
using namespace std;
base::FilePath my_dir;
GetCacheDirectory(&my_dir);
this->loadRulesInternal(my_dir, true);
}
}
void RulesManager::loadRulesInternal(base::FilePath& path, bool processExceptions) {
base::FileEnumerator files(path, false, base::FileEnumerator::FILES);
for (base::FilePath name = files.Next(); !name.empty(); name = files.Next()) {
this->loadRulesFromFile(name.AsUTF8Unsafe(), processExceptions);
}
}
bool RulesManager::loadRulesFromFile(const std::string& filePath, bool processException) {
bool result = false;
std::ifstream file(filePath);
std::string str;
while (std::getline(file, str)) {
result = this->addRule(str, processException);
}
return result;
}
最佳答案
Android 在
关于android - native 调用会阻塞主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48023622/