Issue
I'm pretty new to app development, but I have an issue I can't figure out.
I have a splash screen I am using to load various things that the app needs to function (config files, html from the internet) and the latter is giving me a huge problem. Here is the code
Document doc = null;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
//Fix loading data, dunno why this happens
try {
doc = new getHtml().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Utils.saveData(Splash.this, doc);
Intent mainIntent = new Intent(Splash.this, PocketFrameNavigation.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
public static class getHtml extends AsyncTask<Void, Void, Document> {
protected Document doInBackground(Void... args) {
try {
return Jsoup.connect(DROP_DATA_URL).get();
} catch(Exception e) {
return null;
}
}
protected void onPostExecute(Document html){
doc = html;
}
}
The code that's giving me an issue is the inside the try statement. It seems to freeze the main thread, no matter where I put it. Is there something I am doing terribly wrong? Thanks in advance for the help.
Also, the getHTML function works whenever there is not a post delayed handler involved. so i think it has something to do with that.
Solution
I think that will be work:
Document doc = null;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
new GetHTMLContent().excute();
}
private static class GetHTMLContent extends AsyncTask<Void, Void, Document> {
protected Document doInBackground(Void... args) {
try {
return Jsoup.connect(DROP_DATA_URL).get();
} catch(Exception e) {
return null;
}
}
protected void onPostExecute(Document html){
doc = html;
Utils.saveData(Splash.this, doc);
goToMainActivity();
}
}
private void goToMainActivity(){
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this, PocketFrameNavigation.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Answered By - Hoàng Vũ Anh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.