1

我正在实现一个android应用程序。我在一项活动中使用网络服务。我正在显示一个进度对话框,直到它加载第二个活动。但它不会一直显示并显示一段时间黑屏。它看起来像应用程序挂起。我应该怎么办?我浪费了我的三天。我正在为这些过程使用 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();
     */
}
4

3 回答 3

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并不是一个好的方法。

于 2012-07-18T06:11:44.940 回答
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);
        }
    }
于 2012-07-18T06:16:20.427 回答
0

这就是我实现它的方式。在此处复制代码以供其他人使用。希望它也适用于你。您可以复制此函数“display_splash_screen()”并从 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());
    }
}

`

于 2015-07-28T03:24:06.033 回答