安卓开发入门基础之获取手机联系人以及联系电话

Belle ·
更新时间:2024-09-20
· 627 次阅读

AndroidManifest.xml 需要添加获取手机联系人权限 xml.java 主要就是垂直线性布局,里面放2个TextView和1个Button按钮 MainActivity.java public class MainActivity extends AppCompatActivity { private Cursor cursor; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = findViewById(R.id.text); } public void btn(View view) { try{ ContentResolver contentResolver = getContentResolver(); Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; cursor = contentResolver.query(uri, null, null, null,null); text.setText(""); while (cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); text.append("姓名:"+name+" "); text.append("电话号码:"+number); text.append("\n"); } cursor.close(); }catch (Exception ignore){ Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show(); } } } 设计效果图 刚进入APP

原创文章 19获赞 55访问量 4712 关注 私信 展开阅读全文
作者:时光踏路已久



安卓开发入门 开发入门 电话 联系 手机

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章