gpt4 book ai didi

java - Android Asynctask 和 runOnUiThread

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:13 24 4
gpt4 key购买 nike

我最近尝试实现这个项目 openalpr,它在原始代码中运行得很好。但是当我尝试修改它并添加一些功能时,我遇到了一些问题。这是它的部分代码:

AsyncTask.execute(new Runnable() {
@Override
public void run() {
String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

Log.d("OPEN ALPR", result);

try {
final Results results = new Gson().fromJson(result, Results.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (results == null || results.getResults() == null || results.getResults().size() == 0) {
Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
resultTextView.setText("It was not possible to detect the licence plate.");
} else {
resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
// Trim confidence to two decimal places
+ " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
// Convert processing time to seconds and trim to two decimal places
+ " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
}
}

这工作得很好。但是当我尝试在 runOnUiThread 之前获取“结果”时,它无法再正确检测。下面是我修改后的代码:

AsyncTask.execute(new Runnable() {
@Override
public void run() {
String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

Log.d("OPEN ALPR", result);

try {
final Results results = new Gson().fromJson(result, Results.class);
if(results!=null) Log.d("ShowTheResults",results.getResults().get(0).getPlate());
runOnUiThread(new Runnable() {
@Override
public void run() {
if (results == null || results.getResults() == null || results.getResults().size() == 0) {
Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
resultTextView.setText("It was not possible to detect the licence plate.");
} else {
resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
// Trim confidence to two decimal places
+ " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
// Convert processing time to seconds and trim to two decimal places
+ " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
}
}

我只是不明白为什么输出会与 UI 线程不同。谁能告诉我如何在 UI 上显示“结果”之前在后台正确使用它?

最佳答案

我认为您使用AsyncTask的方式非常糟糕。您想要在异步线程上执行 Runnable,然后在其中调用 runOnUiThread 来处理 UI 线程上的结果。为什么不按照 AsyncTask 的预期用途来使用它们呢?

创建一个新任务并使用其常用方法:doInBackgroundonProgressUpdateonPostExecute。如果 AsyncTask 在主线程上执行,则最后两个任务已在 UI 线程上调用。

new AsyncTask<Void, Void, Results>() {
@Override
protected Results doInBackground(Void... params) {
String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("us", "", destination.getAbsolutePath(), openAlprConfFile, 10);

Log.d("OPEN ALPR", result);

Results results = new Gson().fromJson(result, Results.class);
if (results != null || results.getResults() != null || results.getResults().size() > 0)
Log.d("ShowTheResults", results.getResults().get(0).getPlate());

return results;
}

@Override
protected void onPostExecute(Results result) {
if (results == null || results.getResults() == null || results.getResults().size() == 0) {
Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
resultTextView.setText("It was not possible to detect the licence plate.");
} else {
resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
// Trim confidence to two decimal places
+ " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
// Convert processing time to seconds and trim to two decimal places
+ " Processing time: " + String.format("%.2f", ((results.getProcessingTimeMs() / 1000.0) % 60)) + " seconds");
}
}
}.execute();

关于java - Android Asynctask 和 runOnUiThread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42759506/

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