获取电话记录(Contentprovider)

1.需要获取权限    
 
  
 
 
  
 
  
 
  
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.主布局
 
  



    



 
  
5.item布局 
  
 
  


    
    
    


你可能感兴趣的:(获取电话记录(Contentprovider))