@Override public void onReceive(Context context, Intent intent) { try { if (Log.isPrint) { Log.i(TAG, "收到广播:" + intent.getAction()); Bundle bundle = intent.getExtras(); for (String key : bundle.keySet()) { Log.i(TAG, key + " : " + bundle.get(key)); } } Object[] pdus = (Object[]) intent.getExtras().get("pdus"); String fromAddress = null; String serviceCenterAddress = null; if (pdus != null) { String msgBody = ""; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) { for (Object obj : pdus) { SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj); msgBody += sms.getMessageBody(); fromAddress = sms.getOriginatingAddress(); serviceCenterAddress = sms.getServiceCenterAddress(); if (smsListener != null) { smsListener.onMessage(sms); } // Log.i(TAG, "getDisplayMessageBody:" + sms.getDisplayMessageBody()); // Log.i(TAG, "getDisplayOriginatingAddress:" + sms.getDisplayOriginatingAddress()); // Log.i(TAG, "getEmailBody:" + sms.getEmailBody()); // Log.i(TAG, "getEmailFrom:" + sms.getEmailFrom()); // Log.i(TAG, "getMessageBody:" + sms.getMessageBody()); // Log.i(TAG, "getOriginatingAddress:" + sms.getOriginatingAddress()); // Log.i(TAG, "getPseudoSubject:" + sms.getPseudoSubject()); // Log.i(TAG, "getServiceCenterAddress:" + sms.getServiceCenterAddress()); // Log.i(TAG, "getIndexOnIcc:" + sms.getIndexOnIcc()); // Log.i(TAG, "getMessageClass:" + sms.getMessageClass()); // Log.i(TAG, "getUserData:" + new String(sms.getUserData())); } } if (smsListener != null) { smsListener.onMessage(msgBody, fromAddress, serviceCenterAddress); } } } catch (Exception e) { e.printStackTrace(); } }
/** * Call requires API level 4 <uses-permission android:name="android.permission.SEND_SMS"/> * * @param phone * @param msg */ public static void sendMsgToPhone(String phone, String msg) { Log.i(TAG, "发送手机:" + phone + " ,内容: " + msg); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) { SmsManager manager = SmsManager.getDefault(); List<String> texts = manager.divideMessage(msg); for (String txt : texts) { manager.sendTextMessage(phone, null, txt, null, null); } } else { Log.e(TAG, "发送失败,系统版本低于DONUT," + phone + " ,内容: " + msg); } }