我对应用程序开发还很陌生,但我有一个无法解决的问题。
我有一个启动屏幕,我用它来加载应用程序运行所需的各种内容(配置文件、来自互联网的 html),而后者给我带来了一个巨大的问题。这是代码
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;
}
}
给我带来问题的代码是 try 语句内部。无论我把它放在哪里,它似乎都会卡住主线程。我做错了什么吗?预先感谢您的帮助。
此外,只要不涉及后期延迟处理程序,getHTML 函数就会起作用。所以我认为这与此有关。
最佳答案
我认为这会起作用:
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);
}
}
关于java - 当 postDelayed 处理程序运行时,AsynchTask 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610160/