Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

android - How to get Missed call & SMS count

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...