Get sent sms from android device individually - android

I have implemented a code which gets me the first sms from "Sent SMS" in string format.
I want to get all the sms from the sent box one by one starting from the first.
I would like to save them in SQLite database.
The below code shows the first sent sms in Toast but how do i get all the sent sms?
public class MainActivity extends Activity {
String address, name, date, msg, type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
//Log.e("Count",count);
System.out.println("Count:" + count);
while (cursor1.moveToNext()){
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
}
}
Toast.makeText(getApplicationContext(), address + "\n" + name + "\n" + date + "\n" + msg + "\n" + type, Toast.LENGTH_LONG).show();
}
});
}
}

use
cursor.moveToFirst();
before line
while (cursor1.moveToNext())

instead of while use
if(cursor1.moveToFirst())
{
for (int i = 0; i <cursor1.getCount(); i++)
{
address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
cursor1.moveToNext();
}
}
and also close cursor at end

Related

Get Only Before 5 Minutes Inbox SMS

I want to select only latest inbox SMS on button click. Here is my code.
btnGet = (Button) findViewById(R.id.btnGet);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (fetchInbox() != null) {
ArrayList sms1 = fetchInbox();
for (int i = 0; i < sms1.size(); i++) {
String st = sms1.get(i).toString();
String[] sArr = st.split("\\$");
mobile = sArr[0];
sms = sArr[1];
useGet(mobile, sms);
}
} else {
textView1.setText("no sms");
}
} catch (Exception ex) {
textView1.setText("Exception" + ex.getMessage());
}
}
});
And here is my function to fetch SMS.
public ArrayList fetchInbox()
{
ArrayList sms = new ArrayList();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
String id = cursor.getString(0);
String address = cursor.getString(1);
String body = cursor.getString(3);
sms.add(address + "$" + body + "$" + id);
}
return sms;
}
I am able get all inbox SMS by this code, but I want to select only before 5 minutes inbox SMS. I am new with android apps.
Cursor cursor = getContentResolver().query (Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder)
selection : A filter declaring which rows to return. Passing null will
return all rows for the given URI.
As you can see, you can specifies the criteria for selecting rows.
First get the current date time.
Calendar date = Calendar.getInstance();
long t = date.getTimeInMillis();
Then subtract 5 mins from current time.
static final long ONE_MINUTE_IN_MILLIS = 60000;
Date afterSubtractingFiveMins = new Date(t - (5 * ONE_MINUTE_IN_MILLIS));
Now create the filter and query the messages.
String filter = "date>=" + afterSubtractingFiveMins.getTime();
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},filter,null,null);
PS: I didn't check the code. You may have to optimize.

Load Sms from inbox for specific number

I have implemented the code to get sms from inbox to my app.It gets all messages.But I want to load messages from specific number.I followed the tutorial from [Read all SMS from a particular sender it shows empty view.I worked out this code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
ListView list = (ListView) findViewById(R.id.listView1);
List<String> msgList = getSMS();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, msgList);
list.setAdapter(adapter);
}
public List<String> getSMS() {
List<String> sms = new ArrayList<String>();
// StringBuilder smsBuilder = new StringBuilder();
final String SMS_URI_INBOX = "content://sms/inbox";
final String SMS_URI_ALL = "content://sms/";
try {
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "address='5558'", null, null);
if (cur.moveToFirst()) {
int index_Address = cur.getColumnIndex("address");
int index_Person = cur.getColumnIndex("person");
int index_Body = cur.getColumnIndex("body");
int index_Date = cur.getColumnIndex("date");
int index_Type = cur.getColumnIndex("type");
do {
String strAddress = cur.getString(index_Address);
int intPerson = cur.getInt(index_Person);
String strbody = cur.getString(index_Body);
long longDate = cur.getLong(index_Date);
int int_Type = cur.getInt(index_Type);
sms.add("Number: " + strAddress + " .Message: " + strbody);
// smsBuilder.append("[ ");
// smsBuilder.append(strAddress + ", ");
// smsBuilder.append(intPerson + ", ");
// smsBuilder.append(strbody + ", ");
// smsBuilder.append(longDate + ", ");
//smsBuilder.append(int_Type);
// smsBuilder.append(" ]\n\n");
} while (cur.moveToNext());
if (!cur.isClosed()) {
cur.close();
cur = null;
}
else {
// smsBuilder.append("no result!");
} // end if
}} catch (SQLiteException ex) {
Log.d("SQLiteException", ex.getMessage());
}
return sms;
}
I passed address as my another emulator.If I gave null replacing address field of getContentResolver()it will load all sms in inbox.Can anyone help me where I have to modify ?
Use following code,
Uri uri = Uri.parse("content://sms/");
ContentResolver contentResolver = getContentResolver();
String phoneNumber = "+911234567890";
String sms = "address='"+ phoneNumber + "'";
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "body" }, sms, null, null);
System.out.println ( cursor.getCount() );
while (cursor.moveToNext())
{
String strbody = cursor.getString( cursor.getColumnIndex("body") );
System.out.println ( strbody );
}
Following permission is required,
<uses-permission android:name="android.permission.READ_SMS"/>
100% working code :
String[] projection = new String[] { "_id", "thread_id","address", "person", "body", "date", "type" };
Uri uri = Uri.parse("content://sms/");
Cursor c = cr.query(uri, projection,"address='9876543210'",null, "date desc");
int totalSMS = 0;
if (c != null) {
totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int j = 0; j < totalSMS; j++) {
String id = c.getString(c.getColumnIndexOrThrow(Telephony.Sms._ID));
String thread_id = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.THREAD_ID));
String smsDate = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.DATE));
String number = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.ADDRESS));
String body = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.BODY));
String type;
switch (Integer.parseInt(c.getString(c.getColumnIndexOrThrow(Telephony.Sms.TYPE)))) {
case Telephony.Sms.MESSAGE_TYPE_INBOX:
type = "inbox";
messageList.add(new Message(id, thread_id, number, body, smsDate, type));
break;
case Telephony.Sms.MESSAGE_TYPE_SENT:
type = "sent";
messageList.add(new Message(id, thread_id, number, body, smsDate, type));
break;
case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
break;
default:
break;
}
c.moveToNext();
}
}
c.close();

Access SMS Sent Message

Is there a way to access the sent message of the receiver?
I'm using smsmanager on android to sent message to a certain number and my objective now is that I'll create a method that will confirm me that the receiver has receive the message and show me what message he/she received.
sms type constants
MESSAGE_TYPE_ALL = 0;
MESSAGE_TYPE_INBOX = 1;
MESSAGE_TYPE_SENT = 2;
MESSAGE_TYPE_DRAFT = 3;
MESSAGE_TYPE_OUTBOX = 4;
MESSAGE_TYPE_FAILED = 5; // for failed outgoing messages
MESSAGE_TYPE_QUEUED = 6; // for messages to send later
use MESSAGE_TYPE_SENT=2 with condition
so use following code :
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "person", "date",
"body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
Log.e("Count",count);
while (cursor1.moveToNext()){
String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
if(type.equals("2")) // 2 for Sent Sms
{
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
}
}
}
You also need Following Permissions in your AndroidManifest.xml
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
Register a content observer.
SMSObserver smsSentObserver = new SMSObserver( new Handler() );
getContentResolver().registerContentObserver(Uri.parse("content://sms/out"), true, smsSentObserver);
public class SMSObserver extends ContentObserver
{
public SMSObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
querySMS();
}
}
protected void querySMS() {
Uri uriSMS = Uri.parse("content://sms/out");
Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent
String body = cur.getString(cur.getColumnIndex("body")); //content of sms
String add = cur.getString(cur.getColumnIndex("address")); //phone num
String time = cur.getString(cur.getColumnIndex("date")); //date
String protocol = cur.getString(cur.getColumnIndex("protocol")); //protocol
int type = Integer.parseInt(cur.getString(cur.getColumnIndex("type")));
}
this is the code i using for the similar function, hope it helps.
public class OutgoingSms {
SmsManager sms = SmsManager.getDefault();
ContentResolver contentResolver;
ContentObserver contentObserver;
String senderNum;
String message;
String type;
String smsContent;
contentResolver.registerContentObserver(Uri.parse("content://sms"), true, contentObserver);
contentObserver = new ContentObserver(new Handler()) {
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Uri smsURI = Uri.parse("content://sms/sent");
String[] strings = {"address", "body"};
Cursor c = getContentResolver().query(smsURI, null, null, null, null);
c.moveToNext();
senderNum = c.getString(c.getColumnIndex("address"));
message = c.getString(c.getColumnIndex("body"));
type = c.getString(c.getColumnIndex("type"));
if (type.equals("2"))
{
smsContent = "send sms to " + senderNum + " content is " + message1;
sms.sendTextMessage("+123456789", null, smsContent, null, null);
}
}
}
};

How to Delete last received SMS from inbox in Android

I am developing an Android app in which I want to replace message body of last received SMS with some new text.
I am using BroadcastReceiver in which I want to store the message body of last received SMS in a variable and delete the SMS from inbox and Now after deletion I want to put a new encoded message in the inbox.
Now the issue that I am facing is how to delete last received SMS from inbox. I have developed some code in this respect but It delete second last(previous) SMS from inbox. Please check my code below and help that I could continue my app, I would be very thankful to you for this act of kindness.
public void deleteLastSMS()
{
// abortBroadcast();
String body = null;
String num = null;
try
{
Uri uri = Uri.parse("content://sms/inbox");
Cursor c =contex.getContentResolver().query(uri, null, null ,null,null);
if(c.moveToFirst())
{
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
num = c.getString(c.getColumnIndexOrThrow("address")).toString();
}
int id = c.getInt(0);
int thread_id = c.getInt(1);
Uri thread = Uri.parse( "content://sms");
contex.getContentResolver().delete( thread, "thread_id=? and _id=?", new String[]{String.valueOf(thread_id), String.valueOf(id)} );
}
catch(CursorIndexOutOfBoundsException ee)
{
}
}
I've been looking at this since past hour, this is the stuff i found by far, hop i help :)
void deleteMessage(Context context){
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms, null,null,null,null);
int id = c.getInt(0);
int thread_id = c.getInt(1); //get the thread_id
context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
}
void deleteSMS(Context context){
Uri deleteUri = Uri.parse("content://sms");
context.getContentResolver().delete(deleteUri, "address=? and date=?", new String[] {strMessageFrom,strTimeStamp});
}
public void deleteSMS1(Context context, String message, String number) {
try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(
uriSms,
new String[] { "_id", "thread_id", "address", "person",
"date", "body" }, "read=0", null, null);
if (c != null && c.moveToFirst()) {
do {
long id = c.getLong(0);
long threadId = c.getLong(1);
String address = c.getString(2);
String body = c.getString(5);
String date = c.getString(3);
Log.e("log>>>",
"0>" + c.getString(0) + "1>" + c.getString(1)
+ "2>" + c.getString(2) + "<-1>"
+ c.getString(3) + "4>" + c.getString(4)
+ "5>" + c.getString(5));
Log.e("log>>>", "date" + c.getString(0));
if (message.equals(body) && address.equals(number)) {
// mLogger.logInfo("Deleting SMS with id: " + threadId);
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), "date=?",
new String[] { c.getString(4) });
Log.e("log>>>", "Delete success.........");
}
} while (c.moveToNext());
}
} catch (Exception e) {
Log.e("log>>>", e.toString());
}
}

Android SMS: group by its thread id

I am implementing a SMS App, till now i achieved to get all the messages(sent, received, drafts) with its contact number, thread id, contact id, date, type.
Here is my code:
Uri mSmsinboxQueryUri = Uri.parse("content://sms");
Cursor cursor = _context.getContentResolver().query(
mSmsinboxQueryUri,
new String[] { "_id", "thread_id", "address", "date", "body",
"type" }, null, null, null);
String[] columns = new String[] { "address", "thread_id", "date",
"body", "type" };
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String address = null, date = null, msg = null, type = null, threadId = null;
address = cursor.getString(cursor.getColumnIndex(columns[0]));
threadId = cursor.getString(cursor.getColumnIndex(columns[1]));
date = cursor.getString(cursor.getColumnIndex(columns[2]));
msg = cursor.getString(cursor.getColumnIndex(columns[3]));
type = cursor.getString(cursor.getColumnIndex(columns[4]));
Log.e("SMS-inbox", "\nTHREAD_ID: "
+ threadId + "\nNUMBER: " + address + "\nTIME: " + date + "\nMESSAGE: " + msg + "\nTYPE: " + type);
}
}
}
Now, I need to separate these messages by thread id (messages with same thread id). How can I best achieve that? Thanks!
I would not save those strings seperatly in the first place.
What I would do is a class like:
public class SmsMsg {
private String address = null;
private String threadId = null;
private String date = null;
private String msg = null;
private String type = null;
//c'tor
public SmsMsg(Cursor cursor) {
this.address = cursor.getString(cursor.getColumnIndex("address"));
this.threadId = cursor.getString(cursor.getColumnIndex("thread_id"));
this.date = cursor.getString(cursor.getColumnIndex("date"));
this.msg = cursor.getString(cursor.getColumnIndex("body"));
this.type = cursor.getString(cursor.getColumnIndex("type"));
}
}
now you can instantiate an object of SmsMsg in your while-loop as long cursor.moveToNext() is true and add it to a List of your choice.
You now could copy all messages of a desired threadId to a different List and sort it by date for example. That depends on what you want do do with it.

Categories

Resources