android:隐藏progressdialog,当应用程序重新获得控制权

时间:2013-11-19 11:35:01

标签: android facebook controls progressdialog

我有一个应用程序,当你点击一个按钮打开Facebook应用程序,它工作正常但在慢速设备(如我的)facebook需要几秒钟才能显示,所以我想添加一个简单的progressdialog说“请等一下“

我可以显示进度对话框并使用以下代码打开Facebook:

final ProgressDialog pd = ProgressDialog.show(contatti.this, "", "Attendere...", true);
            Intent facebookIntent = getOpenFacebookIntent(getApplicationContext());
            startActivity(facebookIntent);

            //pd.dismiss();

我第一次尝试,它工作正常,但当我从Facebook回到我的应用程序时,对话框仍然显示,我无法关闭它。

添加了dismiss()以尝试隐藏它,但这是一个愚蠢的想法>。<

当应用程序重新获得控制权时,如何解除对话框?

3 个答案:

答案 0 :(得分:1)

对于这种情况,如果应用程序发送到后台然后关闭对话框,则必须检查应用程序是否在暂停时发送。

用于检查应用程序是否为bacground或者只是看看

android:how to check if application is running in background

答案 1 :(得分:0)

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gvsu.cis.toptracks.R;
import edu.gvsu.cis.toptracks.TopTrackListActivity;
import edu.gvsu.cis.toptracks.data.Customer;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class LastWebAPITask extends AsyncTask<String, Integer, String> {
    private ProgressDialog progDialog;
    private Context context;
    private TopTrackListActivity activity;
    private static final String debugTag = "LastWebAPITask";

    public LastWebAPITask(TopTrackListActivity activity) {
        super();
        this.activity = activity;
        this.context = this.activity.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progDialog = ProgressDialog.show(this.activity, "Search", this.context
                .getResources().getString(R.string.looking_for_tracks), true,
                false);
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Log.d(debugTag, "Background:" + Thread.currentThread().getName());
            String result = LastHelper.downloadFromServer(params);
            return result;
        } catch (Exception e) {
            return new String();
        }
    }

    @Override
    protected void onPostExecute(String result) {

        ArrayList<Customer> trackdata = new ArrayList<Customer>();

        progDialog.dismiss();
        if (result.length() == 0) {
            this.activity.alert("Unable to find track data. Try again later.");
            return;
        }

        try {
            JSONObject respObj = new JSONObject(result);
            JSONArray tracks = respObj.getJSONArray("GetStockListResult");
            for (int i = 0; i < tracks.length(); i++) {
                JSONObject track = tracks.getJSONObject(i);
                String Inventory_Status = track.getString("Inventory_Status");
                String modify_Date = track.getString("modify_Date");
                String msg = track.getString("msg");
                String serial_nbr = track.getString("serial_nbr");
                ;

                trackdata
                        .add(new Customer(Inventory_Status, modify_Date, msg, serial_nbr));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        this.activity.setTracks(trackdata);

    }
}


    import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class LastHelper {

    private static final String LastUrl = "http://etosxmldev.ctdi.com/ws/wcf/UNIVERSAL-DEMO/Service.svc/GetStockList?LocationId=300779360.svc/GetStockList/1111";
    private static final int HTTP_STATUS_OK = 200;
    private static byte[] buff = new byte[1024];
    private static final String logTag = "LastHelper";

    public static class ApiException extends Exception {
        private static final long serialVersionUID = 1L;

        public ApiException(String msg) {
            super(msg);
        }

        public ApiException(String msg, Throwable thr) {
            super(msg, thr);
        }
    }

    protected static synchronized String downloadFromServer(String... params)
            throws ApiException {
        String retval = null;

        String url = LastUrl;

        Log.d(logTag, "Fetching " + url);

        // create an http client and a request object.
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        try {

            // execute the request
            HttpResponse response = client.execute(request);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != HTTP_STATUS_OK) {
                // handle error here
                throw new ApiException("Invalid response from last.fm"
                        + status.toString());
            }

            // process the content.
            HttpEntity entity = response.getEntity();
            InputStream ist = entity.getContent();
            ByteArrayOutputStream content = new ByteArrayOutputStream();

            int readCount = 0;
            while ((readCount = ist.read(buff)) != -1) {
                content.write(buff, 0, readCount);
            }
            retval = new String(content.toByteArray());

        } catch (Exception e) {
            throw new ApiException("Problem connecting to the server "
                    + e.getMessage(), e);
        }

        return retval;
    }
}

你必须使用Asynctask类来执行此操作。在您的Activity类中为我工作示例

 LastWebAPITask lfmTask = new LastWebAPITask(
                        TopTrackListActivity.this);
lfmTask.execute(metroTxt);

答案 2 :(得分:0)

感谢另一个论坛。看起来最简单快捷的方法就是使用onResume():

@Override
public void onResume(){
    super.onResume();
    if(waitdialog != null){ //check if we are resuming (not coming from another activity)
        if (waitdialog.isShowing()) { //check if is showing
            waitdialog.dismiss(); //dismiss
        }
    }
}