您的位置:首页 > 移动开发 > Android开发

Android开发之获取通话记录

2017-08-24 14:48 253 查看
上一篇讲的是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>


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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android android开发