码迷,mamicode.com
首页 > 其他好文 > 详细

钢镚儿冲刺一周期第五天

时间:2020-04-22 18:18:20      阅读:65      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!