Android开发之获取通话记录

上一篇讲的是Android开发之获取手机通讯录,这一篇博客也将针对手机联系人这一块进行开发。下面是获取手机通话记录的详细步骤:

1. 首先,我们需要新建一个类CallLogInfo,用于通话记录的数据封装,定义字符串。

public class CallLogInfo {
     public String number;
        public long date;
        public int type;
        public CallLogInfo(String number, long date, int type) {
            super();
            this.number = number;
            this.date = date;
            this.type = type;
        }
        public CallLogInfo() {
            super();
        }    
}

2.然后,主要是通过 context.getContentResolver()方法来获得通话记录 ,这个方法返回一个游标的数据类型,通过moveToNext()方法来获取所有的手机号码及其类型、时间,将获得的信息放入列表CallLogInfo中。

 public class ContactsMsgUtils {
    public  List<CallLogInfo> getCallLog(Context context) {
        List<CallLogInfo> infos = new ArrayList<CallLogInfo>();
        ContentResolver cr = context.getContentResolver();
        Uri uri = Calls.CONTENT_URI;
        String[] projection = new String[] { Calls.NUMBER, Calls.DATE,
                Calls.TYPE };
        Cursor cursor = cr.query(uri, projection, null, null, null);
        while (cursor.moveToNext()) {
            String number = cursor.getString(0);
            long date = cursor.getLong(1);
            int type = cursor.getInt(2);
            infos.add(new CallLogInfo(number, date, type));
        }
        cursor.close();
        return infos;
    }

}

3.注意!读取手机通话记录涉及了用户的隐私数据 ,需要在AndroidManifest文件中申请以下权限。

    <uses-permission    android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE" /> 

4.主要activity代码如下:

public class CallLogActivity extends Activity {
    private ListView lv;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts_msg_calllog);
        lv = (ListView) findViewById(R.id.callView);
        ContactsMsgUtils contactsMsgUtils = new ContactsMsgUtils();
        List<CallLogInfo> infos = contactsMsgUtils.getCallLog(this);
        adapter = new MyAdapter(infos);
        lv.setAdapter(adapter);
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                CallLogInfo info = (CallLogInfo) adapter.getItem(arg2);
                final String number = info.number;
                String[] items = new String[] { "复制号码到拨号盘, 拨号, 发送短信 "};
                new AlertDialog.Builder(CallLogActivity.this).setTitle("操作")
                        .setItems(items, new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                switch (which) {
                                case 0:
                                    startActivity(new Intent(
                                            Intent.ACTION_DIAL, Uri
                                                    .parse("tel:" + number)));
                                    break;
                                case 1:
                                    startActivity(new Intent(
                                            Intent.ACTION_CALL, Uri
                                                    .parse("tel:" + number)));
                                    break;
                                case 2:
                                    startActivity(new Intent(
                                            Intent.ACTION_SENDTO, Uri
                                                    .parse("sms:" + number)));
                                    break;

                                default:
                                    break;
                                }

                            }
                        }).show();

                return false;
            }
        });

    }
    private class MyAdapter extends BaseAdapter {
        private List<CallLogInfo> infos;
        private LayoutInflater inflater;

        public MyAdapter(List<CallLogInfo> infos) {
            super();
            this.infos = infos;
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {

            return infos.get(position);
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.call_log_item, null);
            TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
            TextView tv_date = (TextView) view.findViewById(R.id.tv_date);
            TextView tv_type = (TextView) view.findViewById(R.id.tv_type);
            CallLogInfo info = infos.get(position);
            tv_number.setText(info.number);
            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd hh:mm:ss");
            String dateStr = format.format(info.date);
            tv_date.setText(dateStr);
            String typeStr = null;
            int color = 0;
            switch (info.type) {
            case Calls.INCOMING_TYPE:
                typeStr = "来电";
                color = Color.BLUE;

                break;
            case Calls.OUTGOING_TYPE:
                typeStr = "去电";
                color = Color.GREEN;

                break;
            case Calls.MISSED_TYPE:
                typeStr = "未接";
                color = Color.RED;

                break;

            default:
                break;
            }
            tv_type.setText(typeStr);
            tv_type.setTextColor(color);
            return view;
        }

    }
}

5.与activity绑定的界面的代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CallLogActivity" >
<!-- id为@id/android:list的ListView为客户化的list布局,如果没有,则系统会调用默认的布局 -->  
    <ListView
         android:id="@+id/callView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scrollbars="vertical"
        android:visibility="visible" >
    </ListView>
</RelativeLayout>

最后,成功实现。截图如下:

这里写图片描述

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值