ProgressDialog微调器不在AsyncTask中旋转(在某些设备上)

时间:2017-04-30 17:17:32

标签: android android-layout android-asynctask progressdialog

这是我单击ListView中的项目时正在执行的AsyncTask。我在任务运行时显示ProgressDialog,并在onPostExecute()结束时将其关闭,如下所示。这有效,微调器显示在我的Nexus 5X仿真器中。但是,在任何其他模拟器或我的真实设备(Galaxy S7 Edge Nougat)上,对话框显示但没有微调器或旋转器,但它立即冻结。我已经在开发人员选项中将所有过渡/动画打开到1x并且所有其他动画都有效,所以我知道这不是问题。

public class LoadHunt extends AsyncTask<String, Void, String> {

private static final String TAG = LoadHunt.class.getSimpleName();
private PostLoginFragment listener;
private String hunt;
private int item;
private ProgressDialog pd;

public LoadHunt(PostLoginFragment listener, int item) {
    this.listener = listener;
    this.item = item;
}

@Override
public void onPreExecute() {

    pd = new ProgressDialog(listener.getActivity());
    pd.setMessage("Loading...");
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.setCancelable(false);
    pd.show();

    //((PostLoginFragment)listener).getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

}

@Override
protected String doInBackground(String... huntname) {

    try {
        hunt = huntname[0];
        String link = "https://cniekirk.com/gethunt3.php?huntname=" + huntname[0];

        URL url = new URL(link);
        HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();

        urlCon.setDoInput(true);
        BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
        StringBuffer sb = new StringBuffer("");
        String s = "";

        while((s = in.readLine()) != null){
            sb.append(s);
        }

        in.close();

        return sb.toString();

    }catch (Exception e) {
        Log.e(TAG, "Connection error!");
        return null;
    }
}

@Override
protected void onPostExecute(String result){

    List<String> resultSplit = Arrays.asList(result.substring(0, result.length() - 5).split(" xxxx"));

    List<List<String>> clueDetailsSplit = new ArrayList<>();
    for(String clueStuff: resultSplit) {
        clueDetailsSplit.add(Arrays.asList(clueStuff.split("\\s+")));
    }

    final List<Clue> clues = new ArrayList<>();

    for(List<String> clueDetails : clueDetailsSplit) {

        final StringBuilder stringBuilder = new StringBuilder();

        for(int i = 1; i <= clueDetails.size() - 5; i++) {
            if(stringBuilder.length() > 0) {
                stringBuilder.append(" ");
            }
            stringBuilder.append(clueDetails.get(i));
        }

        final Clue clue;

        if(!(clueDetails.get(clueDetails.size() - 1).equals("noimage"))) {

            clue = new Clue(clueDetails.get(0), stringBuilder.toString(), com.ploetz.dev.treasurehunt.Data.Status.LOCKED,
                    Double.parseDouble(clueDetails.get(clueDetails.size() - 3)), Double.parseDouble(clueDetails.get(clueDetails.size() - 2)),
                    getBitmapFromUrl("https://cniekirk.com/" + clueDetails.get(clueDetails.size() - 1)));

        } else {

            clue = new Clue(clueDetails.get(0), stringBuilder.toString(), com.ploetz.dev.treasurehunt.Data.Status.LOCKED,
                    Double.parseDouble(clueDetails.get(clueDetails.size() - 3)), Double.parseDouble(clueDetails.get(clueDetails.size() - 2)));

        }

        clues.add(clue);

    }

    //((PostLoginFragment)listener).getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

    GlobalResources.setHuntClues(clues);
    GlobalResources.setHuntname(hunt);
    GlobalResources.organiseClues();
    GlobalResources.setItemClicked(item);

    //System.gc();
    pd.dismiss();

    listener.onTaskCompleted();

}

public Bitmap getBitmapFromUrl(final String src) {

    final CountDownLatch latch = new CountDownLatch(1);
    final Bitmap[] bmp = new Bitmap[1];

    Thread thread = new Thread(){
        @Override
        public void run(){

            try {
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                bmp[0] = BitmapFactory.decodeStream(input, null, options);
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }

            // Causes latch.await() to stop blocking and carry on execution
            latch.countDown();
        }
    };
    thread.start();
    try {
        latch.await();
    }catch (InterruptedException e) {
        Log.e(TAG, "Thread error!");
    }

    return bmp[0];
}

}

0 个答案:

没有答案