我想使用static String getLastOutgoingCall()
方法来拉取上次拨打电话的时间,但我不知道怎么做!
我是java编程的初学者(我通常用c ++编程)
我发现的教程使用了古老的API,但没有一个使用我正在讨论的方法。
答案 0 :(得分:2)
我希望我没有误解你的问题。如果是的话,请告诉我。
根据the documentation,来自String getLastOutgoingCall (Context context)
的方法android.provider.CallLog.Calls
返回
拨打的最后一个电话号码(外拨)或空字符串(如果没有) 还存在。
因此,您无法使用该方法检索上一个拨出呼叫持续时间。
要获取上一个拨出电话的持续时间,您可以查询CallLog.Calls.CONTENT_URI
以检索此信息。
您可以使用以下方法:
public String getLastOutgoingCallDuration(final Context context) {
String output = null;
final Uri callog = CallLog.Calls.CONTENT_URI;
Cursor cursor = null;
try {
// Query all the columns of the records that matches "type=2"
// (outgoing) and orders the results by "date"
cursor = context.getContentResolver().query(callog, null,
CallLog.Calls.TYPE + "=" + CallLog.Calls.OUTGOING_TYPE,
null, CallLog.Calls.DATE);
final int durationCol = cursor
.getColumnIndex(CallLog.Calls.DURATION);
// Retrieve only the last record to get the last outgoing call
if (cursor.moveToLast()) {
// Retrieve only the duration column
output = cursor.getString(durationCol);
}
} finally {
// Close the resources
if (cursor != null) {
cursor.close();
}
}
return output;
}
注意:要执行此查询,您需要向清单添加以下权限:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
根据您自己的答案进行编辑:
您需要在活动的getLastOutgoingCallDuration()
方法上调用onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Here you need to set the name of your xml
TextView displayDuration;
displayDuration = (TextView) findViewById(R.id.textView2);
String duration = getLastOutgoingCallDuration(this);
displayDuration.setText(output + "sec");
}