Android detect sms belongs to sender or receiver - android

I want to detect from sms list, which of messages are mine and witch of them belongs to other contacts.
For example:
Me: "body": Hi John
John "body: Hi there...
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = getActivity().getContentResolver().query(uriSMSURI, null, null, null, null);
while (cur.moveToNext()) {
//detect here
}

You can use the Telephony.TextBasedSmsColumns.CREATOR field to get the creator. Compare this to your address (mobile number) to distinguish yourself from the other people. You can get your number with the following:
(TelephonyManager)getSystemService(TELEPHONY_SERVICE).getLine1Number();

Related

Android SMS count database

I am looking for code to get the total sent-sms count in android mobiles. If I use content://sms/sent/ for querying the database I get the sms count of the available sent sms in the mobile, but I want the total sms that is sent from buying the mobile.
Retrieve All SMS messages in the Android
The URI (content://sms/) will used to retrive the all SMS messages, also we can split up by the message type as well as.
Uri allMessage = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(allMessage, null, null, null, null);
while (c.moveToNext()) {
String row = c.getString(1);
}
you can get the all inbox/outbox here
change your URI
use this one: content://sms/

Get email address from phone number in Android

I'm attempting to find someone's email address, given their phone number (assuming they have it stored in Contacts).
At the moment I've been fine if I want the person's name, or something, just doing:
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cur = getContentResolver().query(uri, null, null, null, null);
String name = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
However, there is no way to search using a phone number, through the email DB
Uri emailUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
Uri.encode(phoneNumber));
Cursor cur = getContentResolver().query(emailUri, null, null, null, null);
//Returns nothing, unless someone has the email +441234567891#hotmail.com or something
Now I could use CONTENT_FILTER_URI, and search for the name of the person I just found... But then I could get 2 John Smiths... There must be some way to just join across the PhoneLookup and the Email DBs? Maybe using the LOOKUP_KEY, or something?

Set sms as read in Android

In my application I need to read sms coming from only a number, and when i receive it I need to set it as read automatically, without setting it in the sms android application, but from my app. How can I do?
Thanks!
Let me update this:
ContentValues values = new ContentValues();
values.put("read",true);
getContentResolver().update(Uri.parse("content://sms/"),values, "_id="+SmsMessageId, null);
SmsMessageId is _id of message, which you find in SMS database.
A short example:
Uri uri = Uri.parse("content://sms/inbox");
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
while (cursor.moveToNext()) {
// Retrieve sms
// see column "address" for comparing
// Then update the sms and set the column "read" to 1
}

contacts in android

I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?
ContentResolver cr = getContentResolver();
int index=0;
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0)
{
phoneNames=new String[cur.getCount()];
phoneNumbers=new String[cur.getCount()];
while (cur.moveToNext())
{
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
phoneNames[index]=name;
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
phoneIndex++;
phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
index++;
}
pCur.close();
}
}
After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.
If you are specifically looking for data related to phone numbers i suggest you query on
android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor.
The following are the fields that you would be interested in:
DISPLAY_NAME
HAS_PHONE_NUMBER
NUMBER
TYPE
e.g
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
Further details please refer this
Please post your requirement if the assumptions are not true.
Some of quick checks:
Select only the required columns and not all in the first query.
Instead of using Integer.parseInt(cur.getString) use
cur.getInt()
Use PhoneLookup whenever dealing with phone numbers ( the number
field gives the raw phone number
instead of the value stored in
the database which can contain
-,),( appended with it)
Avoid using Cursor within a cursor. Use the API's which includes
joins already implemented in it like
RawContactsEntity, PhoneLookup.
Hope that helps.
Don't do complex database queries on the UI thread. What are you trying to do with the results? If you are displaying things in a list, use a CursorAdapter so that you only pull out what you need when you need it.

Getting AND Displaying Contacts

Well, I've been looking around and can't find a specific way to actually DISPLAY the contacts list. I've found tutorials such as higher pass's but it never actually discusses how to display them, just how to get them. I simply want to display the contacts list in a listView or something similar. I know it has to be a lot more simple than I'm making it out to be, because it seems to be a common thing. Specifically, all I want is the contact's name and phone numbers. I have my query set up, which I got the above mentioned tutorial, and I think it's right:
ContentResolver cr = 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()) {
}
pCur.close();
}
}
}
There is a built-in activity for this:
startActivity(new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI));
If you use startActivityForResult(), you can even get the user's chosen contact back.
If that's not what you're looking for, you want to cut back to a single query if possible, then create an Adapter for your ListView. Here is a sample project that shows querying the contacts to populate a ListView, showing either the contact's name, name and phone number, or name and email address. This gets a bit more complicated because the sample shows both the Android 1.6-and-lower and Android 2.0-and-higher contacts APIs.

Categories

Resources