AsynchTask 在 postDelayed 处理程序为 运行 时冻结
AsynchTask freezing while a postDelayed handler is running
我是应用程序开发的新手,但我遇到了一个无法解决的问题。
我有一个启动画面,我用它来加载应用程序运行所需的各种东西(配置文件,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 语句中的代码。它似乎冻结了主线程,无论我把它放在哪里。我做错了什么吗?在此先感谢您的帮助。
此外,只要不涉及 post 延迟处理程序,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);
}
}
我是应用程序开发的新手,但我遇到了一个无法解决的问题。
我有一个启动画面,我用它来加载应用程序运行所需的各种东西(配置文件,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 语句中的代码。它似乎冻结了主线程,无论我把它放在哪里。我做错了什么吗?在此先感谢您的帮助。
此外,只要不涉及 post 延迟处理程序,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);
}
}