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 8374413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:55:04+00:00 2026-06-09T14:55:04+00:00

I would like to write an app to show a progress dialog while waiting

  • 0

I would like to write an app to show a progress dialog while waiting to receive a SMS. When receiving SMS, the non-null TextView smsReceive is served as the condition to dismiss progress dialog. But it gets runtimeException. Could you show me what to change in the code.

package cx.opseng.PerfTOLD;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.TextView;

public class Sms extends Activity {
/** Called when the activity is first created. */

static TextView smsReceive;
static TextView smsReceiveChar;

@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sms);

    Intent i = getIntent();
    // Receiving the Data
    String reg = i.getStringExtra("reg");
    String port = i.getStringExtra("port");

    String smsMessage = reg + port ;

    // Show SMS sent message on screen 
    TextView smsSend = (TextView) findViewById(R.id.smsSend);
    smsSend.setText(smsMessage);
    Log.i("smsSend",String.valueOf(smsSend.getText()));
    // Send SMS message
    SmsManager sm = SmsManager.getDefault();
    String number = "5556";
    sm.sendTextMessage(number, null, smsMessage, null, null);

    ProgressDialog dialog = new ProgressDialog(Sms.this);
    dialog.setMessage("working hard......");
    dialog.setIndeterminate(true);
    dialog.setCancelable(true);
    dialog.show();

    // Receive SMS message
    smsReceive = null;
    smsReceive = (TextView) findViewById(R.id.smsReceive);
    Log.i("smsReceive",smsReceive.getText().toString());
    if (smsReceive.getText().toString() != null) {
        Log.i("smsReceive",smsReceive.getText().toString());
        String smsChar = smsReceive.getText().toString();
        Integer smsChar2 = smsChar.length();
        String smsChar3 = Integer.toString(smsChar2);
        Log.i("smsReceive2",smsChar3);
        smsReceiveChar.setText(smsChar);
        smsReceiveChar = (TextView) findViewById(R.id.smsReceiveChar);
    }

    if (smsReceiveChar.getText().toString() .equals("Message")) {
        dialog.dismiss();
    }

}
public static void updateMessageBox(String msg)
{
    smsReceive.append(msg);
    smsReceiveChar.append(msg);
}    

}

public class SmsReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent)
{
    Bundle bundle=intent.getExtras();

    Object[] messages=(Object[])bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];

    for(int n=0;n<messages.length;n++){
        sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
    }

    for(SmsMessage msg:sms){
        String num = msg.getOriginatingAddress();
        Log.i("SMS sender",num);
        if (num.equals("15555215556")) {
        Sms.updateMessageBox("\nFrom: " + msg.getOriginatingAddress() + 
                "\n" + "Message: " + msg.getMessageBody() + "\n");              }
    }
}
}

The logcat is as follows:

08-14 11:16:56.152: E/AndroidRuntime(2091): FATAL EXCEPTION: main
08-14 11:16:56.152: E/AndroidRuntime(2091): java.lang.RuntimeException: Unable to start activity ComponentInfo{cx.opseng.PerfTOLD/cx.opseng.PerfTOLD.Sms}: java.lang.StringIndexOutOfBoundsException
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.os.Looper.loop(Looper.java:123)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at java.lang.reflect.Method.invoke(Method.java:521)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at dalvik.system.NativeStart.main(Native Method)
08-14 11:16:56.152: E/AndroidRuntime(2091): Caused by: java.lang.StringIndexOutOfBoundsException
08-14 11:16:56.152: E/AndroidRuntime(2091):     at java.lang.String.substring(String.java:1579)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at cx.opseng.PerfTOLD.Sms.onCreate(Sms.java:88)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-14 11:16:56.152: E/AndroidRuntime(2091):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-14 11:16:56.152: E/AndroidRuntime(2091):     ... 11 more
  • 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-06-09T14:55:05+00:00Added an answer on June 9, 2026 at 2:55 pm

    I made an example which works for me.

    I had issues with the progress dialog not working which was caused by my use of threading.

    AndroidManifest.xml:

    <manifest package="com.mw.stackoverflow.example02"
        android:versionCode="1"
        android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
    
        <uses-sdk
            android:minSdkVersion="10"
            android:targetSdkVersion="15" />
        <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    
    
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    MainActivity.java:

    package com.mw.stackoverflow.example02;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.IntentFilter;
    
    public class MainActivity extends Activity implements SmsUpdater
    {
        private static final String TAG = "MainActivity";
    
        private SmsReceiver mSmsReceiver;
    
        private class ExaminMessagesTask extends AsyncTask<SmsMessage, Void, Boolean> 
        {
            ProgressDialog mProgressDialog;
    
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
    
                mProgressDialog = new ProgressDialog(MainActivity.this);
                mProgressDialog.setMessage("Receiving SMS.");
                mProgressDialog.setIndeterminate(true);
                mProgressDialog.setCancelable(true);
    
                mProgressDialog.show();
            }
    
    
            protected Boolean doInBackground(SmsMessage... messages) 
            {
                if (messages.length > 0)
                {
                    Log.d(TAG, "Showing progress dialog.");
    
                }
    
                for (SmsMessage m: messages)
                {
                    Log.d(TAG, "Message received");
                }   
    
                int count = 0;
                for(SmsMessage msg:messages)
                {
                    String num = msg.getOriginatingAddress();
                    Log.d(TAG, num);
    
                    mProgressDialog.setTitle("Processing message " + (count++));
    
                    try 
                    {
                        Thread.sleep(1000);
                    }    
                    catch (InterruptedException e) 
                    {
                    }
                }
    
                return true;
            }
    
            protected void onProgressUpdate(Integer... progress) 
            {
    
            }   
    
            protected void onPostExecute(Boolean result) 
            {
                mProgressDialog.dismiss();
            }
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mSmsReceiver = new SmsReceiver(this);
        }
    
        @Override
        protected void onResume() 
        {
            super.onResume();
            IntentFilter filter = new IntentFilter();
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
            registerReceiver(mSmsReceiver, filter);
        }
    
        @Override
        public void onPause() 
        {
            super.onPause();
            unregisterReceiver(mSmsReceiver);
        }
    
        @Override
        public void gotSms(final SmsMessage[] m) 
        {
            Log.d(TAG, "OnReceive, number of messages: " + m.length);
    
            runOnUiThread(new Runnable() 
            {
                public void run() 
                {
                    Toast.makeText(MainActivity.this, "SMS received", Toast.LENGTH_SHORT).show();
    
                    new ExaminMessagesTask().execute(m);
                }   
            });
        }
    
    }
    

    SmsReceiver.java:

    package com.mw.stackoverflow.example02;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.support.v4.app.NavUtils;
    import android.telephony.SmsMessage;
    
    class SmsReceiver extends BroadcastReceiver
    {
        private static final String TAG = "SMSREC";
    
        private SmsUpdater mSmsUpdater;
    
        public SmsReceiver(SmsUpdater u)
        {
            super();
            mSmsUpdater = u;
        }
    
        public void onReceive(Context context, Intent intent)
        {
            Log.d(TAG, "OnReceive called.");
    
            Bundle bundle=intent.getExtras();
    
            Object[] messages=(Object[])bundle.get("pdus");
            SmsMessage[] sms = new SmsMessage[messages.length];
            for(int n=0;n<messages.length;n++)
            {
                sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
            }
    
            mSmsUpdater.gotSms(sms);
        }
    }
    

    SmsUpdater.java

    package com.mw.stackoverflow.example02;
    
    import android.telephony.SmsMessage;
    
    public interface SmsUpdater 
    {
        void gotSms(SmsMessage[] m);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to show progress bar in my swing app, which will zip
I would like to write an app for the iPad but I can't stand
I would like to show a progress bar indicating how much of a file
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write a function that creates a plot of a set
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write a League Fixture generator in python, but I can't.
I would like to write to functional test for one of my rails 3
I would like to write a cross-platform function in C++ that contains system calls.

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.