0

I make project like this : Fetch Messages from Inbox

Part of code :

while  (cursor.moveToNext()) 
{
    String address = cursor.getString(1);
    String body = cursor.getString(3);

    // Loading too long
    System.out.println("======> Mobile number => "+address);
    System.out.println("=====> SMS Text => "+body);
    sms.add("Address=> "+address+"\n SMS => "+body);   
}
return sms;

But, there are too many message and need long time to show. While loding i want to show the animation (progressbar)

Thanks you...

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Arief Rivai
  • 228
  • 6
  • 19
  • I suggest you to do this with `AsynTask` That would very helpful one for you. Do this code in your `doInBackground()` method. Have a look at this [Example](http://www.androidsnippets.com/grab-a-url-source-with-progressdialog-and-asynctask) – Praveenkumar Jun 11 '12 at 07:20
  • Im very confused using AsynTask.. and example code hard from me. can you edit that code and use AsynTask? thanks 4 help me. Im newbie.. – Arief Rivai Jun 11 '12 at 15:22
  • Did you every try anyone of the answers below – Praveenkumar Jun 11 '12 at 15:24

4 Answers4

2

Usually you would use AsyncTask for such a thing on Android..

Intro: By default all code you write not using threads,services,etc.. will execute in UI thread.. That means that if you do some expensive work there, user interface will be blocked (not responsive). Good practise is to move such a expensive task to separate thread, otherwise you app will show ANR dialog and be closed.

How: One of the best approach for your case is use of AsyncTask.. Basically in onPreExecute() you show progress dialog or "long task in progress" animation. In doInBackground(Params... params) you put code of expensive operation. In case u need to update progress bar you do it from onProgressUpdate(Progress... values). When expensive task finishes it will provide results to onPostExecute(Result result) method. From this one you can update UI again and close previously displayed progress dialog.

Hope it helped. Cheers.. ;)

Ewoks
  • 12,285
  • 8
  • 58
  • 67
1

Use Asyntask, put that code in doInBackground , start processbar in onPreExecute() , dimiss in onPostExecute()

Tai Tran
  • 1,406
  • 3
  • 15
  • 27
0

Implement Runnable in your project. Run this code

while  (cursor.moveToNext()) 
{
String address = cursor.getString(1);
String body = cursor.getString(3);

// Loading too long
System.out.println("======> Mobile number => "+address);
System.out.println("=====> SMS Text => "+body);
sms.add("Address=> "+address+"\n SMS => "+body);   
}
//return sms;// Make a global variable which can hold values here...
handler.sendEmptyMessage(0);

in

run()

method. Now start the thread like this:

Thread fetchSmsThread = new Thread(this);
progressDialog = ProgressDialog.show(context,title,message);
fetchSmsThread.start();

Handler Code:

 Handler handler = new Handler()
 {

  //////Catch Message Here.. and Dismiss dialog...
  ///Note You have to declare progress dialog as global so that you can access it here...

 }

For further help about runnable follow this:

How to run a Runnable thread in Android?

If you dont want to use Runnable then use Android AsynClass, follow:

AsyncTask Android example

Community
  • 1
  • 1
Awais Tariq
  • 7,724
  • 5
  • 31
  • 54
0

Add a Progressbar in main.xml with an id (yourProgressBarId in the example below):

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    final ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);  

    final ProgressBar progressBar = (ProgressBar)findViewById(R.id.yourProgressBarId);
    progressBar.setVisibility(View.VISIBLE);
    new Thread(){
        public void run() {

                ArrayList sms =fetchInbox();
                if(sms!=null) 
                lViewSMS.post(new Runnable() {

                    @Override
                    public void run() {
                         ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sms);  
                         lViewSMS.setAdapter(adapter);  
                         progressBar.setVisibilty(View.GONE);
                }
          });
    }   
}.start();

}  
Zelleriation
  • 2,834
  • 1
  • 23
  • 23
  • i got error line this : ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item _1, sms); can u fix that? thanks.. – Arief Rivai Jun 11 '12 at 15:14
  • ArrayAdapter adapter = new ArrayAdapter(NameOfYourActivity.this, android.R.layout.simple_list_item_1, sms); Appreciate if you mark it as answered if it helps you :) – Zelleriation Jun 11 '12 at 17:39