tags:

views:

1161

answers:

3

Hi,

Can you please tell me how can i query the number of unread SMS in android programmically?

How can I implement the SMS unread count like this link: http://android.kanokgems.com/sms-unread-count/

+1  A: 

The API Docs show a constant that you should be able to look for to figure out which messages are received and unread.

This article shows somebody interacting with the SmsMessage class, which might give you some pointers.

Gunny
+2  A: 

Hello,

Here is a code snippet that lets you read messages as they arrive.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
        Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";

        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }

            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }

}

Lucas S.
Thank you. But I don't need to notify every time I get a SMS. I am interested in reading how many SMS are unread and how many voice mail are unread.
n179911
I understand, but it will be easy to modify this code to persist a count of every new messages that arrive, so then you can query it from your main activity.
Lucas S.
Thank you. I tried your code:mSMSReceiver = new SMSReceiver(); Intent registerReceiver = registerReceiver(mSMSReceiver, new IntentFilter(Intents.SMS_RECEIVED_ACTION));But when I test it by sending SMS in emulator, the onReceive() never get called.
n179911
You need to register your broadcast receiver in your manifest file first.
Lucas S.
A: 

Hi How can I query the number of OUTGOING (SENT) SMS in android??

please any give the Code for getting the Count for the SMS sent .

Dam