Android风格的通话记录

时间:2013-10-15 08:55:28

标签: android listview call

我必须创建一个类似于android的通话记录的活动。我没有找到很多关于它的信息。我发现的是这个例子,它有一个主要活动,有3个按钮:

1-外拨电话清单 2-拨打电话清单 3-未接来电列表

每个按钮发送到另一个活动,其中包含每个电话的详细信息。

但我需要的是,首先将所有这个应用程序统一到一个唯一的布局中,一次显示所有错过的,outoging和ingoing调用,按日期列出。我现在认为这应该很简单,但是我无法做到这一点,这是因为我向你寻求帮助。接下来我要做的就是修改listview布局,以便在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那样,但是不要实现它。

1 个答案:

答案 0 :(得分:1)

更改此代码

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);

到此

        int callType = extras.getInt(CallLogsDemoActivity.CALL_TYPE);
    ArrayList<CallLogModel> collectiveList = new ArrayList<CallLogModel>();
    collectiveList.addAll(CallLogsDemoActivity.outgoingList);
    collectiveList.addAll(CallLogsDemoActivity.incomingList);
    collectiveList.addAll(CallLogsDemoActivity.missedcallList);
    listAdapter = new CallLogsArrayAdapter(this,collectiveList);
    mainListView.setAdapter(listAdapter);