发送短信示例1
发送短信示例代码
1 public class SendMsgActivity extends Activity { 2 private EditText et_phone, et_content; 3 private Button bt; 4 5 @Override 6 public void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.main); 9 10 et_phone = (EditText) findViewById(R.id.et_phone); 11 et_content = (EditText) findViewById(R.id.et_content); 12 bt = (Button) findViewById(R.id.bt_send); 13 14 bt.setOnClickListener(new View.OnClickListener() { 15 16 @Override 17 public void onClick(View v) { 18 String mobile = et_phone.getText().toString(); 19 String content = et_content.getText().toString(); 20 21 SmsManager smsManager = SmsManager.getDefault(); 22 PendingIntent sentIntent = PendingIntent.getBroadcast( 23 SendMsgActivity.this, 0, new Intent(), 0); 24 25 if (content.length() >= 70) { 26 // 短信字数大于70,自动分条 27 List<String> ms = smsManager.divideMessage(content); 28 29 for (String str : ms) { 30 // 短信发送 31 smsManager.sendTextMessage(mobile, null, str, 32 sentIntent, null); 33 } 34 } else { 35 smsManager.sendTextMessage(mobile, null, content, 36 sentIntent, null); 37 } 38 39 Toast.makeText(SendMsgActivity.this, "发送成功!", Toast.LENGTH_LONG) 40 .show(); 41 } 42 }); 43 } 44 }
在AndroidManifest.xml文件中添加授权
<!-- 添加短信服务 --> <uses-permission android:name="android.permission.SEND_SMS" />