Android发送短信
首先做好布局,然后在“发送”做点击事件,如下所示:
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟主机、营销软件、网站建设、华蓥网站维护、网站推广。
String phoneNumber = editPhone.getText().toString().trim(); String message = editMessage.getText().toString().trim(); sendSMS(phoneNumber,message); private void sendSMS(String phoneNumber, String message) { String SENT_SMS_ACTION="SENT_SMS_ACTION"; String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; sendIntent = new Intent(SENT_SMS_ACTION); deliverIntent = new Intent(DELIVERED_SMS_ACTION); //返回发送的状态 sentPI = PendingIntent.getBroadcast(this, 0, sendIntent, 0); this.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(MainActivity.this, "短信发送成功", Toast.LENGTH_SHORT) .show(); editPhone.setText(""); editMessage.setText(""); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } } }, new IntentFilter(SENT_SMS_ACTION)); //返回接收的状态 deliverPI = PendingIntent.getBroadcast(this, 0, deliverIntent, 0); this.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { Toast.makeText(MainActivity.this, "收信人已经成功接收", Toast.LENGTH_SHORT) .show(); } }, new IntentFilter(DELIVERED_SMS_ACTION)); //发送短信 SmsManager smsManager =SmsManager.getDefault(); ArrayListdivideMessage = smsManager.divideMessage(message); for(String text:divideMessage){ smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI); } }
这样就好了,最后不要忘记了在清单文件中配置短信的权限
标题名称:Android发送短信
转载来源:http://hbruida.cn/article/jepigo.html