-6

I am creating a Sms spamming app that will send a user defined number of sms to a specific number.

for (i = counter; i > 0; i--) {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, message, null, null);
                Toast.makeText(getApplicationContext(), +i + " SMS Remaining", Toast.LENGTH_LONG).show();
            }

So i used a for loop. And made a stop button that should terminate this for loop. can anyone help out how to stop this for loop and app should go in its initial state. Because i tried "break" "System.exit(0)" everything. but the msgs doesnt stops sending until the counter is finished.

4

3 回答 3

2

You could define a volatile boolean variable like:

volatile boolean doStop = false;

And change your loop have additional check like:

 for (i = counter; i > 0 && !doStop; i--) {
    ..

And when you hit stop button, you set this variable to true like:

doStop = true;
于 2015-03-07T12:44:30.107 回答
1

First problem you would have with the current code is that once you have clicked the button it would result in lot of messages being sent even before user realizes that he sent that many messages, you need to induce a delay between each message.

The next problem is that your app could hang as your app would be executing the infinite loop of sending messages, executing the code in separate thread will help.

To resolve these two issues, you have to put the sending sms code in a new thread(use AsyncTask for that) and induce a delay after each message is sent and after the delay check if the stop button is clicked.

Below is the code for doing this:

class SendSMS extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                while (!dostop) { //if stop button is clicked stop the loop
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNo, null, message, null, null);
                    Toast.makeText(getApplicationContext(), +i + " SMS Remaining", Toast.LENGTH_LONG).show();                        Thread.sleep(5000);
                }
            } catch (Exception e) {

            }
            return null;
        }
    }

Usage: Call the above code after the send sms button is clicked.

send.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        dostop = false; //set to false first
                                        new SendSMS().execute();
                                    }
                                }
        );

When the stop button is clicked you need to set a variable dostop=true.

stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dostop = true;
            }
        });

Also I suggest adding some UI to show the count of sms sent.

于 2015-03-07T17:59:58.243 回答
0

I think that your problem isn't exiting the loop. Le loop will terminate too quickly.
The toasts are showed one by one. And you think that you can stop the loop but the loop is already done. Try to add a log message in the loop.

Use Android Log like that:

Log.d("MyTag", "SMS " + i);

Next, open logcat and you can filter by "MyTag".


Or you make your loop in the main thread and your UI is freezed, so the button isn't responding.

于 2015-03-07T17:18:18.423 回答