Displaying all SMS from one specific sender in my android app - android

My app is currently displaying all the SMS in the inbox with this code:
public void onClick(View v) {
if (v == btnInbox) {
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content
// Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
lvMsg.setAdapter(adapter);
}
}
and I want to display only messages from one specific sender, what will i change to eliminate messages from the other sender?

apply selection arguments to filter the contents based on address field.
Actual code becomes:
String[] selectionArgs = { "12672631" }; //add the phone number here
Cursor c = cr.query(inboxURI, reqCols, "address=?", selectionArgs, null);

Related

Reading last sms from a particular sender

Im building a app that is reading last sms from spec number (vb# 8888). Code is working great. I only have one prob. When i get a new sms from other number (vb# 7777)my code stops reading sms from (v#8888). if i delete the new sms from (7777) than my code strats working again. I'm using this to update a string in my app. Can someone help me
This is my code
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri, null, null, null, null);
String[] columns = new String[]{"address", "body"};
if(cursor1.moveToFirst()) {
String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
if address.equalsIgnoreCase("+597*******")) {
body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);//body
Your code is just reading the most recent message in the inbox, whomever it's from. If you want the most recent message from that particular number, you can adjust your query to match the number, and limit the results to one record.
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] projection = {"address", "body"};
String phoneNumber = "+597*******";
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
projection,
"address = ?",
new String[] {phoneNumber},
"date DESC LIMIT 1");
if (cursor1 != null && cursor1.moveToFirst()) {
body = cursor1.getString(cursor1.getColumnIndex("body"));
Koers = (TextView) findViewById(R.id.Koersdiedag);
Koers.setText(body);
}

query sms text body and show in listview

I want to filter the inbox view of my app depending on the string entered in search view.
So if user enters "How" the filter should get all the texts containing "How" in it
this is the code I have written :
int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
AutoCompleteTextView textView = (AutoCompleteTextView) searchView.findViewById(id);
textView.setTextColor(Color.WHITE);
final Uri inboxURI = Uri.parse("content://sms/");
final String[] reqCols = new String[] {"_id","address","body" };
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
final SimpleCursorAdapter sadapter = new SimpleCursorAdapter(this, R.layout.list_row, c, new String[] {"address", "body" }, new int[] {R.id.phone_number,R.id.text_msg});
textView.setAdapter(sadapter);
sadapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return getContentResolver().query(inboxURI, reqCols, "body" + " LIKE '" + constraint + "%'", null, null);
}
});
but here if I type "How" the filter returns only texts which start with "How". I want all the texts which contain this string at any position in it.
Need help on how to create a query to get the result.
your query like constraint% ... change it like to like %constraint% (note the additional %)

How to access miscall and new message state in android?

I wanna make an android app that runs in background and check if there is a miscall or new massage and if it is true do something.How can I run my app in background (it does not close when I click back button) and how to access miscall and new message state I am an amateur in android.
Thanks!
For not seen sms messages there is a column in content://sms that you can check in order to determine if a message has been seen.
Column name is "seen".
example of calling it and checking:
ContentResolver mContectResolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
String[] mProjection = {
"_id",
"address",
"person",
"date",
"body",
"protocol",
"seen"
};
Cursor cursor = mContectResolver.query(uri, mProjection, null, null, null);
cursor.moveToFirst();
for(int i = 0 ; i<cursor.getCount() ; i++){
if(cursor.getString(cursor.getColumnIndexOrThrow("seen")).equalsIgnoreCase("1")){
//Message has not been seen
}
cursor.moveToNext();
}
cursor.close();

How to obtain the name of a caller from the phone number in Android

I am working in Android and I would like to know how to find out the contact name of a phone number.
In the BroadcastReceiver I have this:
String phonenr=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
I need the contact name ( full name - firstname + familyname , if there is) for this phone number.
Thx!
You can Try this link
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME} .....)
Use the function Below like this:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("your receive phone number"));
Cursor phones = getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
while (phones.moveToNext())
{
//it returns contact name
String name=phones.getString(phones.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
Check this out Retrieving Contact Information
//query for the people in your address book
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,People.NAME + " ASC");
startManagingCursor(cursor);
//bind the name and the number fields
String[] columns = new String[] { People.NAME, People.NUMBER };
int[] to = new int[] { R.id.name_entry, R.id.number_entry };
SimpleContactAdapter mAdapter = new SimpleContactAdapter(this, R.layout.list_entry, cursor, columns, to);
this.setListAdapter(mAdapter);

What is the correct CONTENT_URI for the SMS thread table?

I'm attempting to query the SMS thread table, but I don't seem to use the correct URI.
Querying the URI "content://sms/conversations" gives me the columns:
[thread_id, msg_count, snippet]
Querying the URI "content://mms-sms/conversations" gives me the columns:
[body, person, sub, subject, retr_st, type, date, ct_cls, sub_cs,
_id, read, ct_l, tr_id, st, msg_box, thread_id, reply_path_present,
m_cls, read_status, ct_t, status, retr_txt_cs, d_rpt, error_code,
m_id, m_type, v, exp, pri, service_center, address, rr, rpt_a,
resp_txt, locked, resp_st, m_size]
I'm looking for the URI that gives the values from the Threads table in android/provider/Telephony.java - data, recipient_ids, message_count, read, etc...
Thanks
try this
Uri uri = Uri.parse("content://sms/");
you can get what you want like this
Uri uri = Uri.parse("content://sms/");
// List required columns
String[] reqCols = new String[] { "_id", "body" };
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = context.getContentResolver();
// Fetch Sent SMS Message from Built-in Content Provider
Cursor cursor = cr.query(uri, reqCols, "address = '" + number + "'", null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(context, R.layout.single_conversation_view, cursor, new String[] {"body" }, new int[] { R.id.tv_single_conversation_message }, 1);
lv_single_conversation.setAdapter(adapter);
hope it will help you

Categories

Resources