我必须创建一个类似于 Android 通话记录的活动。我没有找到太多关于它的信息。我发现的是这个例子,它有一个主要活动,其中有 3 个按钮:
- 去电清单
- 来电列表
- 未接来电清单
每个按钮都会发送到另一个活动,该活动是一个包含每个呼叫详细信息的列表视图。
但我需要的是,首先将所有这个应用程序统一到一个布局中,该布局一次显示所有未接来电、拨出电话和拨入电话,按日期列出。我现在认为这应该很简单,但我无法做到,这是因为我向你寻求帮助。在此之后我应该做的下一件事是修改列表视图布局,以显示 Android 显示的通话记录,带有电话图标以及如果您触摸姓名则可以呼叫该人的选项。
但首先,我需要应用程序按照我需要的方式运行。
这是主布局的代码,其中有 3 个按钮:
public class CallLogsDemoActivity extends Activity {
public static ArrayList<CallLogModel> outgoingList;
public static ArrayList<CallLogModel> incomingList;
public static ArrayList<CallLogModel> missedcallList;
public static final int OUTGOING_CALLS = 1;
public static final int INCOMING_CALLS = 2;
public static final int MISSED_CALLS = 3;
public static final String CALL_TYPE = "TYPE";
private Button inBtn, outBtn, missedBtn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
outBtn = (Button) findViewById(R.id.button1);
inBtn = (Button) findViewById(R.id.button2);
missedBtn = (Button) findViewById(R.id.button3);
inBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showIncomingList();
}
});
outBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showOutGoingList();
}
});
missedBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showMissedList();
}
});
outgoingList = new ArrayList<CallLogModel>();
incomingList = new ArrayList<CallLogModel>();
missedcallList = new ArrayList<CallLogModel>();
new ReadLogs().execute();
}
private void showIncomingList() {
Intent intent = new Intent(this, CallLogs.class);
intent.putExtra(CALL_TYPE, INCOMING_CALLS);
startActivity(intent);
}
private void showMissedList() {
Intent intent = new Intent(this, CallLogs.class);
intent.putExtra(CALL_TYPE, MISSED_CALLS);
startActivity(intent);
}
private void showOutGoingList() {
Intent intent = new Intent(this, CallLogs.class);
intent.putExtra(CALL_TYPE, OUTGOING_CALLS);
startActivity(intent);
}
private String getDuration(long milliseconds) {
int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
if (hours < 1)
return minutes + ":" + seconds;
return hours + ":" + minutes + ":" + seconds;
}
private String getDateTime(long milliseconds) {
Date date = new Date(milliseconds);
//return DateFormat.getDateTimeInstance().format(new Date());
return date.toLocaleString();
}
private void readCallLogs() {
missedcallList.clear();
incomingList.clear();
outgoingList.clear();
Cursor callLogCursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, null, null,
android.provider.CallLog.Calls.DEFAULT_SORT_ORDER);
if (callLogCursor != null) {
while (callLogCursor.moveToNext()) {
String id = callLogCursor.getString(callLogCursor
.getColumnIndex(CallLog.Calls._ID));
String name = callLogCursor.getString(callLogCursor
.getColumnIndex(CallLog.Calls.CACHED_NAME));
String cacheNumber = callLogCursor.getString(callLogCursor
.getColumnIndex(CallLog.Calls.CACHED_NUMBER_LABEL));
String number = callLogCursor.getString(callLogCursor
.getColumnIndex(CallLog.Calls.NUMBER));
long dateTimeMillis = callLogCursor.getLong(callLogCursor
.getColumnIndex(CallLog.Calls.DATE));
long durationMillis = callLogCursor.getLong(callLogCursor
.getColumnIndex(CallLog.Calls.DURATION));
int callType = callLogCursor.getInt(callLogCursor
.getColumnIndex(CallLog.Calls.TYPE));
String duration = getDuration(durationMillis * 1000);
String dateString = getDateTime(dateTimeMillis);
if (cacheNumber == null)
cacheNumber = number;
if (name == null)
name = "No Name";
CallLogModel callLogModel = new CallLogModel(name, cacheNumber,
duration, dateString);
if (callType == CallLog.Calls.OUTGOING_TYPE) {
outgoingList.add(callLogModel);
} else if (callType == CallLog.Calls.INCOMING_TYPE) {
incomingList.add(callLogModel);
} else if (callType == CallLog.Calls.MISSED_TYPE) {
missedcallList.add(callLogModel);
}
}
callLogCursor.close();
}
}
private class ReadLogs extends AsyncTask<Void, Void, Void> {
/* Object */
ProgressDialog dialog;
/*
* Function name: onPreExecute Parameters: Void params Return: void
*/
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(CallLogsDemoActivity.this,
"Reading Call Logs...", "Please wait...", true);
}
/*
* Function name: doInBackground Parameters: Void params Return: void
*/
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
readCallLogs();
return null;
}
/*
* Function name: onPostExecute Parameters: Void result Return: void
*/
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
}
}
这是显示列表的另一个活动:
public class CallLogs extends Activity {
private ListView mainListView;
private ArrayAdapter<CallLogModel> listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
mainListView = (ListView) findViewById(R.id.listView);
mainListView.setSmoothScrollbarEnabled(true);
Bundle extras = getIntent().getExtras();
int callType = extras.getInt(CallLogsDemoActivity.CALL_TYPE);
if (callType == CallLogsDemoActivity.OUTGOING_CALLS)
listAdapter = new CallLogsArrayAdapter(this,
CallLogsDemoActivity.outgoingList);
else if (callType == CallLogsDemoActivity.INCOMING_CALLS)
listAdapter = new CallLogsArrayAdapter(this,
CallLogsDemoActivity.incomingList);
else if (callType == CallLogsDemoActivity.MISSED_CALLS)
listAdapter = new CallLogsArrayAdapter(this,
CallLogsDemoActivity.missedcallList);
mainListView.setAdapter(listAdapter);
}
public void initElements() {
listAdapter.notifyDataSetChanged();
}
}
如前所述,我尝试统一此代码以仅显示在一个布局中,并像 Android 那样一次显示所有调用类型,但没有实现。