1

I am implementation an android application. I am using web service on one activity. I am showing a progress dialog until it loads second Activity. But it does not show for whole time and shows black screen for a time.It looks like application hang. What should i do? i wasted my three day. I am using asynctask for these process. Please help me .

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();
     */
}
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
Nitesh Kabra
  • 71
  • 2
  • 4
  • 11

3 Answers3

2

for your case use this...

made progressDialog public to your 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();

but it is not a good approch use AsyncTask.

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • Thanks M Mohsin sir for your code but it showing same problem as previous one. I also posted code for performbackground process . Please suggest how asynctask task will be implemented for it. – Nitesh Kabra Jul 18 '12 at 06:23
  • what is time taking process in `performBackgroundProcess()` ?? I think you just launching a `Activity`? Where is `Web Service calling` for `AsyncTask` you need to read yourself :) If you face any problem then came again to ask..you are most welcome.. – Mohsin Naeem Jul 18 '12 at 06:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14041/discussion-between-m-mohsin-naeem-and-nitesh-kabra) – Mohsin Naeem Jul 18 '12 at 07:25
  • because it is fetching data from web services.and it will take time in this process. until some thing need to interact GUI. – Nitesh Kabra Jul 18 '12 at 08:49
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);
        }
    }
Nirali
  • 13,571
  • 6
  • 40
  • 53
0

This is how I implemented it. Copying the code here for others. Hopefully it works for you as well. You can copy this function "display_splash_screen()" and call it from your OnCreate() function (or whereever it is needed).

My Dialog (Splash Screen) R.layout.welcome_splash_screen is displayed, and then in a thread I dismiss the dialog after 3 secs.

`

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());
    }
}

`

Sudhanshu Gupta
  • 57
  • 1
  • 2
  • 5