我正在创建一个应用程序,根据某些规则以编程方式断开调用!我遇到麻烦的是我试图在断开电话后获得最后一次通话的通话时间! 但问题是我得到的持续时间是之前的呼叫持续时间而不是断开连接的呼叫!
我的代码是:
public void disconnect(){
disconnectPhoneItelephony(context);
calldisconnected();
WhitelistDAO whiteListDao1 = new WhitelistDAO(context);
String dura = LastCall();
if (dura.equals("0")) {
int dbTime = Integer.parseInt(whiteListDao1.getLimit(finalNumber));
whiteListDao1.updateLimit(String.valueOf(dbTime), finalNumber);
} else {
whiteListDao1.updateLimit("0", finalNumber);
}
}
public String LastCall() {
String callDura = "0";
StringBuffer sb = new StringBuffer();
Uri contacts = CallLog.Calls.CONTENT_URI;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
Cursor managedCursor = getApplicationContext().getContentResolver().query(
contacts, null, null, null, android.provider.CallLog.Calls.DATE+ " DESC limit 1;");
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
if( managedCursor.moveToFirst() == true ) {
String phNumber = managedCursor.getString( number );
callDura = managedCursor.getString( duration1 );
String callDate = managedCursor.getString(date);
String callDayTime = new Date(Long.valueOf(callDate)).toString();
String dir = null;
Log.e("DUR","\nPhone Number:--- "+phNumber +" \nCall duration in sec :--- "+callDura +" \nCall Date in sec :--- "+callDayTime );
}
managedCursor.close();
return callDura;
}
答案 0 :(得分:2)
我认为这应该解决它! 在处理程序中以1秒的延迟运行该函数!然后,您将获得正确的最后呼叫持续时间,而不是之前的呼叫持续时间。我认为android在离开后不能立即获得最后的通话详情!可能需要第二次延迟才能插入数据库!
像:
private Handler handler = new Handler();
public void disconnect(){
disconnectPhoneItelephony(context);
calldisconnected();
handler.removeCallbacksAndMessages(null);
handler.postDelayed(new Runnable() {
@Override
public void run() {
WhitelistDAO whiteListDao1 = new WhitelistDAO(context);
String dura = LastCall();
if (dura.equals("0")) {
int dbTime = Integer.parseInt(whiteListDao1.getLimit(finalNumber));
whiteListDao1.updateLimit(String.valueOf(dbTime), finalNumber);
} else {
whiteListDao1.updateLimit("0", finalNumber);
}
}
}, 1000);
}