当我收到PHONE_STATE意图时,如何在下面创建一个呼叫信息对话框
答案 0 :(得分:0)
首先添加此权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
在您的活动中使用LoaderManager.LoaderCallbacks接口,如下所示:
abstract Loader onCreateLoader(int id, Bundle args)
//Instantiate and return a new Loader for the given ID.
abstract void onLoadFinished(Loader loader, D data)
//Called when a previously created loader has finished its load.
abstract void onLoaderReset(Loader loader)
//Called when a previously created loader is being reset, and thus making its data unavailable.
onCreateLoader()的实现:
@Override
public Loader onCreateLoader(int loaderID, Bundle args) {
Log.d(TAG, "onCreateLoader() >> loaderID : " + loaderID);
switch (loaderID) {
case URL_LOADER:
// Returns a new CursorLoader
return new CursorLoader(
this, // Parent activity context
CallLog.Calls.CONTENT_URI, // Table to query
null, // Projection to return
null, // No selection clause
null, // No selection arguments
null // Default sort order
);
default:
return null;
}
}
在onLoadFinished()方法中调用您的详细信息:
Override
public void onLoadFinished(Loader loader, Cursor managedCursor) {
Log.d(TAG, "onLoadFinished()");
StringBuilder sb = new StringBuilder();
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("<h4>Call Log Details <h4>");
sb.append("\n");
sb.append("\n");
sb.append("<table>");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int callTypeCode = Integer.parseInt(callType);
switch (callTypeCode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "Missed";
break;
}
sb.append("<tr>")
.append("<td>Phone Number: </td>")
.append("<td><strong>")
.append(phNumber)
.append("</strong></td>");
sb.append("</tr>");
sb.append("<br/>");
sb.append("<tr>")
.append("<td>Call Type:</td>")
.append("<td><strong>")
.append(dir)
.append("</strong></td>");
sb.append("</tr>");
sb.append("<br/>");
sb.append("<tr>")
.append("<td>Date & Time:</td>")
.append("<td><strong>")
.append(callDayTime)
.append("</strong></td>");
sb.append("</tr>");
sb.append("<br/>");
sb.append("<tr>")
.append("<td>Call Duration (Seconds):</td>")
.append("<td><strong>")
.append(callDuration)
.append("</strong></td>");
sb.append("</tr>");
sb.append("<br/>");
sb.append("<br/>");
}
sb.append("</table>");
managedCursor.close();
callLogsTextView.setText(Html.fromHtml(sb.toString()));
}
有关详细信息,请参阅以下参考资料: