如何在 Android SDK 2.2 上检索未接来电 [英] How to retrieve missed calls on Android SDK 2.2
问题描述
在我的应用程序中,当来电但用户未接听时,我应该执行一些操作.
in my app I should do some action when a call comes but not answered by the user.
我在 android.telephony 和 NotificationManager 中搜索过,但没有找到解决此问题的方法.
I have searched in the android.telephony and the NotificationManager, but I haven't found a method to solve this problem.
有人知道如何知道电话上是否有未接来电吗?
Does someone have an idea of how to get to know if there is a missed call on the phone or not ?
推荐答案
这里是可以在通话记录中查询未接来电的代码.基本上,您必须以某种方式触发它,并确保您给通话记录一些时间(几秒钟应该这样做)来写入信息,否则如果您过早检查通话记录,您将找不到最近的通话.
Here is code that can query the call log for a missed call. Basically, you will have to trigger this somehow and make sure that you give the call log some time ( a few seconds should do it) to write the information otherwise if you check the call log too soon you will not find the most recent call.
final String[] projection = null;
final String selection = null;
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor cursor = null;
try{
cursor = context.getContentResolver().query(
Uri.parse("content://call_log/calls"),
projection,
selection,
selectionArgs,
sortOrder);
while (cursor.moveToNext()) {
String callLogID = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls._ID));
String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
String callDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
String isCallNew = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));
if(Integer.parseInt(callType) == MISSED_CALL_TYPE && Integer.parseInt(isCallNew) > 0){
if (_debug) Log.v("Missed Call Found: " + callNumber);
}
}
}catch(Exception ex){
if (_debug) Log.e("ERROR: " + ex.toString());
}finally{
cursor.close();
}
我希望你觉得这很有用.
I hope you find this useful.
这篇关于如何在 Android SDK 2.2 上检索未接来电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!