使用线程在android中应用UI更改

时间:2017-04-03 09:52:35

标签: java android android-layout

我遇到了一个问题,需要一些帮助。我想在单击按钮后删除一些用户的文件,并显示删除进度的文件(在进度条中)并显示一些花哨的UI。首先,我在按钮点击后更改了布局,隐藏了一些元素并可以看到其他元素。之后,我调用了删除文件的方法。问题是我看不到任何UI更改和系统挂起,直到所有用户文件被删除,之后基于我的场景它转到另一个活动。我已经谷歌了,发现我应该使用线程或UI线程,但完全不知道如何。这是我的代码:

new Thread() {
    public void run() {
        try {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ProgressBar spinner;
                    spinner = (ProgressBar) findViewById(R.id.progressBar);
                    listview.setVisibility(View.GONE);
                    spinner.setVisibility(View.VISIBLE);
                    preresult.setVisibility(View.VISIBLE);
                    resulttxt.setVisibility(View.VISIBLE);
                    wv.setVisibility(View.VISIBLE);
                    btnClear.setVisibility(View.GONE);
                    wv.loadUrl("file:///android_asset/rocket.gif");
                    resulttxt.setText("");
                }
            });
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}.start();

Thread b = new Thread() {
    @Override
    public void run() {
        Long TotalJunk = 0L;
        for (Apps social : checkedSocial) {
            if (social.getName() == "Telegram") {
                preresult.setText("Calculating Files :");
                resulttxt.setText("Telegram");
                preresult.setText("Removing Files...");
                clearMediashistory(social.path);
                TotalJunk = TotalJunk + social.junksize;
            }
                                           }
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("FreeUp", formatSize(TotalJunk));
        //commits your edits
        editor.commit();
    }
};
b.start();

我的代码出了什么问题。有没有更好的方法呢?

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

尝试

new AsyncTask<String, String, String> () {

    @Override
    protected void onPreExecute() {
        //show loader if requried
    }

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

        Long TotalJunk = 0L;
        for (Apps social : checkedSocial) {
            if (social.getName() == "Telegram") {
                preresult.setText("Calculating Files :");
                resulttxt.setText("Telegram");
                preresult.setText("Removing Files...");
                clearMediashistory(social.path);
                TotalJunk = TotalJunk + social.junksize;
            }
        }
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("FreeUp", formatSize(TotalJunk));
        //commits your edits
        editor.commit();
    }

    @Override
    protected void onPostExecute(String result){

        ProgressBar spinner;
        spinner = (ProgressBar) findViewById(R.id.progressBar);
        listview.setVisibility(View.GONE);
        spinner.setVisibility(View.VISIBLE);
        preresult.setVisibility(View.VISIBLE);
        resulttxt.setVisibility(View.VISIBLE);
        wv.setVisibility(View.VISIBLE);
        btnClear.setVisibility(View.GONE);
        wv.loadUrl("file:///android_asset/rocket.gif");
        resulttxt.setText("");
    }

}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTER);