满堂花醉三千客,一剑霜寒十四州。这篇文章主要讲述Android收发短信相关的知识,希望能为你提供帮助。
效果:点击发送短信开始发送短信
收到短信时将短信的内容显示出来
代码如下:
一、权限声明
< uses-permission android:name="android.permission.RECEIVE_SMS" /> < uses-permission android:name="android.permission.SEND_SMS" />
二、代码实现【Android收发短信】我们的短信功能用到了SmsManager和SmsMessage两个主要类。
当收不到短信时候,手动在应用管理中将应用的短信收发权限打开
//java /** * Created by Linda on 16/5/14. * 短信的收发 * 根绝国际标准 每条短信的长度不超过160 个字符 * 如果短信长度过长,可以调用sendMultipartTextMessage()分隔成多条短信来发送 */ public class MesaageActivity extends Activity { private TextView sender; private TextView content; private EditText to; //接收短信的手机号码 private EditText msgInput; //发送信息的内容private Button send; private IntentFilter receiveFilter; //短信接收private MessageReceiver messageReceiver; private IntentFilter sendFilter; //短信发送private SendStatusReceiver sendStatusReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.message); sender = (TextView) findViewById(R.id.sender); content = (TextView) findViewById(R.id.content); to = (EditText) findViewById(R.id.to); msgInput = (EditText) findViewById(R.id.msg_input); send = (Button) findViewById(R.id.send); //注册短信接收的广播 receiveFilter = new IntentFilter(); receiveFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); receiveFilter.setPriority(100); messageReceiver = new MessageReceiver(); registerReceiver(messageReceiver, receiveFilter); //注册监听短信发送的广播 sendFilter = new IntentFilter(); sendFilter.addAction("SENT_SMS_ACTION"); sendStatusReceiver = new SendStatusReceiver(); registerReceiver(sendStatusReceiver, sendFilter); //点击按钮发送短信 send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SmsManager smsManager = SmsManager.getDefault(); Intent sentIntent = new Intent("SENT_SMS_ACTION"); PendingIntent pi = PendingIntent.getBroadcast( MesaageActivity.this, 0, sentIntent, 0); //根据号码和内容发送短信(需要权限) smsManager.sendTextMessage(to.getText().toString(), null, msgInput.getText().toString(), pi, null); } }); }@Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(messageReceiver); unregisterReceiver(sendStatusReceiver); } //短信接收的广播 class MessageReceiver extends BroadcastReceiver {@Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Object[] pdus = (Object[]) bundle.get("pdus"); //获取短信数组 SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); //将pdu字符数组转换成SmsMessage对象 } String address = messages[0].getOriginatingAddress(); //获取短信发送方的号码 String fullMessage = ""; for (SmsMessage message : messages) { fullMessage += message.getMessageBody(); //获取短信的内容 } sender.setText(address); content.setText(fullMessage); abortBroadcast(); }} //监听广播是否发送成功的广播 class SendStatusReceiver extends BroadcastReceiver {@Override public void onReceive(Context context, Intent intent) { if (getResultCode() == RESULT_OK) { Toast.makeText(context, "Send succeeded", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(context, "Send failed", Toast.LENGTH_LONG) .show(); } }}}
推荐阅读
- DozerBeanMapper + 对象转Map方法
- Android中View滑动实现方式
- Spring3 MVC 注解---注解基本配置及@controller和 @RequestMapping 常用解释(转)
- glog日志库移植Android平台
- vue 单页面应用 app自适应方案
- Android EditText获取光标位置并插入字符删除字符
- app微信支付宝支付后台的插件模式+回调通过spring广播处理后续业务(已亲测可用)
- maven-webapp项目pom.xml配置
- Https系列之四(https的SSL证书在Android端基于okhttp,Retrofit的使用)