Reputation: 107
I am using ActionBar Tablistener with ViewPager and I have three fragments.
On launch my App is downloading my XML file data source from where my ListView in fragments is being populated.
How can I add as simple progress dialog while downloading my XML file(when there is slow internet connetion) in AsyncTask when I start application, so that my Activity runs after xml is downloaded?
Currently I'm using SystemClock.sleep(2760);
so there is enough time to download on normal connection speed...
My main Activity:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
TextView mSearchText;
ViewPager viewPager=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
final ActionBar actionBar=getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
addTabs(actionBar);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
Log.d("VIVZ","onPageScrolled "+i+" "+v+" "+i2);
}
@Override
public void onPageSelected(int i) {
actionBar.setSelectedNavigationItem(i);
Log.d("VIVZ","onPageSelected "+i);
}
@Override
public void onPageScrollStateChanged(int i) {
if(i==ViewPager.SCROLL_STATE_IDLE)
Log.d("VIVZ","onPageScrollStateChanged scroll state idle "+i);
if(i==ViewPager.SCROLL_STATE_DRAGGING)
Log.d("VIVZ","onPageScrollStateChanged scroll state dragging "+i);
if(i==ViewPager.SCROLL_STATE_SETTLING)
Log.d("VIVZ","onPageScrollStateChanged scroll state settling "+i);
}
});
if(isNetworkAvailable()){
Log.i("mobAppModel", "starting download Task");
AppDownloadTask download = new AppDownloadTask();
download.execute();
SystemClock.sleep(2760);
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private class AppDownloadTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... arg0) {
//Download the file
try {
Downloader.DownloadFromUrl("https://URL_on_kategorijeXML.xml", openFileOutput("XMLsource.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
}
Upvotes: 1
Views: 223
Reputation: 10876
AsyncTask have 3 main basic overridden method..
1) onPreExecute
2) doInBackGround
3) onPostExecute
onPreExcute get calls when AsyncTask start to run..before doInBackGround.. so here you can show your progress dialog..
doInBackground : here you can process with your service or any other long process. e.g. call webserivice. it's not work on UI thread.
onPostExecute : it gets called after doInbackGround complete.. so here you can hide progress bar. it's work on UI thread.
e.g.
private class DownloadFilesTask extends AsyncTask<Void,Void,Void> {
protected void onPreExecute() {
showDialog();
}
protected Void doInBackground(Void... args) {
//Download the file
try {
Downloader.DownloadFromUrl("https://URL_on_kategorijeXML.xml", openFileOutput("XMLsource.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Void result) {
//dismiss dialog..
}
}
Upvotes: 1
Reputation: 15775
Don't call SystemClock.sleep()
in your Activity.onCreate()
method, you'll block the UI completely and likely get your app to crash. You'll need to provide AsyncTask
method implementation for onPostExecute
. Your onPostExecute
will run on the UI thread, therefore it can call through to a method in your Activity
which dismisses your dialog and updates your adapter or whatever else. Consider using a loader backed by an AsyncTask
for this as it will be lifecycle aware. If you do not, you'll need to make sure your Activity
properly handles the lifecycle cleanup of the AsyncTask
(i.e. call AsyncTask.cancel()
.
Upvotes: 1
Reputation: 1430
Personally I would use a interface which is called on the postexecute of your asynctask, then start a progress dialog in your onpreexecute and then close it before you call your interface class on the post execute, implement this from your main task and run your next code when the event is fired.
eg
Main Class
public void taskdone();
Async Task
OnPostExecute
MyInterface.taskdone();
Interface
public taskdone();
Upvotes: 0