I want to get the count of missed calls and unread messages in my application. and I'd like to open the relevant application when user click on the count.
Now biggest problem is how to get the count?
I searched online but couldn't find any solution.
Thanks in advance.
http://developer.android.com/reference/android/provider/CallLog.Calls.html
Take a look at this CallLog class. All you need is to query the phone for any calls then extract missed one (оr do this when you are querying the phone, in the selection arguments). The same applies for the messages. SMS are stored in the Content provider under "content://sms/"
Then just get the count of rows in the Cursor that is return by the query. :)
I hope this helps.
For missed calls:
String[] projection = {
CallLog.Calls.CACHED_NAME,
CallLog.Calls.CACHED_NUMBER_LABEL,
CallLog.Calls.TYPE
};
String where = CallLog.Calls.TYPE + "=" + CallLog.Calls.MISSED_TYPE;
Cursor c = this.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
selection,
where,
null,
null
);
c.moveToFirst();
Log.d("CALL", ""+c.getCount()); //do some other operation
if (c.getCount() == SOME_VALUE_TO_START_APP_ONE) //...etc etc
In the where clause you set condition for selection of data. In our case we need everything which type equals CallLog.Calls.MISSED_TYPE. We select project the Name of the caller and his number, ofcourse you can specify more information to be queried like type of number like mobile, home, work.
The expression is equivalent to SQL query, something like: SELECT CACHED_NAME, CACHED_NUMBER_LABEL, TYPE FROM CONTENT_URI WHERE TYPE=MISSED_TYPE
This requires permissions to be added to the Manifest
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
For querying SMS ContentProvider:
Uri sms_content = Uri.parse("content://sms");
Cursor c = this.getContentResolver().query(sms_content, null,null, null, null);
c.moveToFirst();
Log.d("SMS COUNT", "" + c.getCount()); //do some other operation
// Here proceed with the what you wanted
if (c.getCount() == SOME_VALUE_TO_START_APP_ONE)//...etc etc
You can go deeper in the content tree like specifying the type of sms, like: content://sms/sent or content://sms/inbox and add projection and selection for the second argument of the query() method like, name, person, status of the message (like the Calls example).
This requires permission:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
As I don't have enough reputation to answer #Prasad question comment about
ERROR -> getContentResolver() is undefined for the type new Runnable(){}
getContentResolver() is part of application context, so if you are using a BroadcastReceiver use context in onReceive() function like this
#Override
public void onReceive(Context context, Intent intent) {
context.getContentResolver()
}
If you are using the code above inside an Activity, then you can use
getApplicationContext().getContentResolver()
also make sure to use [Ctrl + Shift + O (O not zero)] to organize imports
Key Shortcut for Eclipse Imports
Related
I need to get a link from auto-sent-SMS, but this SMS is always from a different number, so it's not like Retriever API.
I wanted to know - is it even possible for Android 8-9 and higher to programmatically read SMS via app?
If you know some examples on how - appreciate if you'll share :)
*most of the topics here are outdated or related to Retriever API so I wanted to know for sure about the latest Android versions.
There are Telephony.Sms.Inbox and Telephony.Sms.Sent content providers to access incoming and outgoing sms data. I used this far far way ago, but now i got my old code, and it still working in api 29. Here is my sample code to get basic data from inbox table, it's full text, but better way is to access content_uri and columns via content provider classes. If you want to get more columns, check docs. Remember, you need to grant READ_SMS permission.
Uri inboxUri = Uri.parse("content://sms/inbox");
String[] projection = new String[] { "_id", "date", "date_sent", "address", "body" };
String sortOrder = "date";
String limit = " DESC";
Cursor cursor = mContext.getApplicationContext().getContentResolver().
query(inboxUri, projection, null, null, sortOrder + limit);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
//read sms data
} while (cursor.moveToNext());
}
cursor.close();
}
Is there an accepted/received way of checking to see if a person (phone number) is in the Contact List?
I'm hoping there's something I can call like this:
bool bInContactList = InContactList("1415922353");
It is not as simple as you want but you can query a Contacts content provider for the contact associated with a phone number:
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = getContentResolver().query(lookupUri, new String[] {PhoneLookup._ID}, null, null, null);
if (c.getCount() > 0) {
// there is some contact
} else {
// there is no contacts with phoneNumber
}
The application needs android.permission.READ_CONTACTS permission to access contacts data.
You can check Android Developer site for further references about content providers and android.providers package documentation for the list of available standard providers in Android.
I am afraid you did not provide enough information about the language you are talking about. If you use Java -for example- you have a contains method (common to all collections) so, if you want to know if a certain String is contained in a collection you could do it by invoking this method:
boolean found = someCollection.conmtains("1415922353");
I'm stuck with the call_log functionality in Froyo. As many of you know, Froyo logs in call log not only calls but also each outgoing and incomming SMS message. You can chose in options to show all that crap, or only specific types (outgoing calls, incoming calls, sent messages, received messages etc), but since this is radio button, you cannot specify for example only ongoing and incoming calls. Very known and annoing Froyo functionality.
So I started to write some simple tool to read the call log by myself. Here is the code snippet:
try {
mCur = getApplicationContext().getContentResolver()
.query(CallLog.Calls.CONTENT_URI, columns, null, null, null );
mCur.moveToFirst();
io = mCur.getColumnIndex(CallLog.Calls._ID);
bo = mCur.getColumnIndex(CallLog.Calls.NUMBER);
no = mCur.getColumnIndex(CallLog.Calls.CACHED_NAME);
to = mCur.getColumnIndex(CallLog.Calls.TYPE);
while (mCur.isAfterLast() == false) {
i = mCur.getString(io);
b = mCur.getString(bo);
n = mCur.getString(no);
t = mCur.getString(to);
Log.i(TAG, "CallLog: ID="+i+" number="+b+" name="+n+" type="+t);
mCur.moveToNext();
}
} catch (Exception e) {
Log.e(TAG, "updateCallLog", e);
} finally {
if (mCur != null) {
mCur.close();
mCur = null;
}
}
Surprise, surprise, the call_log provider skips the sms records from the call log. So with the code above I see only call records (incoming or outgoing), all other records are skipped. The little more digging into it revealed that the CallLog provider adds internally filtering to the call log database:
02-03 09:26:42.348 E/CLCService(28244): android.database.sqlite.SQLiteException:
near ")": syntax error: , while compiling:
SELECT _id, name, number, type FROM logs WHERE (logtype=100 OR logtype=500) AND (_ID=)
Do not look for the syntax error, it was created on purpose to force provider to dump the SQL query by calling query(CallLog.Calls.CONTENT_URI, columns, "_ID=", null, null )). The (_ID=) is what is provided in the query, the rest of (logtype=100 OR logtype=500) is apparently added by the call log provider itself.
So I have two questions:
Where I can find in the Android code how the provider is adding the logtype filter? I was looking into CallLog.java and CallLogProvider.java and cannot find it.
How can I read all records from the call log in Froyo? I cannot bypass the call log provider and use my own SQL helper for this until I will not root the phone, which is not an option. Is there any other way to do it?
I'm not certain just what is going wrong but reading the call log to get just incoming or outgoing calls is simple enough. The sample below adds restrictions to the query so that it only returns data for outgoing calls made after a certain date. The where string uses question marks to indicate where the values from the wargs array should be substituted in to form the sql query.
About where the extra WHERE clause occurs. Almost certainly in the calllog provider implementation. The providers commonly have a switch statement that uses the uri that you use to open the provider and then adds restrictions based on the uri. The calllog one seems to be in packages/providers/ContactsProvider.
public static int getMinutesUsedSince(Context context, Date date) {
Uri uri = CallLog.Calls.CONTENT_URI;
String columns[] = new String[] { CallLog.Calls.DURATION };
String where = CallLog.Calls.TYPE + "=? AND " + CallLog.Calls.DATE + ">?";
String wargs[] = new String[] {
String.valueOf(CallLog.Calls.OUTGOING_TYPE),
String.valueOf(date.getTime())
};
String sortOrder = "date DESC";
Cursor c = context.getContentResolver().query(uri, columns, where, wargs, sortOrder);
long sum = 0;
int durationIndex = c.getColumnIndex(CallLog.Calls.DURATION);
if (c.moveToFirst()) {
do {
/* for each individual call, round up to the nearest minute */
long duration = c.getLong(durationIndex);
long minutes = (long)Math.ceil(duration / 60.0);
sum += minutes;
} while (c.moveToNext());
}
c.close();
return (int)sum;
}
I'm wondering if the following is possible:
I've an app (service) that stays in background, and gets triggered whenever the user
Adds/deletes/updates a contact
Installs/uninstalls an app
Adds/deletes/renames a file on the FS
Do you think this is possible guys? (in a proper way of course, if it's possible to do it by hacking and dirty stuff I'd pass)
I tried to look over the internet a bit but didn't find discussions related to this point.
What's your guess ?
Haven't tried any of this myself, but:
http://mylifewithandroid.blogspot.com/2008/03/observing-content.html seems to deal with detecting contact data changes. Basically you need to register a ContentObserver and handle the changes you are notified of.
Check out http://developer.android.com/reference/android/content/Intent.html - from that you can register a BroadcastReceiver to be notified of applications being installed or uninstalled. Look for ACTION_PACKAGE_ADDED and ACTION_PACKAGE_REMOVED
Please refer to How to detect file or folder changes in Android? for how to detect when files are changed in the filesystem. You may be limited to your sandbox with a FileObserver, I'm not sure. Also - a rename doesn't seem to be explicitly notified, but you will probably detect it from a MOVED_FROM followed by MOVED_TO, or possibly a DELETE followed by CREATE
Found in the SDK sample for SDK version 5+:
/**
* Retrieves the contact information.
*/
#Override
public ContactInfo loadContact(ContentResolver contentResolver, Uri contactUri) {
ContactInfo contactInfo = new ContactInfo();
long contactId = -1;
// Load the display name for the specified person
Cursor cursor = contentResolver.query(contactUri,
new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null);
try {
if (cursor.moveToFirst()) {
contactId = cursor.getLong(0);
contactInfo.setDisplayName(cursor.getString(1));
}
} finally {
cursor.close();
}
// Load the phone number (if any).
cursor = contentResolver.query(Phone.CONTENT_URI,
new String[]{Phone.NUMBER},
Phone.CONTACT_ID + "=" + contactId, null, Phone.IS_SUPER_PRIMARY + " DESC");
try {
if (cursor.moveToFirst()) {
contactInfo.setPhoneNumber(cursor.getString(0));
}
} finally {
cursor.close();
}
return contactInfo;
}
You can specify the contact columns you want to retreive with Cursor cursor = contentResolver.query(contactUri, new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); The column names are described at http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html and looking at the sample, it seems cursor.getLong(0) here is the contact ID you're looking for. It also seems that it is volatile depending on how the contact is edited and how others are added, but you're catching those too so you should be able to handle those cases.
I make a BroadcastReceiver to receive Phone number of the person who call me
<intent-filter>
<action
android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
How to check if the phone number receive is on my contact list ?
Do you have a tip to know if this phone number exist on contact list with out loading contact list ?
I don't want more information, just if this phone number exist.
If it's not possible, and I must load contact list, how to do it on BroadcastReceiver ?
When I try to do getContentResolver, it's not working because I'm on BroadcastReceiver and not inside Activity...
Thanks for your help
public boolean contactExists(Context context, String number) {
// number is the phone number
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
cur.close();
return true;
}
} finally {
if (cur != null)
cur.close();
}
}
return false;
}
I think it's important to say that you need to add the following in your manifest file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
for 1 you should have a look at the recommended ContactsContract.PhoneLookup provider
A table that represents the result of looking up a phone number, for example for caller ID. To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI. This query is highly optimized.
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor mycursor=resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
if (mycursor!=null && mycursor.moveToFirst()) {
// record exists
}
for 2 you can use the context from the onReceive method to call methods that belong to Context
ContentResolver cr=context.getContentResolver();
I suggest you to use Phone.CONTENT_FILTER_URI instead of PhoneLookup.CONTENT_FILTER_URI
because PhoneLookup can be empty and you will get no result from time to time (tested on LG-P500, froyo)
The problem on my device happens for example when:
switch to airplane mode
use the default message application to send a sms (will be queued).
use PhoneLookup.CONTENT_FILTER_URI to query for a contact
Not all devices seems to be affected
Using PhoneLookup.CONTENT_FILTER_URI the returned cursor is always empty.
Using Phone.CONTENT_FILTER_URI everything is ok (you find the contact if any).
Therefore I suggest you to always use Phone.* Uris except when you really need to use PhoneLookup.*... Which usually is just address book synchronization related stuff (and most of the times is not what you are interested in).