Reading last sms from a particular sender - android

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);
}

Related

Fetching more than 10000 contacts from android device

I want to retrieve >10000 contacts from the android device. To fetch that much contact it takes about 8-10 min. Is there any other possible way to do this. I have implemented a method its working fine but when it comes to large number of contacts it taking it time to fetch the contacts.
ContentResolver cr = getActivity().getApplication().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.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()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber = phoneNumber.replace(" ","");
phoneNumber = phoneNumber.replace("-","");
boolean addNumber = stringCheck(phoneNumber,symbols);
if (!addNumber){
if (phoneNumber.length() == 10){
addContact(phoneNumber,phoneType,name);
}else if (phoneNumber.length() == 11){
phoneNumber = phoneNumber.substring(1);
addContact(phoneNumber,phoneType,name);
}else if (phoneNumber.length() == 12){
phoneNumber = phoneNumber.substring(2);
addContact(phoneNumber,phoneType,name);
}else if (phoneNumber.length() == 13){
phoneNumber = phoneNumber.substring(3);
addContact(phoneNumber,phoneType,name);
}
}
}
pCur.close();
}
}
}
If 900 out of the 1000 contacts have phone numbers, you're currently querying the DB 901 times, you can reduce it to only two queries:
Give me all contacts information
Give me all phone numbers
Then you use the contact-id field on both to match the phone to the right contacts.
Also, as noted in other answers, you really should add projection to all your queries to improve performance.
Another improvement you can make is to avoid runtime cur.getColumnIndex() calls, if you have a projection, you should already know the index - so use it hard-coded
Map<Long, List<String>> phones = new HashMap<>();
ContentResolver cr = getActivity().getApplication().getContentResolver();
// First build a mapping: contact-id > list of phones
Cursor cur = cr.query(Phone.CONTENT_URI, new String[] { Phone.CONTACT_ID, Phone.Number }, null, null, null);
while (cur != null && cur.moveToNext()) {
long contactId = cur.getLong(0);
String phone = cur.getString(1);
List list;
if (phones.contains(contactId)) {
list = phones.get(contactId);
} else {
list = new ArrayList<String>();
phones.put(contactId, list);
}
list.add(phone);
}
cur.close();
// Next query for all contacts, and use the phones mapping
cur = cr.query(Contacts.CONTENT_URI, new String[] { Contacts._ID, Contacts.DISPLAY_NAME }, null, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
List<String> contactPhones = phones.get(id);
addContact(id, name, contactPhones);
}
Keep your fetching process in doInBackground method of AyncTask and then display it. And get only those info which is required first, eg, Contact Name and ID.
refer this ans for more clarification:
Fetching a large number of contacts
In order to fetch large number of contacts you must be fetching the contacts in the form of pages by using rest API.
For example page size of contacts is 100.
One possible approach:
If you are showing the contacts in the listview then you can fetch the data while user is scrolling the list. For that You have to set the threshold position of the list i.e. 80, So when user reaches at 80th position while scrolling you can hit your rest API for next page to fetch the new contacts and add those contacts in the listview.
From your comments i can see you want all the contacts in order to move forward.For this you can start an intent service when app is launched. This service will fetch all the contacts and store them. You can use them as per your needs
Here some optimization for speed up
String[] select = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, select, null, null, null);
String[] selectOnly = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE};
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
selectOnly,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
Do your number check, length check in server side if possible. Do it in Async task.

How to get phone number for each SMS on the device?

I'm getting all phone's SMS with this URI - Telephony.Sms.Inbox.CONTENT_URI
TextBasedSmsColumns contains all columns for a projection.
Cursor is built like this:
cursor = getContentResolver().query(Telephony.Sms.Inbox.CONTENT_URI,
new String[] { BaseColumns._ID, TextBasedSmsColumns.ADDRESS },
null, null, null);
There is a TextBasedSmsColumns.ADDRESS column that contains a phone number OR a contact name. So how can I always get the phone number for the SMS? Should I use another approach to get all SMS?
This is how I got.
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
if (cur != null) {
while (cur.moveToNext()) {
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
//Here you will get all PhoneNumbers
String phoneNumber = cur.getString(cur.getColumnIndexOrThrow("address"));
}
cur.close();
}

Get contact by thread_id

I was on this topic Android get contact id from thread id where somebody had the exact same question as i'm asking. Unfortunately, lopez.mikhael answered completely not on the target.
How can I get the contact ID with a thread_id from URI content://sms/conversations ? Assuming that it returns :
- thread_id
- snippet
- msg_count
Getting all the contacts is not helping. I've searched in getColumnsNames() from there and I don't see any good column name that would contain a thread_id.
Why is this not documented ? Is it deprecated to use content://sms/ ? Is there any other solution ?
Thank you.
As mentioned here on this post
You can fetch sms-inbox:
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String where = "thread_id="+ <thread id you get it from content://sms/conversations>;
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, where, null, null);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
String count = Integer.toString(cursor1.getCount());
while (cursor1.moveToNext()){
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]));
String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
}
}
You can fetch other sent items by changing the URI.
Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");
Ones you have phone number you can find contact id using below code
String contactid = null;
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));
Cursor cursor = contentResolver.query(
uri,
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
if (contactid == null) {
Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}else{
//You can contact id do what you want to do with it.
}

Get only numbers shown in the android contactbook

I want to filter what numbers I am getting from Android based on which contacts the user chose to display in his contact book. (For example only contacts saved on the device when all other contacts are excluded)
I read here that you can do this by using
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
I use following code to read the contacts and I allways get every contact, phone, SIM, etc..
//https://stackoverflow.com/questions/16651609/how-to-display-phone-contacts-only-exclude-sim-contacts
// http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/1/
ContentResolver cr = currentActivity.getContentResolver();
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
Cursor cur = cr.query(queryUri,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) { //Are there still contacts?
//See if the contact has at least one number
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID) );
String name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME) );
ArrayList<String> numbers = new ArrayList<String>();
//Read numbers:
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
numbers.add( pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) );
Log.e("Contactbook", "The latest added number is: " + numbers.get(numbers.size()-1) );
}
pCur.close();
}
}
}
What am I missing? This code still gives me both SIM and phone contacts to the log.
Edit: To clarifify, in the contactbook you got the "Display options". In there ist the "select contacts to display"-option, and I want to read the contacts that are shown based on the users choice there. So if a user choses to show only SIM-contacts, read only SIM-contacts, if he choses to only show Phone-Contacts, show onyl phone contacts etc...
Try with following "selection".
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
From Android docs:
public static final String IN_VISIBLE_GROUP: An indicator of whether this contact is supposed to be visible in the UI. "1" if the contact has at least one raw contact that belongs to a visible group; "0" otherwise.
This should be your "selection" argument in query API.
Update: I tried below code on Android-2.3(I know it is old device, but right now Don't have newer device with me).
final ContentResolver cr = getContentResolver();
String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?" ;
String[] Args = { "1" };
final Cursor contacts = cr.query(
ContactsContract.Contacts.CONTENT_URI, projection,
selection,Args ,
null);
This could be a long operation (depending on no. of contacts), hence you should use CursorLoader class (A loader that queries the ContentResolver and returns a Cursor) for this.
cursorLoader.loadInBackground();
This will be called on a worker thread to perform the actual load and to return the result of the load operation.
You can easily create two Arrays for each kind of contacts
ArrayList<String> simContacts = new ArrayList<>();
//get all sim contacts
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = getContext().getContentResolver().query(simUri, null, null, null, null);
while (cursorSim.moveToNext()) {
simContacts.add(cursorSim.getString(cursorSim.getColumnIndex("name")));
}
}
ArrayList<String> allContacts = new ArrayList<>();
//get all contacts
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
while (!cursor.isAfterLast()) {
String phoneNumber = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayNameColumn = Utils.hasHoneycomb() ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME;
String displayName = cursor.getString(cursor.getColumnIndexOrThrow(displayNameColumn));
//check if simContacts Array contains this particular name
if (!simContacts.contains(displayNameColumn){
allContacts.add(displayNameColumn);
}
}
}
This is just a working example,of course you can modify to your needs.You can parse more contact fields and make more complication queries.

Contact's ID from number

I can find the number of the contact from her id. The following snippet prints a number on the screen.
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
null, null);
phones.moveToNext();
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getBaseContext(), phoneNumber, Toast.LENGTH_LONG).show();
However, if I try to find her id from her number,
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
null, null);
phones.moveToNext();
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Now I got a valid phone number
//using that number to find the id
Cursor ids = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber,
null, null);
ids.moveToNext();
String id = ids.getString(
ids.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
Toast.makeText(getBaseContext(), id, Toast.LENGTH_LONG).show();
I get the following error.
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
The projection I use is as follows:
private static final String[] CONTACT_PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID
};
The second cursor is empty even if I know there is such an entry in the contacts. What am I doing wrong?
Don't know why, but PhoneLookup worked for me. For those who have the same problem, here is my code.
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("aNumber"));
Cursor phones = getContentResolver().query(contactUri , PEOPLE_PROJECTION, null, null, null);
//here, the cursor keeps the wanted contact
...
private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.PhoneLookup._ID
};

Categories

Resources