Reputation: 622
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?
Upvotes: 1
Views: 65
Reputation: 10126
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);
Upvotes: 1
Reputation: 1334
Use AsyncTask instead of Thread https://developer.android.com/reference/android/os/AsyncTask.html
Android AsyncTask example and explanation
Upvotes: 2