慕友帮忙看看这是什么问题,谢谢了
慕友,大神们,看看这是什么问题
慕友,大神们,看看这是什么问题
2016-10-11
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //验证授权 仅支持6.x以上 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS )== PackageManager.PERMISSION_DENIED){ //请求权限 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 123); }else { PrintContacts(); } } //权限请求结果 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case 123: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { PrintContacts(); } } } //打印联系人 private void PrintContacts (){ ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, new String[]{Contacts._ID, Contacts.DISPLAY_NAME, }, null,null,null); if(cursor!=null &&cursor.moveToFirst()){ while (!cursor.isAfterLast()){ Log.i("info", cursor.getString(cursor.getColumnIndex(Contacts._ID))+"---"+ cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME))); cursor.moveToNext(); } } } }
举报