标签:play androi ada create nta err default contract android
public class MainActivity extends Activity implements OnClickListener { private Button but_old = null; private Button but_func = null; private ImageView iv_show = null; private Button button; ArrayAdapter<String> adapter; List<String> contactList = new ArrayList<>(); private ListView ListView_ReadContact; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.picture_tuise); but_func = (Button) findViewById(R.id.btn_one); but_old = (Button) findViewById(R.id.btn_two); iv_show = (ImageView) findViewById(R.id.imgShow); // but_func.setOnClickListener(this); // but_old.setOnClickListener(this); setContentView(R.layout.permission); button = (Button) findViewById(R.id.Button); ListView_ReadContact = (ListView) findViewById(R.id.ListView_ReadContact); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactList); ListView_ReadContact.setAdapter(adapter); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)!= // PackageManager.PERMISSION_GRANTED){ // ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE // },1); // }else { // call(); // } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 1); } else { readContact(); } } }); } public void readContact() { Cursor cursor = null; try { cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { //获取联系人姓名 String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String Num=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contactList.add(displayName+"\n"+Num); } adapter.notifyDataSetChanged(); } } catch (Exception e) { e.printStackTrace(); }finally { if (cursor!=null){ cursor.close(); } } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if (grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){ readContact(); }else { Toast.makeText(this,"未获取权限",Toast.LENGTH_SHORT).show(); } break; default: break; } }
标签:play androi ada create nta err default contract android
原文地址:https://www.cnblogs.com/suanai/p/12753840.html