gpt4 book ai didi

java - AsyncTask - 从主 UI 线程调用

转载 作者:行者123 更新时间:2023-12-01 10:13:56 25 4
gpt4 key购买 nike

在我为 Android 应用程序中的 Activity 组合的 AsyncTask 期间尝试显示和隐藏对话框时,我遇到了问题。

这是我目前正在做的事情:

public void syncCard(final Tag tag) {
syncTask = new SyncTask();
syncTask.execute(tag);
}

private class SyncTask extends AsyncTask<Tag, Void, Void> {
private final Dialog dialog;
private boolean mException;

public SyncTask() {
// Prepare dialog
dialog = buildSyncingDialog();
}

@Override
protected void onPreExecute() {
dialog.show();
}

@Override
protected Void doInBackground(Tag... params) {
mTagcomm = IsoDep.get(params[0]);
if (mTagcomm == null) {
//TODO - Handle communication error with the present card
}
mException = false;

try {
// Open connection
mTagcomm.connect();
lastAts = getAts(mTagcomm);

mProvider.setmTagCom(mTagcomm);

EmvParser parser = new EmvParser(mProvider, true);
mCard = parser.readEmvCard();
if (mCard != null) {
mCard.setAtrDescription(extractAtsDescription(lastAts));
}

} catch (IOException e) {
mException = true;
} finally {
// close tagcomm
IOUtils.closeQuietly(mTagcomm);
}

return null;
}

@Override
protected void onPostExecute(Void result) {
if (dialog.isShowing()) {
dialog.dismiss();
}

if (!mException) {
if (mCard != null) {
showPaymentCardDetails();
} else if (mCard.isNfcLocked()) {
//TODO - Show error message informing user the card is locked
}
} else {

}
}

private Dialog buildSyncingDialog() {
String syncingText = String.format( "%s\n%s",
getString( R.string.syncing ), getString( R.string.do_not_remove_card ) );
Dialog dialog = new CustomDialog( PaymentCardRegisterActivity.this,
syncingText, R.layout.layout_syncing );
dialog.setCancelable( false );

return dialog;
}

@Override
public void onCancelled() {
dialog.dismiss();
}
}

我看到以下错误:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

我遇到了这个旧答案 - Can't create handler inside thread which has not called Looper.prepare()

但是我仍然对我需要做什么感到有点困惑,该帖子的最后一个回复提到从 UI 线程运行任务,我将如何使用上面的代码来执行此操作?

编辑

我的 Activity 类扩展了 NfcActivity,因为我使用此类来读取非接触式卡详细信息。

我的 syncCard 正在从另一个后台线程调用(我相信),就像这样

@Override
public void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
processIntent(intent);
}

private void processIntent(final Intent intent) {
String action = intent.getAction();

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tagFromIntent = intent.getParcelableExtra( NfcAdapter.EXTRA_TAG );
onTagDiscovered(tagFromIntent);
}
}

@Override
public void onTagDiscovered( final Tag tag ) {
super.onTagDiscovered(tag);
try {
PaymentCardRegisterActivity.this.syncCard(tag);
} catch ( Exception e) {
Log.e( PaymentCardRegisterActivity.class.getName(), "Failed to sync card", e );
// Return to start activity
finish();
}
}

最佳答案

如果 onTagDiscovered 正在非 UI 线程的线程上运行,那么您应该将对 syncCard 的调用分派(dispatch)到主线程。如果您有对 Activity 的引用,则可以使用 runOnUiThread 并发布 Runnable。这将确保 Runnable 在主线程上执行。在您的代码中,我认为您需要更改此内容

@Override
public void onTagDiscovered( final Tag tag ) {
super.onTagDiscovered(tag);
try
{
PaymentCardRegisterActivity.this.syncCard(tag);
}
catch ( Exception e)
{
Log.e( PaymentCardRegisterActivity.class.getName(), "Failed to sync card", e );
// Return to start activity
finish();
}
}

进入此

@Override
public void onTagDiscovered( final Tag tag ) {
super.onTagDiscovered(tag);
runOnUiThread(new Runnable() {

public void run() {
try {
PaymentCardRegisterActivity.this.syncCard(tag);
} catch ( Exception e) {
Log.e( PaymentCardRegisterActivity.class.getName(), "Failed to sync card", e );
}
}
});
}

关于java - AsyncTask - 从主 UI 线程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36012543/

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