4

I would know one thing about SMS...

I know that is possible read SMS in Android, but I don't know how I can read "Unread SMS", and in particular how to accede to each field like "sender", "SMS", "date", "time", "text".

What that i must do is: when a sms is received, an application "reads" the message and stores all the information in a data structure.

IMPORTANT: Another question: is it possible read "Unread SMS" in WhatsApp or other IM application (for example facebook messenger) ? And in this case how I can accede to each field ?

Regards

  • 1
    Check this out http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android – Rohit5k2 Feb 03 '15 at 15:17
  • I have already read this question, but missing the answer about the second part of my question :) –  Feb 03 '15 at 15:18
  • 1
    For facebook you need to implement their api. See this http://stackoverflow.com/questions/11486995/read-messages-in-facebook – Rohit5k2 Feb 03 '15 at 15:20
  • I guess my post answer all the three questions. – Rohit5k2 Feb 03 '15 at 15:23

2 Answers2

5

Reading Phone SMS

Check this out How can I read SMS messages from the device programmatically in Android?

Reading Facebook messages

For facebook you need to implement their API. See this Read Messages in Facebook

Reading Whatsapp messages

Option 1

Whatsapp did not publish any official APIs.

There's this open source API for communicating with whatsapp, it's not official and might stop working if Whatsapp update their protocols.

https://github.com/venomous0x/WhatsAPI

Regarding the legality of using this or other non-official API, it depends on the service agreement that you agreed to with Whatsapp. Read it and see if they frown upon using their communication protocols with clients other than theirs. My guess would be they do not allow it.

Option 2

WhatsApp makes a chat backup everyday at 4 AM on your SD Card. This is a single database encrypted with an AES key. Since it is on the external storage, your app can read it if you have access to the external storage.

You can easily decrypt this database (there is a paper available on this online).

However, this will only give you updated chats once every 24 hours.

If you want more realtime updates, your device has to be rooted, and you will need a completely new parser, as on the internal storage the chats are stored in more than one database.

Option 3

Read this SO question Get all messages from Whatsapp

NOTE: I am not sure about the Whatsapp stuff. It's just a compilation of various posts together.

Community
  • 1
  • 1
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
3

The easiest function

To read the sms I wrote a function that returns a Conversation object:

class Conversation(val number: String, val message: List<Message>)
class Message(val number: String, val body: String, val date: Date)

fun getSmsConversation(context: Context, number: String? = null, completion: (conversations: List<Conversation>?) -> Unit) {
        val cursor = context.contentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null, null)

        val numbers = ArrayList<String>()
        val messages = ArrayList<Message>()
        var results = ArrayList<Conversation>()

        while (cursor != null && cursor.moveToNext()) {
            val smsDate = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.DATE))
            val number = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.ADDRESS))
            val body = cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Sms.BODY))

            numbers.add(number)
            messages.add(Message(number, body, Date(smsDate.toLong())))
        }

        cursor?.close()

        numbers.forEach { number ->
            if (results.find { it.number == number } == null) {
                val msg = messages.filter { it.number == number }
                results.add(Conversation(number = number, message = msg))
            }
        }

        if (number != null) {
            results = results.filter { it.number == number } as ArrayList<Conversation>
        }

        completion(results)
    }

Using:

getSmsConversation(this){ conversations ->
    conversations.forEach { conversation ->
        println("Number: ${conversation.number}")
        println("Message One: ${conversation.message[0].body}")
        println("Message Two: ${conversation.message[1].body}")
    }
}

Or get only conversation of specific number:

getSmsConversation(this, "+33666494128"){ conversations ->
    conversations.forEach { conversation ->
        println("Number: ${conversation.number}")
        println("Message One: ${conversation.message[0].body}")
        println("Message Two: ${conversation.message[1].body}")
    }
}
Mickael Belhassen
  • 2,970
  • 1
  • 25
  • 46