How to get Missed call & SMS count

馋奶兔 提交于 2019-12-17 18:09:29

问题


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.


回答1:


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.

Edit: 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>



回答2:


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

I hope this will help you and help someone else, and I hope more that this is not against our community rule.



来源:https://stackoverflow.com/questions/7621893/how-to-get-missed-call-sms-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!