簡體   English   中英

requestPermissions() 沒有響應,當我單擊按鈕時沒有任何反應

[英]the requestPermissions() does not respond , nothing happen when I click the button

這是我的代碼,我像文檔說的那樣寫。 但是,當我單擊按鈕時沒有任何反應。 只有按摩節目:“D / CompatibilityChangeReporter:報告的兼容性更改ID:147798919;UID 10155;state:啟用”

Button bt_show_contacts_list=findViewById(R.id.button_show_contacts_list);
        bt_show_contacts_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS)==
                        PackageManager.PERMISSION_GRANTED){
                    // You can use the API that requires the permission
                    start();
                }else if(shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)){
                    Toast.makeText(getBaseContext(),"it's important , you are suppose to allow the permission",Toast.LENGTH_LONG).show();
                }else{
                    requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},1);
                }
            }
        });

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //Application specific request code to match with a result reported to TODO

        switch (requestCode){
            case 1:
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    start();
                }else{
                    Toast.makeText(getBaseContext(),"go to setting , otherwise you can't use this",Toast.LENGTH_LONG).show();
                }
                return;
        }

    }
    public void start(){
        Toast.makeText(getBaseContext(),"start",Toast.LENGTH_LONG).show();
    }

您需要在 AndroidManifest.xml 中添加此代碼,然后代碼將 wordk。

 <uses-permission android:name="android.permission.READ_CONTACTS"/> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM