AndroidManifest.xml
< uses-permission android: name= " android.permission.READ_CONTACTS" />
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 ( ) ;
}
}
}
设计效果图
点击按钮显示手机里的联系人和电话 手机内存的联系人