使用服务在CallLog上获得最后一次呼叫 [英] get last Call on CallLog using a Service
问题描述
我正在做一个程序,将CallLog发送到服务器,我创建了一个服务,该服务使用以下方法对电话状态进行轮询。此方法称为onCreate。
I'm doing a program to send the CallLog to a Server, I created a Service that makes a poll of the telephone status with the following method. This method is called onCreate.
private void pollForCallStatus() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Log.d("Poll", Integer.toString(telephonyManager.getCallState()));
switch (telephonyManager.getCallState()){
case TelephonyManager.CALL_STATE_RINGING:
Log.d("CallService", "ringing");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callStart();
break;
case TelephonyManager.CALL_STATE_IDLE:
callEnd();
break;
}
}
}, 0, 1000);
}
CallEnd方法,检查呼叫是否已经开始,如果是获得CallLog的此呼叫的持续时间,我使用CallLog,因为我无法确定拨出电话的开始时间。
The method CallEnd, check if the call has started, if its the case its get the duration of this call of the CallLog, I use CallLog because i can't determinate when the outgoing calls starts.
private void callEnd() {
if (isStart) {
Log.d("CallService", "callEnd");
Integer duration = getDuration();
Log.d("CallService", duration.toString());
Command sender = new SendCall(this.phoneNumber, duration);
isStart = false;
this.stopSelf();
}
}
我粘贴了方法getDuration,我用它来获取
I pasted the method getDuration, I used it to get the duration of the call in CallLog.
private Integer getDuration(){
Integer duration = 0;
callLogCursor.moveToFirst();
if ( phoneNumber.equalsIgnoreCase(callLogCursor.getString(callLogCursor.getColumnIndex(CallLog.Calls.NUMBER))) )
duration = callLogCursor.getInt(callLogCursor.getColumnIndex(CallLog.Calls.DURATION));
return duration;
}
我的问题是getDuration没有让我获得最后的通话时间,尽管我接听前一个电话。我尝试等待更多的时间,但收到的结果相同。
My problem is that getDuration dont get me the last call duration, despite that i recive the previous call. I tried to wait more time but i receive the same result.
---编辑---
我将代码更改为使用ContentObserver,但结果相同。我在BroadcastReceiver上创建了Observer
I changed the code to use ContentObserver, but i have the same result. I create the Observer on BroadcastReceiver
public class CallsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
ContentResolver cr = context.getContentResolver();
Cursor callLogCursor = cr.query(android.provider.CallLog.Calls.CONTENT_URI,
null,
null,
null,
android.provider.CallLog.Calls.DEFAULT_SORT_ORDER /*sort by*/);
Command sender = new SendCall(phoneNumber, callLogCursor);
context.getContentResolver().
registerContentObserver(android.provider.CallLog.Calls.CONTENT_URI,
true, new CallObserver(context, sender));
}
}
当CallLog更改执行方法getDuration时,新的getDuration是
When CallLog change executes the method getDuration, the new getDuration is
private Integer getDuration(){
Integer duration = 0;
callCursor.moveToFirst();
if ( phoneNumber.equalsIgnoreCase(callCursor.getString(callCursor.getColumnIndex(CallLog.Calls.NUMBER))) )
duration = callCursor.getInt(callCursor.getColumnIndex(CallLog.Calls.DURATION));
Log.d("SendCall.getDuration", duration.toString());
return duration;
}
上次通话的持续时间继续错误,该持续时间与上次通话相同呼叫。我不知道我能做到。
and the duration of the last call continues wrong, this duration is to the previous call. I don't know that i can do it.
推荐答案
在通话记录中添加ContentObserver,当发生任何更改时,它将通知您在callLog中,即如果将新呼叫添加到呼叫日志中,或者您已删除呼叫日志
Add a ContentObserver on call log which will notify you when anything is changed in callLog i.e. if new call is added to call log or you removed the call log
EDIT
尝试一下通话结束后获得新的通话记录,我用此代码得到正确的结果
Try this get new call log after call finishes I am getting right result with this code
通话结束后等待3-4秒,然后
Wait for 3-4 seconds after call finish and then
CursorLoader cursorLoader = new CursorLoader(mActivity,
CallLog.Calls.CONTENT_URI, null, null, null, null);
managedCursor = cursorLoader.loadInBackground();
int durationIndex = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
managedCursor.moveToFirst();
String duration =managedCursor.getString(duration)
这篇关于使用服务在CallLog上获得最后一次呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!