发送超过100条短信时Android应用程序强制关闭

时间:2011-07-24 02:00:07

标签: android

我正在尝试发送文本消息一定次数,当我做任何超过100的事情时,它强制关闭应用程序。有什么建议吗?

好的,所以这里是我的更新代码,现在有一些额外的问题 - 吐司永远不会消失,如果我启用我的if语句进行数据验证它将无法工作(基本上是尝试提示用户输入信息,如果它是null而不是仅强制关闭应用程序)

这是我更新的代码:

package net.learn2develop.SMS;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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();
                //variable for phone number
                String num = PhNumber.getText().toString();
                //variable for the amount of text messages to send.
                int max = Integer.parseInt(TxtCount.getText().toString());
                //variable to watch button and hide keyboard
                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                //test to see if count has any value at all
                //if (TxtCount.getText().toString().equals(""))
                //{
                //  Toast.makeText(getBaseContext(), "Please enter a number of times to nuke!", Toast.LENGTH_SHORT).show();
                //  return;
                //}
                //test to see if number has a value
                //else if (num.equals(""))
                //{
                //  Toast.makeText(getBaseContext(), "Please enter a phone number to nuke!", Toast.LENGTH_SHORT).show();
                //  return;
                //}
                //test to see if there is a message
                //else if (msg.equals(""))
                //{
                //  Toast.makeText(getBaseContext(), "Please enter a message!", Toast.LENGTH_SHORT).show();
                //  return;
                //}             

                //if all fields have valid data -- send text message until count = max                      
                while (count <= max) {                  
                    sendSMS(num, msg);
                    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)
    {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

        //when the sms has been sent
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS Nuked!", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break; 
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Nuked!",
                                    Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }
}

2 个答案:

答案 0 :(得分:0)

也许尝试拆分消息,或者通过计时器而不是循环发送消息,你可以尝试这样的事情:

               Thread background = new Thread (new Runnable() {
               public void run() {
                   Thread.sleep(100);
                           // Send message
               }
            });

答案 1 :(得分:0)

您可能应该安排服务在每封邮件发送(或失败)时发送广播,然后才发送下一封邮件。你可能正在淹没这项服务,导致部队关闭。

检查the docs如何使用“已发送的意图”。