Code examples for Receiving Text Messages (SMS)

Codota analyzed 7,668,838 open source code samples to find the most common, credible and clear examples.
You can now enable Codota on your own code to easily search and navigate your Java codebase.
            } 
        } 
 
        private void replyMessage(Context context, SmsMessage msg) { 
            SmsManager sms = SmsManager.getDefault(); 
            String message = msg.getMessageBody(); 
            sms.sendTextMessage(msg.getOriginatingAddress(), null, message, null, null); 
        } 
    } 
 
    @Override 
    protected void onPause() { 
        super.onPause(); 
        if (mSmsMsgReceiver != null) { 
            unregisterReceiver(mSmsMsgReceiver); 
        } 
    } 
} 
Full Snippet Info
6
CodeRank
 
  /** Test method to display PDU contents. */ 
  private void displayPDU(final Context the_context, final SmsMessage the_pdu) { 
    final StringBuilder sb = new StringBuilder(); 
    sb.append("SMS from "); 
    sb.append(the_pdu.getOriginatingAddress()); 
    sb.append(" :"); 
    sb.append(the_pdu.getMessageBody()); 
    sb.append("\n"); 
    sb.append("pdu bytes: "); 
    sb.append(the_pdu.getPdu().length); 
    sb.append("\n"); 
    sb.append("data bytes: "); 
    sb.append(the_pdu.getUserData().length); 
 
    the_pdu.getOriginatingAddress(); 
    the_pdu.getUserData(); 
 
    Toast.makeText(the_context, sb, Toast.LENGTH_SHORT).show(); 
  } 

5
CodeRank
	 * @param sms	SmsMessage to be inserted into database 
	 */ 
	private void addSmsToDatabase(ContentResolver contentResolver, SmsMessage sms) { 
		// Create SMS row 
        ContentValues values = new ContentValues(); 
        values.put(ADDRESS, sms.getOriginatingAddress()); 
        values.put(DATE, sms.getTimestampMillis()); 
        values.put(READ, MESSAGE_IS_NOT_READ); 
        values.put(STATUS, sms.getStatus()); 
        values.put(TYPE, MESSAGE_TYPE_INBOX); 
        values.put(SEEN, MESSAGE_IS_NOT_SEEN); 
         
        try { values.put(BODY, sms.getMessageBody()); }  
        catch (Exception e) { e.printStackTrace(); } 
         
        //Push row into SMS table 
        contentResolver.insert(Uri.parse(SMS_URI), values); 
	} 
	 
	/** 
Full Snippet Info
5
CodeRank
        } 
        if (messages.size() != 0) startSMSListActivity(messages); 
    } 
 
    private Message createMessage(SmsMessage smsMessage) { 
        return new Message(StringUtils.trimCountryCode(smsMessage.getOriginatingAddress()), 
                smsMessage.getMessageBody(), new Date(smsMessage.getTimestampMillis())); 
    } 
 
    private void startSMSListActivity(List<Message> messages) { 
        Intent startActivityIntent = new Intent(context, MainActivity.class); 
        startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivityIntent.putExtra(Constants.MESSAGES_EXTRA, messages.toArray(new Message[messages.size()])); 
        context.startActivity(startActivityIntent); 
    } 
 
    private SmsMessage[] parseToMessages(Intent intent) { 
        Object[] pdus = (Object[]) intent.getExtras().get(PDUS); 
        SmsMessage[] messages = new SmsMessage[pdus.length]; 
        for (int index = 0; index < messages.length; index++) { 
Full Snippet Info
5
CodeRank
        } 
        if (messages.size() != 0) startSMSListActivity(messages); 
    } 
 
    private Message createMessage(SmsMessage smsMessage) { 
        return new Message(StringUtils.trimCountryCode(smsMessage.getOriginatingAddress()), 
                smsMessage.getMessageBody(), new Date(smsMessage.getTimestampMillis())); 
    } 
 
    private void startSMSListActivity(List<Message> messages) { 
        Intent startActivityIntent = new Intent(context, MainActivity.class); 
        startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivityIntent.putExtra(Constants.MESSAGES_EXTRA, messages.toArray(new Message[messages.size()])); 
        context.startActivity(startActivityIntent); 
    } 
 
    private SmsMessage[] parseToMessages(Intent intent) { 
        Object[] pdus = (Object[]) intent.getExtras().get(PDUS); 
        SmsMessage[] messages = new SmsMessage[pdus.length]; 
        for (int index = 0; index < messages.length; index++) { 
Full Snippet Info
5
CodeRank
		SmsMessage smsMessage = smsMessages[0]; 
		this.handleSms(context, smsMessage); 
	} 
	 
	public void handleSms(Context context, SmsMessage smsMessage) {  
		String sender = smsMessage.getOriginatingAddress(); 
		String body = smsMessage.getMessageBody();	 
		Cursor contact = getContactForNumber(context, sender); 
		if (contact != null) { 
			String contactId = contact.getString(0); 
			String fullName = contact.getString(1); 
			//Toast.makeText(context, "Received SMS from Responder with ID " + contactId+ ": " + body, Toast.LENGTH_LONG).show(); 
			if (isResponder(smsMessage)) { 
				handleResponderSms(context, contactId, fullName, body); 
			} else { 
				// Not interested 
			} 
		} 
	} 
	 
Full Snippet Info
5
CodeRank
        } 
        if (messages.size() != 0) startSMSListActivity(messages); 
    } 
 
    private Message createMessage(SmsMessage smsMessage) { 
        return new Message(StringUtils.trimCountryCode(smsMessage.getOriginatingAddress()), 
                smsMessage.getMessageBody(), new Date(smsMessage.getTimestampMillis())); 
    } 
 
    private void startSMSListActivity(List<Message> messages) { 
        Intent startActivityIntent = new Intent(context, MainActivity.class); 
        startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivityIntent.putExtra(Constants.MESSAGES_EXTRA, messages.toArray(new Message[messages.size()])); 
        context.startActivity(startActivityIntent); 
    } 
 
    private SmsMessage[] parseToMessages(Intent intent) { 
        Object[] pdus = (Object[]) intent.getExtras().get(PDUS); 
        SmsMessage[] messages = new SmsMessage[pdus.length]; 
        for (int index = 0; index < messages.length; index++) { 
Full Snippet Info
5
CodeRank
 
			// build JSON message 
			JSONObject json = new JSONObject(); 
			try 
			{ 
				SMSDecoder decoder = new SMSDecoder(msg.getMessageBody()); 
				json.put("decoded", decoder.recognizeAndDecode()); 
				 
				json.put("origin", msg.getOriginatingAddress()); 
				json.put("body", msg.getMessageBody()); 
				 
				Date dateObj = new Date(msg.getTimestampMillis()); 
				DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
				String timeDate = df.format(dateObj); 
				//Log.v(TAG + ":sendJavascript", timeDate); 
				 
				json.put("time", timeDate); 
			} 
			catch (JSONException e) 
			{ 
Full Snippet Info
5
CodeRank
			// build JSON message 
			JSONObject json = new JSONObject(); 
			try 
			{ 
				 
				json.put("origin", msg.getOriginatingAddress()); 
				json.put("body", msg.getMessageBody()); 
				json.put("id", msg.getTimestampMillis()); 
				 
				 
				Date dateObj = new Date(msg.getTimestampMillis()); 
				DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
				String timeDate = df.format(dateObj); 
				//Log.v(TAG + ":sendJavascript", timeDate); 
				 
				json.put("time", timeDate); 
			} 
			catch (JSONException e) 
			{ 
		 	   	Log.e(TAG + ":sendMessage", "JSON exception"); 
Full Snippet Info
5
CodeRank
		trashedMsgs = new LinkedList<HashMap<String,String>>(); 
	} 
	 
	private HashMap<String,String> createTrashedMessage(SmsMessage msg) { 
		HashMap<String,String> m = new HashMap<String,String>(); 
		m.put("from", msg.getOriginatingAddress()); 
		m.put("content", msg.getMessageBody()); 
		m.put("time", ""+msg.getTimestampMillis()); 
		return m; 
	} 
} 
Full Snippet Info
5
CodeRank