How to get Missed call & SMS count

前端 未结 2 1989
醉酒成梦
醉酒成梦 2020-12-08 12:48

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 bigge

相关标签:
2条回答
  • 2020-12-08 13:06

    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

    0 讨论(0)
  • 2020-12-08 13:13

    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>
    
    0 讨论(0)
提交回复
热议问题