我想使用static String getLastOutgoingCall()
方法来拉上最后一个传出电话的持续时间,但我不知道如何!我是Java编程的初学者(我通常在C 中编程)
我发现的教程使用了古代API,它们都没有使用我在说的方法。
我希望我不会误解您的问题。如果是这样,请告诉我。
根据文档,android.provider.CallLog.Calls
的方法String getLastOutgoingCall (Context context)
返回
如果没有 存在。
因此,您无法使用该方法检索最后一次传出的呼叫持续时间。
要获取最后一次传出的呼叫持续时间,您可以查询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" />
根据您自己的答案进行编辑:
您需要在活动的onCreate()
方法上调用getLastOutgoingCallDuration()
:
@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");
}