在后台线程上执行 Android 网络操作
Performing an Android network operation on a background thread
目前我使用了一个运行 3 秒的 Android OS 处理程序。问题是我不知道网络操作实际需要多长时间。有没有办法只在删除 200 条消息后停止进度对话框?
处理程序:
new android.os.Handler().postDelayed(
() -> {
// On complete call delete contact method
deleteAllMsgs(progressDialog);
// onLoginFailed();
}, 3000);
方法:
private void deleteAllMsgs(ProgressDialog progressDialog) {
ParseQuery<ParseObject> msgQuery= new ParseQuery<>(ParseConstants.CLASS_MESSAGE);
// Query the Comment class for comments that have a "author" column value equal to the objectId of the current User
msgQuery.whereEqualTo((ParseConstants.KEY_SENDER_AUTHOR_POINTER), ParseUser.msgQuery());
yeetQuery.setLimit(200);
msgQuery.findInBackground((messages, e) -> {
if (e == null) {
// Iterate over all messages
for (ParseObject delete : messages) {
// Delete messages from local data store
try {
delete.unpin();
} catch (ParseException e1) {
e1.printStackTrace();
}
// Delete messages from Parse
delete.deleteInBackground();
}
// Return to main screen
Toast.makeText(getApplicationContext(), "200 messages deleted", Toast.LENGTH_SHORT).show();
finish();
} else {
Log.e("Error", e.getMessage());
}
});
}
ProgressDialog progress = new ProgressDialog(getApplicationContext());
progress.setTitle("In Progress");
progress.show();
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//Do things.
runOnUiThread(new Runnable() {
@Override
public void run() {
//Update UI.
}
});
//Do things.
runOnUiThread(new Runnable() {
@Override
public void run() {
progress.dismiss();
}
});
}
});
progress.setOnDismissListener(newDialogInterface.OnDismissListener()
{
@Override
public void onDismiss(DialogInterface dialogInterface) {
//Called after your background task ended.
}
});
你可以在 asynctask 方法中尽可能多地使用 runOnUiThread 方法。有了它,您可以发送 toast 消息、设置文本视图的文本等...
您必须使用异步任务方法,而不是使用 OS 处理程序,请参考此处 https://www.concretepage.com/android/android-asynctask-example-with-progress-bar。
这里不一定要用"progressUpdate()",不用那个也可以
所以你可以把progressdialog的初始化放在"preExecute()",
然后将你的 "deleteAllMessage()" 放入 doInBackguound() 和
任务完成后会自动跳转到"postExecute()"。
这样你就可以停止显示进度条了。通过调用:
progressdialog.stop()
Is there a way to stop the progress dialog only when the 200 messages have been deleted?
是的,您需要正确使用 Parse 的回调。
将您的 "after messages deleted" 代码 紧接在 for 循环之后而不是在回调之外
并且您不需要 ParseQuery.findInBackground 或 deleteInBackground
的处理程序或异步任务
此外,您应该使用带有回调的 deleteAllInBackground(List<T> objects, DeleteCallback callback)
来了解对象列表何时被删除,而不是必须先找到它们
目前我使用了一个运行 3 秒的 Android OS 处理程序。问题是我不知道网络操作实际需要多长时间。有没有办法只在删除 200 条消息后停止进度对话框?
处理程序:
new android.os.Handler().postDelayed(
() -> {
// On complete call delete contact method
deleteAllMsgs(progressDialog);
// onLoginFailed();
}, 3000);
方法:
private void deleteAllMsgs(ProgressDialog progressDialog) {
ParseQuery<ParseObject> msgQuery= new ParseQuery<>(ParseConstants.CLASS_MESSAGE);
// Query the Comment class for comments that have a "author" column value equal to the objectId of the current User
msgQuery.whereEqualTo((ParseConstants.KEY_SENDER_AUTHOR_POINTER), ParseUser.msgQuery());
yeetQuery.setLimit(200);
msgQuery.findInBackground((messages, e) -> {
if (e == null) {
// Iterate over all messages
for (ParseObject delete : messages) {
// Delete messages from local data store
try {
delete.unpin();
} catch (ParseException e1) {
e1.printStackTrace();
}
// Delete messages from Parse
delete.deleteInBackground();
}
// Return to main screen
Toast.makeText(getApplicationContext(), "200 messages deleted", Toast.LENGTH_SHORT).show();
finish();
} else {
Log.e("Error", e.getMessage());
}
});
}
ProgressDialog progress = new ProgressDialog(getApplicationContext());
progress.setTitle("In Progress");
progress.show();
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//Do things.
runOnUiThread(new Runnable() {
@Override
public void run() {
//Update UI.
}
});
//Do things.
runOnUiThread(new Runnable() {
@Override
public void run() {
progress.dismiss();
}
});
}
});
progress.setOnDismissListener(newDialogInterface.OnDismissListener()
{
@Override
public void onDismiss(DialogInterface dialogInterface) {
//Called after your background task ended.
}
});
你可以在 asynctask 方法中尽可能多地使用 runOnUiThread 方法。有了它,您可以发送 toast 消息、设置文本视图的文本等...
您必须使用异步任务方法,而不是使用 OS 处理程序,请参考此处 https://www.concretepage.com/android/android-asynctask-example-with-progress-bar。
这里不一定要用"progressUpdate()",不用那个也可以
所以你可以把progressdialog的初始化放在"preExecute()",
然后将你的 "deleteAllMessage()" 放入 doInBackguound() 和
任务完成后会自动跳转到"postExecute()"。
这样你就可以停止显示进度条了。通过调用:
progressdialog.stop()
Is there a way to stop the progress dialog only when the 200 messages have been deleted?
是的,您需要正确使用 Parse 的回调。
将您的 "after messages deleted" 代码 紧接在 for 循环之后而不是在回调之外
并且您不需要 ParseQuery.findInBackground 或 deleteInBackground
的处理程序或异步任务此外,您应该使用带有回调的 deleteAllInBackground(List<T> objects, DeleteCallback callback)
来了解对象列表何时被删除,而不是必须先找到它们