Note that there are some explanatory texts on larger screens.

plurals
  1. POshow progress dialog while waiting for receiving SMS
    text
    copied!<p>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.</p> <pre><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); } </code></pre> <p>}</p> <pre><code>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&lt;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"); } } } } </code></pre> <p>The logcat is as follows:</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload