须知少年凌云志,曾许人间第一流。这篇文章主要讲述Android短信收发相关的知识,希望能为你提供帮助。
接收SMS类,代码如下
//for receive SMS private SmsReceiver mSmsReceiver; @Override protected void onResume() { super.onResume(); mSmsReceiver = new SmsReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); intentFilter.setPriority(1000); registerReceiver(mSmsReceiver, intentFilter); } @Override protected void onPause() { super.onPause(); unregisterReceiver(mSmsReceiver); } public class SmsReceiver extends BroadcastReceiver { public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.d(TAG,"action: "+action); if (SMS_RECEIVED_ACTION.equals(action)) { Bundle bundle = intent.getExtras(); //String messageContent = bundle.getString("message"); Object[] pdus = (Object[]) bundle.get("pdus"); //获取短信数组 SmsMessage[] messages = new SmsMessage[pdus.length]; String format = intent.getStringExtra("format"); for (int i = 0; i < messages.length; i++) { if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i],format); //将pdu字符数组转换成SmsMessage对象 } else { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } } String address = messages[0].getOriginatingAddress(); //获取短信发送方的号码 String fullMessage = ""; for (SmsMessage message : messages) { fullMessage += message.getMessageBody(); //获取短信的内容 } //sender.setText(address); //content.setText(fullMessage); Log.d(TAG,"SMS notes: "+fullMessage); abortBroadcast(); } } }
【Android短信收发】
推荐阅读
- Android 的cyclicBarrier
- DozerBeanMapper + 对象转Map方法
- Android收发短信
- Android中View滑动实现方式
- Spring3 MVC 注解---注解基本配置及@controller和 @RequestMapping 常用解释(转)
- glog日志库移植Android平台
- vue 单页面应用 app自适应方案
- Android EditText获取光标位置并插入字符删除字符
- app微信支付宝支付后台的插件模式+回调通过spring广播处理后续业务(已亲测可用)