Android得到Missed电话

时间:2012-06-14 15:07:56

标签: android

所以我想得到未接来电的数量,例如通知栏中显示的电话应用

所以我想出了这段代码:

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,projection,where, null, null);
c.moveToFirst();    
Log.d("CALL", ""+c.getCount()); //do some other operation

if(c.getCount() > 0)//...etc etc
    Toast.makeText(app.this,String.valueOf(c.getCount()), Toast.LENGTH_LONG).show(); 

此代码为我提供了大量未接来电,但我只想要通知栏中显示的最新电话

有人知道怎么做吗?

3 个答案:

答案 0 :(得分:2)

CallLog.Calls也有IS_READ和NEW字段。

我相信你应该添加到你的字符串 - IS_READ = 0。

请注意,IS_READ来自API Level 14,NEW来自API Level 1,所以我会检查它们。

http://developer.android.com/reference/android/provider/CallLog.Calls.html#IS_READ

答案 1 :(得分:2)

String PATH = "content://call_log/calls";

String[] projection = new String[] { CallLog.Calls.CACHED_NAME,
                CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.TYPE };

String sortOrder = CallLog.Calls.DATE + " DESC";

StringBuffer sb = new StringBuffer();
sb.append(CallLog.Calls.TYPE).append("=?").append(" and ").append(CallLog.Calls.IS_READ).append("=?");

Cursor cursor = context.getContentResolver().query(
                Uri.parse(PATH),
                projection,
                sb.toString(),
                new String[] { String.valueOf(Calls.MISSED_TYPE), "0" },sortOrder);

https://play.google.com/store/apps/details?id=com.meaning36.msreminder

答案 2 :(得分:0)

gc hong发布的代码如何显示最近的未接来电?它的工作非常正确,但是它是如何做的。

String PATH = "content://call_log/calls";

String[] projection = new String[] { CallLog.Calls.CACHED_NAME,
            CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.TYPE };

String sortOrder = CallLog.Calls.DATE + " DESC";

StringBuffer sb = new StringBuffer();
 sb.append(CallLog.Calls.TYPE).append("=?").append(" and 
").append(CallLog.Calls.IS_READ).append("=?");

Cursor cursor = context.getContentResolver().query(
            Uri.parse(PATH),
            projection,
            sb.toString(),
            new String[] { String.valueOf(Calls.MISSED_TYPE), "0" 
},sortOrder);