Search sms Inbox - android

How can I search the sms inbox and show latest message from a special number. For example search for "999999999" and show last message received from this number. Is any way to do this?
I have use this code to return the count of messages my inbox
TextView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = (TextView) findViewById(R.id.textView1);
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
int unreadMessagesCount = c.getCount();
c.deactivate();
view.setText(String.valueOf(unreadMessagesCount));

If you only want the last message sent by a specific number use this:
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(SMS_INBOX, null, "address = ?",
new String[] {"9999999999"}, "date desc limit 1");
if(cursor.moveToFirst()) {
// Do something
Log.v("Example", cursor.getString(cursor.getColumnIndex("body")));
}
cursor.close();
The second answer to How many database columns associated with a SMS in android? is a great reference for different columns in an SMS.

try as:
final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(SMS_INBOX,null,null, null,"DATE desc");
if(c.moveToFirst()){
for(int i=0;i<c.getCount();i++){
String body= c.getString(c.getColumnIndexOrThrow("body")).toString();
String number=c.getString(c.getColumnIndexOrThrow("address")).toString();
if(number=="999999999")
{
//get message body here
break;
}
else
c.moveToNext();
}
}
c.close();

Related

My phone number in mms address field of android conversation

I develop simple sms/mms client. With sms everything work good, but with mms I have problem related to address field in conversation.
I have next method to load conversations. There are last sms and mms.
public static List<Conversation> getConversations(Context c) {
List<Conversation> conversations = new ArrayList<>();
Conversation conversation;
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor cursor = c.getContentResolver().query(uri, null, null, null, "normalized_date DESC");
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
conversation = new Conversation();
conversation.setId(cursor.getString(cursor.getColumnIndexOrThrow("_id")));
conversation.setThreadID(cursor.getString(cursor.getColumnIndexOrThrow("thread_id")));
conversation.setDate(new Date(Long.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("date")))));
conversation.setReadType(ReadType.values()[Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("read")))]);
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
if (isSMS(type)) {
conversation.setBody(cursor.getString(cursor.getColumnIndexOrThrow("body")));
conversation.setNumber(cursor.getString(cursor.getColumnIndexOrThrow("address")));
conversation.setMessageFromType(MessageFromType.values()[Integer.parseInt(type) - 1]);
} else {
Map<String, String> mmsContent = getMMSByID(c, conversation.getId());
conversation.setBody(mmsContent.get("body"));
conversation.setNumber(mmsContent.get("address"));
}
conversations.add(conversation);
cursor.moveToNext();
}
}
cursor.close();
return conversations;
}
The problem is that when I sent mms, my number was put to address field of conversation. With sms everything is okay. So after that I don't know who is my opponent in chat.
Also I load mms number in the next way
private String getNumber(Context c, String mmsdid) {
String add = "";
final String[] projection = new String[]{"address", "contact_id"};
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(String.valueOf(mmsid)).appendPath("addr");
Cursor cursor = c.getContentResolver().query(
builder.build(),
projection,
null,
null, null);
if (cursor.moveToFirst()) {
add = cursor.getString(cursor.getColumnIndex("address"));
}
return add;
}
Maybe someone have the same problem? Or have any suggestions how to solve it?
The answer was pretty easy, hope it may help someone. Everything is okay except one part. In selection parameter I should add
"from=151"
or
"from="+PduHeaders.TO
So code will look like this:
private static String getNumber(Context c, String id) {
String add = "";
final String[] projection = new String[]{"address"};
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(String.valueOf(id)).appendPath("addr");
Cursor cursor = c.getContentResolver().query(
builder.build(),
projection,
"type="+PduHeaders.TO,
null, null);
if (cursor.moveToFirst()) {
add = cursor.getString(cursor.getColumnIndex("address"));
}
cursor.close();
return add;
}

Android ContactsContract with AutocompleteTextview too slow

Am trying to use the ContactsProvider with my AutoCompleteTextview using a method that fetches the data (name and phone number) and stores them in a list. As expected, this method will always take time to complete as am calling the method in the onCreateView method of my Fragment class.
This is the method:
...
ArrayList<String> phoneValues;
ArrayList<String> nameValues;
...
private void readContactData() {
try {
/*********** Reading Contacts Name And Number **********/
String phoneNumber = "";
ContentResolver contentResolver = getActivity()
.getContentResolver();
//Query to get contact name
Cursor cursor = contentResolver
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
// If data data found in contacts
if (cursor.getCount() > 0) {
int k=0;
String name = "";
while (cursor.moveToNext())
{
String id = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//Check contact have phone number
if (Integer
.parseInt(cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
//Create query to get phone number by contact id
Cursor pCur = contentResolver
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { id },
null);
int j=0;
while (pCur
.moveToNext())
{
// Sometimes get multiple data
if(j==0)
{
// Get Phone number
phoneNumber =""+pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Add contacts names to adapter
autocompleteAdapter.add(name);
// Add ArrayList names to adapter
phoneValues.add(phoneNumber.toString());
nameValues.add(name.toString());
j++;
k++;
}
} // End while loop
pCur.close();
} // End if
} // End while loop
} // End Cursor value check
cursor.close();
} catch (Exception e) {
Log.i("AutocompleteContacts","Exception : "+ e);
}
}
Am sure there is a better way to accomplish this, but this method works and the suggestions are presented when I type into the AutocompleteTextview. Am just worried about the time it takes. How can I accomplish this without populating an ArrayList?
I have looked at this question: Getting name and email from contact list is very slow and applied the suggestions in the answer to my code, but now nothing is suggested when I type.How can I improve the performance of my current code?
This is how i have implemented AutoCompleteTextView and it works fine for me ..
final AutoCompleteTextView act=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayList<String> alContacts = new ArrayList<String>();
ArrayList<String> alNames= new ArrayList<String>();
ContentResolver cr = this.getContentResolver(); //Activity/Application android.content.Context
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
alNames.add(contactName);
alContacts.add(contactNumber);
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
String[] array=new String[alNames.size()];
array=(String[]) alNames.toArray(array);
ArrayAdapter<String> myArr= new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,array);
act.setAdapter(myArr);
act.setThreshold(1);
I got rid of the slow response by placing the method inside an AsynTask.
public class AutocompleteAsyncTask extends AsyncTask<Void, Void, Void> {
public Void doInBackground(Void...params) {
try {
/*********** Reading Contacts Name And Number **********/
String phoneNumber = "";
ContentResolver contentResolver = getActivity()
.getContentResolver();
//Query to get contact name
Cursor cursor = contentResolver
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
// If data data found in contacts
if (cursor.getCount() > 0) {
int k=0;
String name = "";
while (cursor.moveToNext())
{
//...Rest of the same code as above
and then calling this in my onCreateView():
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
new AutocompleteAsyncTask().execute();
return rootView;
}
Now the task of inflating my view and fetching the data are separated into two different threads.
The CursorLoader option mentioned by Eugen Pechanec is kinda the best option, so I'll update this answer with that option when I can.

how to read last sms

Using the code below i wrote in TextView "textStareLed" first unreaded message. How can write the last message?
I try to use the cursor.moveToPrevious() but didn`t work.
protected void AfisareStareLed() {
TextView view = new TextView(this);
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSMSURI, null, null, null, null)
while (cursor.moveToNext()){
if(cursor.getString(2).equals("+4xxxxx") && cursor.getString(8).equals("0")){//cursor.getString(8)==0 <=> "read=0"
TextView textStareLed = (TextView) findViewById(R.id.textStareLed);
textStareLed.setText(cursor.getString(13));
}
}
markMessageRead();//mark all message "read=1"
}
Just add "date desc limit 1"
String msgData = "";
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[] {"body"}, null ,null, "date desc limit 1"); //if you change the 1 to 2 you´ll get the last 2 sms...
cursor.moveToFirst();
do{
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
msgData += cursor.getString(idx);
}
}while(cursor.moveToNext());
textStareLed.setText(msgData);

Insert predefined message into existing conversation (Android)

Is there any way that I can insert a predefined message into an existing conversation in the inbox from an app built on the Android platform?
With this code I am able to retrieve the last sent and received messages.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
Uri uriSMS = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMS, null, null, null, null);
cur.moveToNext();
String body = cur.getString(cur.getColumnIndex("body"));
String add = cur.getString(cur.getColumnIndex("address"));
String time = cur.getString(cur.getColumnIndex("date"));
String protocol = cur.getString(cur.getColumnIndex("protocol"));
String contactName = "";
Uri personUri = Uri.withAppendedPath( ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(add));
Cursor c = getContentResolver().query(personUri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null );
if( c.moveToFirst() ) {
int nameIndex = c.getColumnIndex(PhoneLookup.DISPLAY_NAME);
contactName = c.getString(nameIndex);
}
c.close();
cur.close();
String out = "";
Date d = new Date(Long.valueOf(time));
if (protocol == null)
out = "Sending to: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";
else
out = "Received from: "+ contactName + " <"+add +">\nDate: "+d +"\nBody: "+body+"\n\n";
tv.setText(out);
setContentView(tv);
}
Is there any way that I can incorporate this into my broadcast receiver (SmsReceiver.java)? Hopefully there is a way that I can trigger an event so that when a new message is received I can insert a predefined message into the conversation related to the newest text message in the inbox.
While I'm at it. Is there any way to accurately retrive the native phone number without asking the user to enter it manually?
Solved using the following code in my receiver.
ContentValues values = new ContentValues();
values.put("address", "0123456789");
values.put("body", "Message goes here");
context.getContentResolver().insert(Uri.parse("content://sms/"), values);

How to read message from inbox

This is the code i am using to read the inbox message when view button is pressed in context menu but i am unable to read messages i get only the body of the first message.
public class Smsread extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
Uri uri = Uri.parse("content://sms/inbox");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);
String sms = "";
if(c.moveToFirst()){
for(int i=0;i<c.getCount();i++){
sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
// sms=c.getString(c.getColumnIndexOrThrow("address")).toString();
}
c.close();
c.moveToNext();
/*
sms ="From :" + c.getString(2) + " : " + c.getString(11)+"\n";
*/
}
view.setText(sms);
setContentView(view);
}
}
Try this:
if(c.moveToFirst()){
for(int i=0;i<c.getCount();i++){
sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
c.moveToNext();
}
}
You will have to move to next record for each iteration of for loop.then only,you will get all messages there.Here,in for loop,after you get String sms,you can use that sms to show up somewhere.Each time,it will grab next sms into it!
Hope you got the point!
You must change
sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
to
sms+= c.getString(c.getColumnIndexOrThrow("body")).toString();
Try this:
String[] sms=new String[c.getCount()];
if(c.moveToFirst()){
do{
sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
} while(c.moveToNext());
}

Categories

Resources