1.需要获取权限
<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
2.主函数3.listview适配器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); } }
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布局<?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>
<?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>