問題描述
顯示等待通知,直到回調從線程返回 (Show waiting notification till callback returns from thread)
I am new to android and am stuck with this situation
I am calling a thread from the Activity UI. The thread will return to a callback function after doing some background operation .
Till then I need to show a waiting notification in the Activity UI.[Effectively freezing the app till the callback returns]
I tried with status flags and async task, but with partial success.
Please tell me the optimal solution to this problem.
‑‑‑‑‑
參考解法
方法 1:
Show A Progress before you start a thread and dismiss the progress dialog just before returning.
For more about progress dialog see this link :http://developer.android.com/reference/android/app/ProgressDialog.html
方法 2:
You need to show the progress dialog box just before you start your thread.
private CustomizeDialog mCustomizeDialog = null;
//in onCreate method
mSubmitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//checking few condition
mCustomizeDialog = new CustomizeDialog(makeRequestActivity.this);
mCustomizeDialog.setTitle("Confirmation");
mCustomizeDialog.setMessage("Make Request?");
mCustomizeDialog.okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCustomizeDialog.dismiss();
show();
});
mCustomizeDialog.show();
private void show() {
mCustomizeDialog = new CustomizeDialog(makeRequestActivity.this);
mCustomizeDialog.setTitle("Requesting");
mCustomizeDialog.setMessage("Connecting...");
mCustomizeDialog.okButton.setVisibility(View.GONE);
mCustomizeDialog.cancelButton.setVisibility(View.GONE);
mCustomizeDialog.show();
}
and in my broadcast receiver
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(request.CUSTOM_INTENT_MENU)) {
mExtras = intent.getExtras();
String result = mExtras.getString("tag");
if (mCustomizeDialog.isShowing()) {
mCustomizeDialog.dismiss();
}
}
};