Java Code Examples for android.telephony.SmsMessage#createFromPdu()
The following examples show how to use
android.telephony.SmsMessage#createFromPdu() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmsReceiver.java From Locate-driver with GNU General Public License v3.0 | 6 votes |
private ArrayList<SmsMessage> getMessagesWithKeyword(String keyword, Bundle bundle) { ArrayList<SmsMessage> list = new ArrayList<SmsMessage>(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); for (int i = 0; i < pdus.length; i++) { SmsMessage sms = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { String format = bundle.getString("format"); sms = SmsMessage.createFromPdu((byte[]) pdus[i], format); } else { sms = SmsMessage.createFromPdu((byte[]) pdus[i]); } if (sms.getMessageBody().toString().equals(keyword)) { list.add(sms); } } } return list; }
Example 2
Source File: SmsReceiveJob.java From Silence with GNU General Public License v3.0 | 6 votes |
private Optional<IncomingTextMessage> assembleMessageFragments(Object[] pdus, int subscriptionId, MasterSecret masterSecret) { List<IncomingTextMessage> messages = new LinkedList<>(); for (Object pdu : pdus) { SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu); if (msg != null){ messages.add(new IncomingTextMessage(msg, subscriptionId, masterSecret == null)); } } if (messages.isEmpty()) { return Optional.absent(); } IncomingTextMessage message = new IncomingTextMessage(messages); if (WirePrefix.isPrefixedMessage(message.getMessageBody())) { return Optional.fromNullable(multipartMessageHandler.processPotentialMultipartMessage(message)); } else { return Optional.of(message); } }
Example 3
Source File: SMSBroadcastReceiver.java From BlackList with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") private SmsMessage[] getSMSMessages(Intent intent) { SmsMessage[] messages = null; Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus != null) { messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } else { String format = bundle.getString("format"); messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format); } } } } return messages; }
Example 4
Source File: SMSReceive.java From sana.mobile with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Automatically called when an SMS message is received. This method does * four things: 1) It reads the incoming SMS to make sure it is well formed * (it came from the dispatch server) 2) It stores the diagnosis * notification in the notification database 3) It pops up a temporary * message saying a diagnosis has been received 4) It inserts an alert into * the status bar, clearing all previous alerts in the status bar * <p> * A well-formed SMS message looks like this: <patient=422>Prescribe him * antibiotics. */ public void onReceive(Context context, Intent intent) { if (!intent.getAction().equals(ACTION)) return; Bundle bundle = intent.getExtras(); if (bundle == null) return; Object[] pdus = (Object[]) bundle.get("pdus"); if (pdus == null) return; for (int i = 0; i < pdus.length; ++i) { SmsMessage m = SmsMessage.createFromPdu((byte[]) pdus[i]); Log.i(TAG, "Got message from" + m.getOriginatingAddress()); Log.i(TAG, m.toString()); processMessage(context, m); } }
Example 5
Source File: SMSReceivedEvent.java From LibreTasks with Apache License 2.0 | 6 votes |
/** * Examines the Protocol Description Unit (PDU) data in the text message intent to reconstruct the * phone number and text message data. Caches the information in global variables in case they are * needed again.<br> * TODO(londinop): Further test this method with texts longer than 160 characters, there may be a * bug in the emulator */ private void getMessageData() { // TODO(londinop): Add text message data retrieval code and write a test for it Bundle bundle = intent.getExtras(); Object[] pdusObj = (Object[]) bundle.get("pdus"); // Create an array of messages out of the PDU byte stream SmsMessage[] messages = new SmsMessage[pdusObj.length]; for (int i = 0; i < pdusObj.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]); } // Get the sender phone number from the first message // TODO(londinop): Can there be multiple originating addresses in a single intent? phoneNumber = messages[0].getOriginatingAddress(); // Concatenate all message texts into a single message (for texts longer than 160 characters) StringBuilder sb = new StringBuilder(); for (SmsMessage currentMessage : messages) { sb.append(currentMessage.getDisplayMessageBody()); } messageText = sb.toString(); }
Example 6
Source File: ReadSmsService.java From AutoInputAuthCode with Apache License 2.0 | 6 votes |
@Nullable private SmsMessage[] getSmsUnder19(Intent intent) { SmsMessage[] messages; Bundle bundle = intent.getExtras(); // 相关链接:https://developer.android.com/reference/android/provider/Telephony.Sms.Intents.html#SMS_DELIVER_ACTION Object[] pdus = (Object[]) bundle.get("pdus"); if ((pdus == null) || (pdus.length == 0)) { return null; } messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } return messages; }
Example 7
Source File: MmsDatabase.java From weMessage with GNU Affero General Public License v3.0 | 6 votes |
protected Uri addSmsMessage(Object[] smsExtra){ if (!MmsManager.hasSmsPermissions()) return null; StringBuilder text = new StringBuilder(""); SmsMessage[] smsMessages = new SmsMessage[smsExtra.length]; for (int i = 0; i < smsMessages.length; i++){ SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]); text.append(sms.getMessageBody()); if (i == smsMessages.length - 1) { ContentValues values = new ContentValues(); values.put("address", sms.getOriginatingAddress()); values.put("body", text.toString()); values.put("date", sms.getTimestampMillis()); values.put("status", sms.getStatus()); values.put("read", 0); values.put("seen", 0); values.put("type", 1); return app.getContentResolver().insert(Uri.parse("content://sms/inbox"), values); } } return null; }
Example 8
Source File: SMSBroadcastReceiver.java From Android-9-Development-Cookbook with MIT License | 6 votes |
@Override public void onReceive(Context context, Intent intent) { if (SMS_RECEIVED.equals(intent.getAction())) { Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); String format = bundle.getString("format"); final SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format); } else { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } Toast.makeText(context, messages[0].getMessageBody(), Toast.LENGTH_SHORT) .show(); } } } }
Example 9
Source File: SmsDeliveryListener.java From Silence with GNU General Public License v3.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { JobManager jobManager = ApplicationContext.getInstance(context).getJobManager(); long messageId = intent.getLongExtra("message_id", -1); switch (intent.getAction()) { case SENT_SMS_ACTION: int result = getResultCode(); jobManager.add(new SmsSentJob(context, messageId, SENT_SMS_ACTION, result)); break; case DELIVERED_SMS_ACTION: byte[] pdu = intent.getByteArrayExtra("pdu"); if (pdu == null) { Log.w(TAG, "No PDU in delivery receipt!"); break; } SmsMessage message = SmsMessage.createFromPdu(pdu); if (message == null) { Log.w(TAG, "Delivery receipt failed to parse!"); break; } jobManager.add(new SmsSentJob(context, messageId, DELIVERED_SMS_ACTION, message.getStatus())); break; default: Log.w(TAG, "Unknown action: " + intent.getAction()); } }
Example 10
Source File: SmsReceiver.java From Phonegap-SMS with MIT License | 5 votes |
@Override public void onReceive(Context context, Intent intent) { Bundle extras=intent.getExtras(); //get the sms map if(extras!=null) { Object[] smsExtra=(Object[])extras.get(SMS_EXTRA_NAME); //get received sms array for(int i=0;i<smsExtra.length;i++) { SmsMessage sms=SmsMessage.createFromPdu((byte[])smsExtra[i]); if(isReceiving && callback_receive!=null) { String formattedMsg=sms.getOriginatingAddress()+">"+sms.getMessageBody(); PluginResult result=new PluginResult(PluginResult.Status.OK,formattedMsg); result.setKeepCallback(true); callback_receive.sendPluginResult(result); } } //if the plugin is active and we don't want to broadcast to other receivers if(isReceiving && !broadcast) { abortBroadcast(); } } }
Example 11
Source File: Messenger.java From experimental-fall-detector-android-app with MIT License | 5 votes |
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if ("android.provider.Telephony.SMS_RECEIVED".equals(action)) { Bundle bundle = intent.getExtras(); Object messages[] = (Object[]) bundle.get("pdus"); SmsMessage message[] = new SmsMessage[messages.length]; for (int i = 0; i < messages.length; i++) { message[i] = SmsMessage.createFromPdu((byte[]) messages[i]); } if (message.length > -1) { String contact = message[0].getOriginatingAddress(); String content = message[0].getMessageBody().toUpperCase(Locale.US); String[] items = content.split(";"); if (items.length > 1) { contact = items[1]; } if (Contact.check(context, contact)) { boolean prevent = false; if (content.contains("POSITION")) { Positioning.trigger(); prevent = true; } if (content.contains("ALARM")) { Alarm.siren(context); prevent = true; } if (prevent) { this.abortBroadcast(); } } } } }
Example 12
Source File: SmsReceiver.java From DevUtils with Apache License 2.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { try { Object[] pdus = (Object[]) intent.getExtras().get("pdus"); String originatingAddress = null; String serviceCenterAddress = null; if (pdus != null) { // 消息内容 String message = ""; // 循环拼接内容 for (Object object : pdus) { SmsMessage sms = SmsMessage.createFromPdu((byte[]) object); message += sms.getMessageBody(); // 消息内容 - 多条消息, 合并成一条 originatingAddress = sms.getOriginatingAddress(); serviceCenterAddress = sms.getServiceCenterAddress(); // 触发事件 if (sListener != null) { sListener.onMessage(sms); } } // 触发事件 if (sListener != null) { sListener.onMessage(message, originatingAddress, serviceCenterAddress); } } } catch (Exception e) { LogPrintUtils.eTag(TAG, e, "onReceive"); } }
Example 13
Source File: SmsReceiver.java From AirFree-Client with GNU General Public License v3.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { app = (ApplicationUtil) context.getApplicationContext(); Log.e("IP & PORT", "action:" + intent.getAction()); if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { Bundle bundle = intent.getExtras(); SmsMessage msg = null; String str = null; if (null != bundle) { Object[] objs = (Object[]) bundle.get("pdus"); for (Object obj : objs) { msg = SmsMessage.createFromPdu((byte[]) obj); str = "一条来自" + msg.getOriginatingAddress() + "的短信:" + msg.getDisplayMessageBody(); app.sendMessage("sms", str); } } } else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE); tm.listen(new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: Log.e("IP & PORT", "挂断"); break; case TelephonyManager.CALL_STATE_OFFHOOK: Log.e("IP & PORT", "接听"); break; case TelephonyManager.CALL_STATE_RINGING: Log.e("IP & PORT", "一则来自" + incomingNumber + "的电话:"); app.sendMessage("sms", "一则来自" + incomingNumber + "的电话: "); break; } } }, PhoneStateListener.LISTEN_CALL_STATE); } }
Example 14
Source File: SmsCompatibility.java From medic-gateway with GNU Affero General Public License v3.0 | 5 votes |
/** * @see https://developer.android.com/reference/android/telephony/SmsMessage.html#createFromPdu%28byte[],%20java.lang.String%29 */ @SuppressLint("ObsoleteSdkInt") // lint seems to think checking for > M is unnecessary: "Error: Unnecessary; SDK_INT is never < 16", but I think M is version 23 public static SmsMessage createFromPdu(Intent intent) { byte[] pdu = intent.getByteArrayExtra("pdu"); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { String format = intent.getStringExtra("format"); return SmsMessage.createFromPdu(pdu, format); } else { return SmsMessage.createFromPdu(pdu); } }
Example 15
Source File: SmsReceiver.java From AndroidDesignPatterns with Apache License 2.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { Bundle myBundle = intent.getExtras(); SmsMessage[] messages = null; String strMessageSender; String strMessageBody = ""; if (myBundle != null) { Object[] pdus = (Object[]) myBundle.get("pdus"); String format = myBundle.getString("format"); if (pdus != null) { messages = new SmsMessage[pdus.length]; } if (messages != null) { for (int i = 0; i < messages.length; i++) { /** * "createFromPdu" with extra parameter "format" was introduced for Android Marshmallow to support "3gpp" * for GSM/UMTS/LTE messages in 3GPP format or "3gpp2" for CDMA/LTE messages in 3GPP2 format. */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format); } else { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } strMessageSender = messages[i].getDisplayOriginatingAddress(); //Message Sender Name if (strMessageSender.equalsIgnoreCase(SMS_SENDER_NAME)) { //Whole message text. Extract OTP from strMessageBody strMessageBody = messages[i].getMessageBody(); } } } //Pass the text to listener interface. if (isAutoReadOtp) { mListener.messageReceived(strMessageBody); } } }
Example 16
Source File: SmsListener.java From Silence with GNU General Public License v3.0 | 5 votes |
private SmsMessage getSmsMessageFromIntent(Intent intent) { Bundle bundle = intent.getExtras(); Object[] pdus = (Object[])bundle.get("pdus"); if (pdus == null || pdus.length == 0) return null; return SmsMessage.createFromPdu((byte[])pdus[0]); }
Example 17
Source File: SmsReceiverPositionSender.java From geopaparazzi with GNU General Public License v3.0 | 4 votes |
@Override public void onReceive(final Context context, Intent intent) { if (intent.getAction().equals(SmsReceiverPositionSender.SMS_REC_ACTION)) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); boolean doCatch = preferences.getBoolean(LibraryConstants.PREFS_KEY_SMSCATCHER, false); if (!doCatch) { return; } boolean isGeopapsms = false; Bundle bundle = intent.getExtras(); String body = null; SmsMessage smsMessage = null; if (bundle != null) { if (Build.VERSION.SDK_INT >= 19) { //KITKAT SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent); smsMessage = msgs[0]; } else { Object tmp = bundle.get("pdus"); if (tmp instanceof Object[]) { Object[] pdus = (Object[]) tmp; smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]); } } } if (smsMessage != null) { body = smsMessage.getDisplayMessageBody(); if (body != null && smsMessage.getOriginatingAddress() != null) { if (GPLog.LOG) GPLog.addLogEntry(this, "Got message: " + body); if (body.toLowerCase().matches(".*geopap.*")) { isGeopapsms = true; } if (isGeopapsms) { String msg = null; String lastPosition = context.getString(R.string.last_position); msg = SmsUtilities.createPositionText(context, lastPosition); if (msg.length() > 160) { // if longer than 160 chars it will not work, try using only url msg = SmsUtilities.createPositionText(context, null); } if (GPLog.LOG) GPLog.addLogEntry(this, msg); String addr = smsMessage.getOriginatingAddress(); SmsUtilities.sendSMS(context, addr, msg, true); } } } } }
Example 18
Source File: SmsReceiver.java From android-common with Apache License 2.0 | 4 votes |
@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(); } }
Example 19
Source File: SmsReceiver.java From AndroidBase with Apache License 2.0 | 4 votes |
@Override public void onReceive(Context context, Intent intent) { try { if (LogUtils.getLogConfig().isEnable()) { LogUtils.i("收到广播:" + intent.getAction()); Bundle bundle = intent.getExtras(); for (String key : bundle.keySet()) { LogUtils.i( 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(); } }
Example 20
Source File: SmsReceiver.java From zone-sdk with MIT License | 4 votes |
@Override public void onReceive(Context context, Intent intent) { try { if (LogZSDK.INSTANCE.levelOK(LogLevel.i)) { LogZSDK.INSTANCE.i( "收到广播:" + intent.getAction()); Bundle bundle = intent.getExtras(); for (String key : bundle.keySet()) { LogZSDK.INSTANCE.i( 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(); } }