获取电话记录(Contentprovider)

1.需要获取权限    
 <uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
2.主函数
public class MainActivity extends AppCompatActivity {
    private String path = "content://call_log/calls";
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.lv);
        Cursor query = getContentResolver().query(Uri.parse(path), null, null, null, null);
        spq sp = new spq(this, query, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        lv.setAdapter(sp);
    }
}
3.listview适配器
public class spq extends CursorAdapter{
    public spq(Context context, Cursor c, int flags) {
        super(context, c, flags);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        View inflate = View.inflate(context, R.layout.buju, null);
        return inflate;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView tv1 =  view.findViewById(R.id.tv1);
        TextView tv2 =  view.findViewById(R.id.tv2);
        TextView tv3 =  view.findViewById(R.id.tv3);
        String phone = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
        long aLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
        String type = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
        tv1.setText(phone);
        String format = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date(aLong));
        tv2.setText(format);
        if(type.equals("1")){
            tv3.setText("接收");
        }else if(type.equals("2")){
            tv3.setText("发送");
        }
    }
}



4.主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity"
    android:orientation="vertical">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
</LinearLayout>


5.item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值