我正在实现一个Android应用程序。我在一项活动上使用网络服务。我正在显示一个进度对话框,直到它加载第二个Activity。但它没有显示整个时间并且显示黑色屏幕一段时间。它看起来像应用程序挂起。我该怎么办?我浪费了三天。我正在使用asynctask进行这些过程。请帮帮我。
protected void onListItemClick(ListView l, View v, final int position,
long id) {
super.onListItemClick(l, v, position, id);
progressDialog = ProgressDialog.show(ProjectListActivity.this,
"Please wait...", "Loading...");
new Thread() {
public void run() {
try {
String project = titles.get(position - 1);
performBackgroundProcess(project);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
}
}.start();
}
private void performBackgroundProcess(String project) {
String spaceId = null;
String spaceName = null;
/*
* for (Space space : spaces){
* if(space.getName().equalsIgnoreCase((String) ((TextView)
* v).getText())){ spaceId = space.getId(); } }
*/
for (Space space : spaces) {
if (project.equals(space.getName())) {
newSpace = space;
}
}
spaceId = newSpace.getId();
spaceName = newSpace.getName();
/*
* Intent intent = new Intent(this, SpaceComponentsActivity.class);
* intent.putExtra("spaceId", spaceId); intent.putExtra("tabId", 0);
* intent.putExtra("className", "TicketListActivity"); TabSettings ts =
* new TabSettings(); ts.setSelTab(1); this.startActivity(intent);
*/
Intent intent = new Intent(this, SpaceComponentsActivity.class);
intent.putExtra("spaceId", spaceId);
intent.putExtra("tabId", 0);
intent.putExtra("spaceName", spaceName);
// intent.putExtra("className", "TicketListActivity");
TabSettings ts = new TabSettings();
ts.setSelTab(0);
ts.setSelTabClass("TicketListActivity");
this.startActivity(intent);
/*
* Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
* Toast.LENGTH_SHORT).show();
*/
}
答案 0 :(得分:2)
为您的情况使用此...
将progressDialog
公开发布到您的Activity
progressDialog = ProgressDialog.show(ProjectListActivity.this,
"Please wait...", "Loading...");
new Thread() {
public void run() {
try {
String project = titles.get(position - 1);
performBackgroundProcess(project);
ProjectListActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
});
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}.start();
但使用AsyncTask并不是一个好的方法。
答案 1 :(得分:1)
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
try {
Utilities.arrayRSS = objRSSFeed
.FetchRSSFeeds(Constants.Feed_URL);
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
// display UI
txtTitle.setText(Utilities.RSSTitle);
}
}
答案 2 :(得分:0)
这就是我实现它的方式。在这里为其他人复制代码。希望它也适合你。您可以复制此功能&#34; display_splash_screen()&#34;并从你的OnCreate()函数调用它(或者在需要的时候调用它)。
显示我的对话框(启动画面)R.layout.welcome_splash_screen,然后在一个帖子中我在3秒后解除对话框。
`
private void display_splash_screen() {
try{
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.welcome_splash_screen);
dialog.setTitle("Bulk SMS");
dialog.setCancelable(true);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Thread thrDialogClose = new Thread(){
@Override
public void run() {
super.run();
try {
// Let the dialog be displayed for 3 secs
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
};
thrDialogClose.start();
}});
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}catch (Exception ex){
Log.e("Bulk SMS:", ex.getStackTrace().toString());
}
}
`