Android短信收发

须知少年凌云志,曾许人间第一流。这篇文章主要讲述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短信收发】

    推荐阅读