1, Listen to incoming SMS
1.1, Prepare manifest file
package="org.apache.sms">
Remark: action android.provider.Telephony.SMS_RECEIVED is undocumented.
1.2, Parse SMS
package org.apache.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
public class SMSApp extends BroadcastReceiver
{
private static final String LOG_TAG = "SMSApp";
/* package */
static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage message : messages)
{
String strFrom = message.getDisplayOriginatingAddress();
String strMsg = message.getDisplayMessageBody();
}
}
}
}
}
Reference:
http://davanum.wordpress.com/2007/12/15/android-listen-for-incoming-sms-messages/
2 Listen to outgoing SMS (Remark: following code is not tested)
2.1 Register observer for outgoing SMS
class SMSHandler extends Handler
{
public void handleMessage(Message msg)
{
//Handle message
}
}
class SMSObserver extends ContentObserver
{
private Handle m_handle = null;
public SMSObserver(Handle handle)
{
super(handle);
m_handle = handle;
}
public void onChange(boolean bSelfChange)
{
super.onChange(bSelfChange);
//Send message o Activity
Message msg = new Message();
msg.obj = "xxxxxxxxxx";
m_handle.sendMessage(msg);
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null)
onSMSSend();
else
onSMSReceive();
}
}
ContentResolver contentResolver = getContentResolver();
Handler handler = new SMSHandler();
ContentObserver m_SMSObserver = new SMSObserver(handler);
contentResolver.registerContentObserver(Uri.parse("content://sms/"),
true, m_SMSObserver); //Register to observe SMS in outbox,we can observe SMS in other location by changing Uri string, such as inbox, sent, draft, outbox, etc.)
2.2 Parse SMS
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null)
onSMSSend();
else
onSMSReceive();
3 Read and Delete All SMS
/*
//Available Uri string
String strUriInbox = "content://sms/inbox";//SMS_INBOX:1
String strUriFailed = "content://sms/failed";//SMS_FAILED:2
String strUriQueued = "content://sms/queued";//SMS_QUEUED:3
String strUriSent = "content://sms/sent";//SMS_SENT:4
String strUriDraft = "content://sms/draft";//SMS_DRAFT:5
String strUriOutbox = "content://sms/outbox";//SMS_OUTBOX:6
String strUriUndelivered = "content://sms/undelivered";//SMS_UNDELIVERED
String strUriAll = "content://sms/all";//SMS_ALL
String strUriConversations = "content://sms/conversations";//you can delete one conversation by thread_id
String strUriAll = "content://sms"//you can delete one message by _id
*/
String strUriInbox = "content://sms/inbox";
Uri uriSms = Uri.parse(strUriInbox); //If you want to access all SMS, just replace the uri string to "content://sms/"
Cursor c = mContext.getContentResolver().query(uriSms, null, null, null, null);
while (c.moveToNext())
{
try
{
//Read the contents of the SMS;
for(int i; i < c.getColumnCount(); i++)
{
String strColumnName = c.getColumnName(i);
String strColumnValue = c.getString(i);
}
//Delete the SMS
String pid = c.getString(1); //Get thread id;
String uri = "content://sms/conversations/" + pid;
mContext.getContentResolver().delete(Uri.parse(uri), null, null);
}
catch (Exception e)
{
}
}
REMEBER: must request following permission
1) Read SMS
2) Delete/Modify/Send SMS
in AndroidManifest.xml
1.1, Prepare manifest file
Remark: action android.provider.Telephony.SMS_RECEIVED is undocumented.
1.2, Parse SMS
package org.apache.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
public class SMSApp extends BroadcastReceiver
{
private static final String LOG_TAG = "SMSApp";
/* package */
static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(ACTION))
{
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
for (SmsMessage message : messages)
{
String strFrom = message.getDisplayOriginatingAddress();
String strMsg = message.getDisplayMessageBody();
}
}
}
}
}
Reference:
http://davanum.wordpress.com/2007/12/15/android-listen-for-incoming-sms-messages/
2 Listen to outgoing SMS (Remark: following code is not tested)
2.1 Register observer for outgoing SMS
class SMSHandler extends Handler
{
public void handleMessage(Message msg)
{
//Handle message
}
}
class SMSObserver extends ContentObserver
{
private Handle m_handle = null;
public SMSObserver(Handle handle)
{
super(handle);
m_handle = handle;
}
public void onChange(boolean bSelfChange)
{
super.onChange(bSelfChange);
//Send message o Activity
Message msg = new Message();
msg.obj = "xxxxxxxxxx";
m_handle.sendMessage(msg);
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null)
onSMSSend();
else
onSMSReceive();
}
}
ContentResolver contentResolver = getContentResolver();
Handler handler = new SMSHandler();
ContentObserver m_SMSObserver = new SMSObserver(handler);
contentResolver.registerContentObserver(Uri.parse("content://sms/"),
true, m_SMSObserver); //Register to observe SMS in outbox,we can observe SMS in other location by changing Uri string, such as inbox, sent, draft, outbox, etc.)
2.2 Parse SMS
Uri uriSMSURI = Uri.parse("content://sms");
Cursor cur = this.getContentResolver().query(uriSMSURI, null, null,
null, null);
cur.moveToNext();
String protocol = cur.getString(cur.getColumnIndex("protocol"));
if(protocol == null)
onSMSSend();
else
onSMSReceive();
3 Read and Delete All SMS
/*
//Available Uri string
String strUriInbox = "content://sms/inbox";//SMS_INBOX:1
String strUriFailed = "content://sms/failed";//SMS_FAILED:2
String strUriQueued = "content://sms/queued";//SMS_QUEUED:3
String strUriSent = "content://sms/sent";//SMS_SENT:4
String strUriDraft = "content://sms/draft";//SMS_DRAFT:5
String strUriOutbox = "content://sms/outbox";//SMS_OUTBOX:6
String strUriUndelivered = "content://sms/undelivered";//SMS_UNDELIVERED
String strUriAll = "content://sms/all";//SMS_ALL
String strUriConversations = "content://sms/conversations";//you can delete one conversation by thread_id
String strUriAll = "content://sms"//you can delete one message by _id
*/
String strUriInbox = "content://sms/inbox";
Uri uriSms = Uri.parse(strUriInbox); //If you want to access all SMS, just replace the uri string to "content://sms/"
Cursor c = mContext.getContentResolver().query(uriSms, null, null, null, null);
while (c.moveToNext())
{
try
{
//Read the contents of the SMS;
for(int i; i < c.getColumnCount(); i++)
{
String strColumnName = c.getColumnName(i);
String strColumnValue = c.getString(i);
}
//Delete the SMS
String pid = c.getString(1); //Get thread id;
String uri = "content://sms/conversations/" + pid;
mContext.getContentResolver().delete(Uri.parse(uri), null, null);
}
catch (Exception e)
{
}
}
REMEBER: must request following permission
1) Read SMS
2) Delete/Modify/Send SMS
in AndroidManifest.xml