由于需要,要用手机给其他某个手机批量发送短信,于是想到了写一android应用来实现这一需要。
其中,主要代码如下(通过for循环发送短信)
public class SendManyMessage extends Activity { PendingIntent paIntent; SmsManager smsManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_many_message); TextView backButton = (TextView)findViewById(R.id.back_button); TextView selectANumber = (TextView)findViewById(R.id.select_a_number); final EditText telNumber = (EditText)findViewById(R.id.tel_number); TextView sendMes = (TextView)findViewById(R.id.send_mes); final EditText mesContent = (EditText)findViewById(R.id.mes_content); final EditText telNum = (EditText)findViewById(R.id.tel_num); backButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); selectANumber.setOnClickListener(new OnClickListener() { @Override //打开通讯录选取号码 public void onClick(View v) { Intent i = new Intent(Intent.ACTION_PICK); i.setType("vnd.android.cursor.dir/phone"); startActivityForResult(i, 0); } }); sendMes.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { paIntent = PendingIntent.getBroadcast(SendManyMessage.this, 0, new Intent(), 0); smsManager = SmsManager.getDefault(); String message = ""; int num = 1; int which = 0; String phoneNumber = telNumber.getText().toString(); message = mesContent.getText().toString(); try{ num = Integer.parseInt(telNum.getText().toString()); }catch(Exception e){ num = 1; } if(message.trim().equals("")){ AlertDialog.Builder builder = new Builder(SendManyMessage.this); builder.setMessage("信息不能为空!"); builder.setTitle("提示"); builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); }else{ //通过for循环批量发送短信代码块 for(int i = 0;i < num;i++){ message = mesContent.getText().toString(); which = i + 1; message = message + "(" + which + ")"; if(phoneNumber.length() < 11){ AlertDialog.Builder builder = new Builder(SendManyMessage.this); builder.setMessage("号码格式不对!"); builder.setTitle("提示"); builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); }else{ smsManager.sendTextMessage(phoneNumber, null,message, paIntent, null); } } } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); EditText telNumber = (EditText)findViewById(R.id.tel_number); switch (requestCode) { case 0: if (data == null) { return; } Uri uri = data.getData(); Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); String number = cursor.getString(cursor.getColumnIndexOrThrow(Phones.NUMBER)); Log.d("tag", "number" + number); telNumber.setText(number); break; default: break; } } }
参考地址:http://blog.csdn.net/freestyleboy21/article/details/6849869
http://zhidao.baidu.com/link?url=K0UMZG1sFMAp2_zBcf14TnyaAWI57PQ4k18vKZ8FVNyvH-zM8ixWdIfsXtl7wwGEZWolkNxRI6oNR4V5s4iAOK