当通话结束时,如何在将通话信息写入数据库后查询通话记录?
我正在寻找呼叫的结束,在BroadcastReceiver
上使用带有intent-filter的android.intent.action.PHONE_STATE
,寻找手机闲置。
对此有任何帮助将不胜感激。
由于
答案 0 :(得分:8)
这是非常好的答案。
见以下链接
当你看到上面的例子时,你将学习如何获得呼叫的结束状态,你也会记住,在呼叫结束后CALL_STATE_IDLE
会多次调用,所以你必须在某些地方采取一个静态变量在理想状态下工作之前,你必须检查变量值。
修改强>
Android将呼叫日志信息存储在其内置数据库中。
因此,更好的解决方案是,当您的代码在IDLE
状态之后调用OFFHOOK
状态时,您可以将所有新调用的日志从内置数据库复制到您的数据库以获取调用日志的信息
您可以使用以下查询从内置数据库中检索呼叫日志信息
<强> Cursor c = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, null, null, null);
强>
<强> EDIT2
强>
以下是完整示例
这是PhoneStateListener类
public class CustomPhoneStateListener extends PhoneStateListener {
Context context;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// Toast.makeText(context, "CALL_STATE_IDLE", Toast.LENGTH_LONG).show();
if(UDF.phoneState != TelephonyManager.CALL_STATE_IDLE) {
UDF.fetchNewCallLogs(context);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Toast.makeText(context, "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
//Toast.makeText(context, "CALL_STATE_RINGING", Toast.LENGTH_LONG).show();
endCallIfBlocked(incomingNumber);
break;
default:
break;
}
UDF.phoneState = state;
}
这是广播接收器类
public class PhoneStateBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//UDF.createTablesIfNotExists(context);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
这是从内部数据库获取新呼叫记录的功能
public static void fetchNewCallLogs(Context context) {
CallLogHelper callLogHelper = new CallLogHelper(context);
callLogHelper.open();
Long maxId = callLogHelper.getMaxId();
Cursor c = context.getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, "_id > ?", new String[]{String.valueOf(maxId)}, null);
if(c != null && c.moveToFirst()) {
while (c.isAfterLast() == false) {
int _ID = c.getColumnIndex(android.provider.CallLog.Calls._ID);
int _NUMBER = c.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int _DATE = c.getColumnIndex(android.provider.CallLog.Calls.DATE);
int _DURATION = c.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int _CALLTYPE = c.getColumnIndex(android.provider.CallLog.Calls.TYPE);
int _NAME = c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int _NUMBERTYPE = c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NUMBER_TYPE);
int _NEW = c.getColumnIndex(android.provider.CallLog.Calls.NEW);
String id = c.getString(_ID);
String number = c.getString(_NUMBER);
String date = c.getString(_DATE);
String duration = c.getString(_DURATION);
String callType = c.getString(_CALLTYPE);
String name = c.getString(_NAME);
String numberType = c.getString(_NUMBERTYPE);
String _new = c.getString(_NEW);
callLogHelper.createLog(id, number, date, duration, callType, name, numberType, _new, "N");
c.moveToNext();
}
}
callLogHelper.close();
}
**Where**
=> CallLogHelper is a helper class to communicate with my local database
=> callLogHelper.getMaxId(); will returns the maximum id of call logs in my local database and I am keeping the id in local database and internal database will be same
=> callLogHelper.createLog() is my function to insert call log in my local database
这是清单文件
<receiver android:name=".PhoneStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>