Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6057927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:32:32+00:00 2026-05-23T08:32:32+00:00

In my app i have a processDialog (using AsyncTask) and at doInbackground it fetches

  • 0

In my app i have a processDialog (using AsyncTask) and at doInbackground it fetches data from server.
all are working perfectly.
But I want if Internet is slow then it gives an warning alert that “problem with ur internet connection”
so for that i want to know how much time the progressDialog takes upto dismiss. so that i can put a condition that if time > 5imns then that alert will come.

How can i get how much time it takes or how to set time for dialog.

plz anybody answer me.
thank you in advance

below my code is there,

public class InfoActivity extends Activity {

private TextView tvRestroName, tvRestroAddr, tvRestroArea, tvRestroCity,
        tvRestroPhone, tvRestroRating, tvRestroCuisines, tvRestroAttr;
private ImageView imgRestro;
private ImageAdapter coverImageAdapter = new ImageAdapter(this);
private ScrollView sv;
static Bitmap restroBitmap, selectedImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    // This activity has two different views so to bind two views
    // LinearLayout is used here
    // from two views one is xml and another is coverflow class
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.info, null);

    // adding one view as xml
    ll.addView(view);

    CoverFlow coverFlow;
    coverFlow = new CoverFlow(this);
    coverFlow.setAdapter(new ImageAdapter(this));
    // set Imageadapter which is define within InfoActivity class
    coverFlow.setAdapter(coverImageAdapter);

    coverFlow.setSpacing(-25);
    coverFlow.setSelection(4, true);
    coverFlow.setAnimationDuration(1000);
    // and another view as class
    ll.addView(coverFlow);
    sv = new ScrollView(this);
    sv.addView(ll);

    // To add all views to InfoActivity
    setContentView(sv);
    // to initialize all variables like textview and imageview
    setupViews();
    // for progress dialog
    new DownloadTask().execute(this);

    // click event of images within imageadapter
    coverFlow.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            Toast.makeText(InfoActivity.this, "" + (position + 1),
                    Toast.LENGTH_SHORT).show();
            // imageView.setImageResource(mImageIds[arg2]);
            String slImg = TabHostActivity.galleryArray[position]; 
            selectedImage = loadBitmap(slImg);
            Intent i = new Intent(InfoActivity.this,
                    PostCommentActivity.class);
            startActivity(i);

        }
    });
}

// to initialize all variables like textview and imageview
private void setupViews() {
    imgRestro = (ImageView) findViewById(R.id.imageResturant);
    tvRestroName = (TextView) findViewById(R.id.tvRestroName);
    tvRestroAddr = (TextView) findViewById(R.id.tvRestroAddr);
    tvRestroArea = (TextView) findViewById(R.id.tvRestroArea);
    tvRestroCity = (TextView) findViewById(R.id.tvRestroCity);
    tvRestroPhone = (TextView) findViewById(R.id.tvRestroPhone);
    tvRestroRating = (TextView) findViewById(R.id.tvRestroRating);
    tvRestroCuisines = (TextView) findViewById(R.id.tvRestroCuisines);
    tvRestroAttr = (TextView) findViewById(R.id.tvRestroAttributes);
}

// set values to all fields
private void setupValues() {
    tvRestroName.setText("Restaurant " + TabHostActivity.restroName);
    //Bitmap bmp = loadBitmap(TabHostActivity.restroImageurl);
    System.out.println("str ;" + TabHostActivity.restroImageurl);

    imgRestro.setImageBitmap(Bitmap.createScaledBitmap(restroBitmap, 300, 300, true));
    System.out.println("Image seted");
    tvRestroAddr.setText("Address: " + TabHostActivity.addr);
    tvRestroArea.setText("Area: " + TabHostActivity.area);
    tvRestroCity.setText("City:" + TabHostActivity.city);

    String phones = "Phones: ";
    for (int i = 0; i < TabHostActivity.phoneArray.length; i++)
        phones += TabHostActivity.phoneArray[i] + ", ";
    tvRestroPhone.setText(phones.substring(0, phones.length() - 2));

    tvRestroRating.setText("Rating: " + TabHostActivity.rating);

    String cuisines = "Cuisines: ";
    for (int i = 0; i < TabHostActivity.cuisinesArray.length; i++)
        cuisines += TabHostActivity.cuisinesArray[i] + ", ";
    tvRestroCuisines.setText(cuisines.substring(0, cuisines.length() - 2));

    String attr = "Attributes: ";
    for (int i = 0; i < TabHostActivity.attributesArray.length; i++)
        attr += TabHostActivity.attributesArray[i] + ", ";
    tvRestroAttr.setText(attr.substring(0, attr.length() - 2));
}

// to get image from url in form of bitmap
private static Bitmap loadBitmap(String url) {
    URL myFileUrl = null;
    InputStream is = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
        Log.i("i m connected", "Download in info for main image");

    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.i("i m connected", "Download in info for main image");
    return BitmapFactory.decodeStream(is);
}

// for progressdialog
private class DownloadTask extends AsyncTask<Context, Integer, Void> {

    private ProgressDialog Dialog = new ProgressDialog(InfoActivity.this);

    @Override
    protected void onPreExecute() {
        System.out.println("In onPreExecute ");
        Dialog.setTitle("Loading");
        Dialog.setMessage("Please wait for few seconds...");
        //Dialog.setMax(100);
        //Dialog.setProgress(0);
        Dialog.show();

    }

    @Override
    protected Void doInBackground(Context... params) {
        System.out.println("In doInBackground ");
        //examineJSONFile();
        restroBitmap= loadBitmap(TabHostActivity.restroImageurl);
        System.out.println("conversion of bitmap done");
        int i = 0;

        System.out.println("In doInBackground ");
        Dialog.dismiss();
        //Dialog.cancel();
        for (i = 0; i < 10; i++)
            System.out.println("In doInBackground " + i);

        Bitmap bitmap = null;
        for (i = 0; i < TabHostActivity.galleryArray.length; i++) {
            try {
                publishProgress(i);

            } catch (Exception e) {
                Log.i("in while", e.getMessage());
            }

            bitmap = loadBitmap(TabHostActivity.galleryArray[i]);
            TabHostActivity.bitmapHash.remove(TabHostActivity.galleryArray[i]);
            TabHostActivity.bitmapHash.put(TabHostActivity.galleryArray[i], bitmap);

        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        if (progress[0]==0) {
            setupValues();
            System.out.println("setupvalues");
        } else {
            System.out.println("In progress ");
            coverImageAdapter.setnotify();
        }
    }

    @Override
    protected void onPostExecute(Void arg) {
        System.out.println("In onPostExecute ");
        }
}

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private BitmapDrawable drawable;
    private FileInputStream fis;

    public ImageAdapter(Context c) {
        mContext = c;
        }

    public int getCount() {
        return TabHostActivity.galleryArray.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public void setnotify() {
        notifyDataSetChanged();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        fetchDummy(TabHostActivity.galleryArray[position], i);
        i.setLayoutParams(new CoverFlow.LayoutParams(200, 200));
        i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        // Make sure we set anti-aliasing otherwise we get jaggies
        drawable = (BitmapDrawable) i.getDrawable();
        try {
            drawable.setAntiAlias(true);
        } catch (Exception e) {
            System.out.println("Exception in getview :" + e);
        }

        return i;

        // return mImages[position];
    }

    /**
     * Returns the size (0.0f to 1.0f) of the views depending on the
     * 'offset' to the center.
     */
    public float getScale(boolean focused, int offset) {
        /* Formula: 1 / (2 ^ offset) */
        return Math.max(1, 2.0f / (float) Math.pow(2, Math.abs(offset)));
    }

    private void fetchDummy(String urlString, ImageView i) {
        if (TabHostActivity.bitmapHash.containsKey(urlString)) {
            i.setImageBitmap(TabHostActivity.bitmapHash.get(urlString));
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:32:32+00:00Added an answer on May 23, 2026 at 8:32 am

    Just track the start time when you create the async task (either in the class creation, or in the onPreExecute()). Then every onProgressUpdate() check how long has occurred, and do your appropriate timeout logic.

    If you’re only using a simple spinner dialog for progress and not calling onProgressUpdate(), then you’re going to have to redesign your doInBackground to periodically call it, as you can only show UI changes in the onProgressUpdate() and not in the background thread.

    Edit: Comments on your uploaded code.

    Using BitmapFactory.decodeStream(is) means you’re not able to check the internet slowness easily.

    You could change this to load the stream into a byte[] in a read loop, and monitor the time it takes there, and then throw a timeout exception to the donInBackground() method if it’s going too slow. The doInBackground() can then trap the exception return an appropriate code to the onPostExecute().

    The onPostExecute() needs to take an int param, and decide what to do there, e.g. if you pass a code 0, it can assume everything was ok, and close the dialog. If it gets 1, then assume there was a timeout and close the dialog, followed by showing a “Your connection timed out” message.

    Roughly speaking the code would be (I haven’t tried this out):

            // ...
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            Log.i("i m connected", "Download in info for main image");
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        Log.i("i m connected", "Download in info for main image");
    
        // read the InputStream into an array
        long startTime = System.currentTimeMillis();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
    
        while ((nRead = is.read(data, 0, data.length)) != -1) {
          buffer.write(data, 0, nRead);
          if ((System.currentTimeMillis() - startTime) > 60000) {
              is.close();
              throw new MyTimeoutException();
          }
        }
        return BitmapFactory.decodeByteArray(buffer.toByteArray());
    }
    
    @Override
    protected Integer doInBackground(Context... params) {
        // ...
    
        Bitmap bitmap = null;
        for (i = 0; i < TabHostActivity.galleryArray.length; i++) {
            try {
                publishProgress(i);
    
            } catch (Exception e) {
                Log.i("in while", e.getMessage());
            }
    
            try {
                bitmap = loadBitmap(TabHostActivity.galleryArray[i]);
            } catch (MyTimeoutException e) {
                return RESULT_TIMEOUT;
            }
            TabHostActivity.bitmapHash.remove(TabHostActivity.galleryArray[i]);
            TabHostActivity.bitmapHash.put(TabHostActivity.galleryArray[i], bitmap);
    
        }
        return RESULT_OK;
    }
    
    @Override
    protected void onPostExecute(Integer resultCode) {
        if (resultCode == RESULT_OK) {
            // ... things were good, close dialog etc
        } else {
            // ... close spinner and show a "timeout" dialog
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a little app I am working on that scrapes comics from
I have an AsyncTask which launches a DatabaseHelper class from DoinBackground which copies an
I have an app and want to put the main thread to sleep for
I'm making an Android app where the user can download files from a FTP-server.
In my soundboard app I have created a context menu using this code. public
hello In my app I have a PayPal Pay Now button (working on Sandbox
In my app have a window splitted by a QSplitter, and I need to
On my rails app I have a list of items (like a task list)
For my Django app I have Events, Ratings, and Users. Ratings are related to
In my app I have 2 divs, one with a long list of products

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.