本文主要是介绍weiyi通讯录(四)获取通话记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
昨天晚上做了第二个模块,不过没做完,这里先显示所有的通话记录,由于明天要去面试 这里就不啰嗦了
-----------欢迎加入交流群 386451316 有问题一起讨论吧
图在上面 接下来就说代码 布局也就不说了
在上个模块没有使用这个 所以在这里使用 private AsyncQueryHandler asyncQuery;// 异步查询数据库类对象
直接上代码把
package com.zw.weiyi.fragment;import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;import android.annotation.SuppressLint;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;import com.zw.weiyi.adapter.CallAdapter;
import com.zw.weiyi.enety.Call;
import com.zw.weiyi.phonesms.R;
/*** 通话记录* @author Striver_zw* Date 2014-12-9*/
@SuppressLint("ValidFragment")
public class CallFragment extends Fragment{private ListView callListView;private AsyncQueryHandler asyncQuery;// 异步查询数据库类对象private CallAdapter adapter;private List<Call> callList;private Context context;private View view;//缓存页面public CallFragment(Context context) {this.context = context;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);} @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {if(view==null){view=inflater.inflate(R.layout.call_fragment,container, false);}ViewGroup parent = (ViewGroup) view.getParent();if(parent!=null){parent.removeView(view);//先移除}// 初始化initview(view);return view;}//初始化public void initview(View view) {callListView = (ListView)view.findViewById(R.id.call_log_list);asyncQuery = new MyAsyncQueryHandler(context.getContentResolver());init();}private void init() {Uri uri = android.provider.CallLog.Calls.CONTENT_URI;// 查询的列String[] projection = { CallLog.Calls.DATE, // 日期CallLog.Calls.NUMBER, // 号码CallLog.Calls.TYPE, // 类型CallLog.Calls.CACHED_NAME, // 名字CallLog.Calls._ID, // id};asyncQuery.startQuery(0, null, uri, projection, null, null,CallLog.Calls.DEFAULT_SORT_ORDER);}private class MyAsyncQueryHandler extends AsyncQueryHandler {public MyAsyncQueryHandler(ContentResolver cr) {super(cr);}@Overrideprotected void onQueryComplete(int token, Object cookie, Cursor cursor) {if (cursor != null && cursor.getCount() > 0) {callList = new ArrayList<Call>();SimpleDateFormat sfd = new SimpleDateFormat("MM-dd hh:mm");//时间格式Date date;cursor.moveToFirst(); // 游标移动到第一项for (int i = 0; i < cursor.getCount(); i++) {cursor.moveToPosition(i);date = new Date(cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE)));String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));String cachedName = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));// 缓存的名称与电话号码,如果它的存在int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));Call call = new Call();call.setId(id);call.setNumber(number);call.setName(cachedName);if (null == cachedName || "".equals(cachedName)) {call.setName(number);}call.setType(type);call.setDate(sfd.format(date));callList.add(call);}if (callList.size() > 0) {setAdapter(callList);}}super.onQueryComplete(token, cookie, cursor);}}private void setAdapter(List<Call> call) {adapter = new CallAdapter(context, call);callListView.setAdapter(adapter);}
}
这里说下适配器 看网上都是这么玩的 我也玩下咯
package com.zw.weiyi.adapter;import java.util.List;import com.zw.weiyi.enety.Call;
import com.zw.weiyi.phonesms.R;import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;/*** 通话记录适配器* @author Striver_zw* Date 2014-12-9*/
public class CallAdapter extends BaseAdapter {private Context context;private List<Call> callList;private LayoutInflater inflater;public CallAdapter(Context context, List<Call> calls) {this.context = context;this.callList = calls;this.inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {return callList.size();}@Overridepublic Object getItem(int position) {return callList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if (convertView == null) {convertView = inflater.inflate(R.layout.call_fragment_item,null);holder = new ViewHolder();holder.call_type = (ImageView) convertView.findViewById(R.id.call_type);holder.name = (TextView) convertView.findViewById(R.id.name);holder.number = (TextView) convertView.findViewById(R.id.number);holder.time = (TextView) convertView.findViewById(R.id.time);holder.call_btn = (TextView) convertView.findViewById(R.id.call_btn);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}Call call = callList.get(position);switch (call.getType()) {case 1:holder.call_type//判断电话状态.setBackgroundResource(R.drawable.ic_calllog_outgoing_nomal);break;case 2:holder.call_type.setBackgroundResource(R.drawable.ic_calllog_incomming_normal);break;case 3:holder.call_type.setBackgroundResource(R.drawable.ic_calllog_missed_normal);break;}holder.name.setText(call.getName());holder.number.setText(call.getNumber());holder.time.setText(call.getDate());addViewListener(holder.call_btn, call, position);return convertView;}private static class ViewHolder {ImageView call_type;TextView name;TextView number;TextView time;TextView call_btn;}//点击打击电话private void addViewListener(View view, final Call callLog,final int position) {view.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Uri uri = Uri.parse("tel:" + callLog.getNumber());Intent intent = new Intent(Intent.ACTION_CALL, uri);context.startActivity(intent);}});}
}
主要代码就是这些 好了不说
这篇关于weiyi通讯录(四)获取通话记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!