您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
首先做好布局,然后在“发送”做点击事件,如下所示:
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(); ArrayList<String> divideMessage = smsManager.divideMessage(message); for(String text:divideMessage){ smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI); } }
这样就好了,最后不要忘记了在清单文件中配置短信的权限
<uses-permission android:name="android.permission.SEND_SMS" />
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。