钢镚儿冲刺一周期第五天

一、说在前面

  今天打算先放下在权限前面弹框,先学会如何动态的申请权限。

二、大体思路

  有之前小账本的经验,今天先写一个申请权限的例子,再写的过程当中寻找灵感。

三、代码

  

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;
        }
    }

四、效果

 

 

posted @ 2020-04-22 18:02  酸奶面包  阅读(119)  评论(0编辑  收藏  举报