1

I ran into a problem and need some help. I want to remove some user's files after a button click and also show the files removing progress (in progressbar) and also show some Fancy UI. First i changed the layout after button click and hide some elements and visible the others. after that i called methods to remove files. The problem is that i can not see any UI changes and system hangs until all user file removed and after that based on my scenario it go to another activity. I've google around and found that i should use thread or UI thread but exactly don't know how. Here is my code :

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();

What is wrong with my code. Is there any better method to do that?

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Reza Tanzifi
  • 572
  • 4
  • 13

2 Answers2

2

Use AsyncTask instead of Thread https://developer.android.com/reference/android/os/AsyncTask.html

Android AsyncTask example and explanation

Community
  • 1
  • 1
sreejith v s
  • 1,334
  • 10
  • 17
  • 1
    AsyncTask enables proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.Please check the above links. – sreejith v s Apr 03 '17 at 10:37
1

Try

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);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • Thank you @Pehlaj but i have some errors in my code. new AsyncTask () give me the class anonymous class derived from asynctask must either be declare abstract or implement abstract method 'DoinBackground(params...)'. What is this error? – Reza Tanzifi Apr 03 '17 at 15:37
  • I played around with your code and it worked like a charm. Thank you man. – Reza Tanzifi Apr 03 '17 at 17:38
  • 1
    AsyncTask, first Void defines parameter type of doInBackground, second Void represents return type of doInBackground method & 3rd Void as input to onPostExecute – PEHLAJ Apr 04 '17 at 06:00
  • 1
    In your case, Void will be replaced with String as all three methods accept/return string types. I have updated the answer. Thanks – PEHLAJ Apr 04 '17 at 06:10