运行时权限问题

时间:2016-08-28 17:38:22

标签: java android fragment android-permissions

我不知道为什么,但我的运行时权限代码不起作用。不会弹出。在没有我接受许可的情况下出现进度条。

谁能帮帮我?

感谢每一个人!

我告诉你我的代码:!

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the custom_spinner_items for this fragment

        View rootView = inflater.inflate(R.layout.fragment_four, container, false);



        Button mButton = (Button) rootView.findViewById(R.id.cazzo);

        mButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (hasPermissions()) {
                    // our app has permissions.
                    FiveFragment ff = new FiveFragment();
                    numeroTavolo = ff.showResultTavolo(v);

                    MyListFragment mlf = new MyListFragment();


               /* try {
                    mlf.showResultTotale(v);
                } catch (IOException e) {
                    e.printStackTrace();
                }*/

                    string2 = mlf.showResultTotale2(v);
                    totalepizze = mlf.showResultTotale(v);


                    ThreeFragment tf = new ThreeFragment();

                    string4 = tf.showResult(v);

                    totalebibite = tf.showResult2(v);
                    processingDialog.show();
                    mProgressbarAsync = new MakePDF();
                    mProgressbarAsync.execute();
                    new MakePDF(getContext()).execute();

                } else {
                    //our app doesn't have permissions, So i m requesting permissions.
                    requestPerms();
                }

               /* FiveFragment ff = new FiveFragment();
                numeroTavolo = ff.showResultTavolo(v);

                MyListFragment mlf = new MyListFragment();


               *//* try {
                    mlf.showResultTotale(v);
                } catch (IOException e) {
                    e.printStackTrace();
                }*//*

                string2 = mlf.showResultTotale2(v);
                totalepizze = mlf.showResultTotale(v);


                ThreeFragment tf = new ThreeFragment();

                    string4 = tf.showResult(v);

                    totalebibite = tf.showResult2(v);
             */
               /* string4=tf.showResult(v);
                totalebibite=tf.showResult2(v);*/

                   /* processingDialog.show();
                    mProgressbarAsync = new MakePDF();
                    mProgressbarAsync.execute();
                    new MakePDF(getContext()).execute();*/


            }

        });
        TextView titolo2 = (TextView) rootView.findViewById(R.id.result2);
        return rootView;
    }

关于许可的代码:

 private void requestPerms(){
            String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                requestPermissions(permissions,PERMS_REQUEST_CODE);
            }
        }

        private boolean hasPermissions(){

            int res = 0;
            //string array of permissions,
            String[] permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};

            for (String perms : permissions){
                res = checkCallingOrSelfPermission(getContext(),perms);
                if (!(res == PackageManager.PERMISSION_GRANTED)){
                    return false;

                }
            }
            return true;

        }






        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            boolean allowed = true;

            switch (requestCode){
                case PERMS_REQUEST_CODE:

                    for (int res : grantResults){
                        // if user granted all permissions.
                        allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
                    }

                    break;
                default:
                    // if user not granted permissions.
                    allowed = false;
                    break;

            }

            if (allowed){
                //user granted all permissions we can perform our task.
                //makeFolder();
                FiveFragment ff = new FiveFragment();
                numeroTavolo = ff.showResultTavolo(v);

                MyListFragment mlf = new MyListFragment();


                   /* try {
                        mlf.showResultTotale(v);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/

                string2 = mlf.showResultTotale2(v);
                totalepizze = mlf.showResultTotale(v);


                ThreeFragment tf = new ThreeFragment();

                string4 = tf.showResult(v);

                totalebibite = tf.showResult2(v);
                processingDialog.show();
                 mProgressbarAsync = new MakePDF();
                mProgressbarAsync.execute();
                new MakePDF(getContext()).execute();
            }
            else {
                // we will give warning to user that they haven't granted permissions.
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                        Toast.makeText(getContext(), "Storage Permissions denied.", Toast.LENGTH_SHORT).show();
                    }
                }
            }

        }

3 个答案:

答案 0 :(得分:1)

请尝试一下这对我有用。

  1. 我用于Fragment中的联系人权限

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (context.checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
            runReadContact();
        } else {
            ActivityCompat.requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, MyActivity.PERMISSION_CODE);
        }
    } else {
        runReadContact();
    }
    
  2. onRequestPermission()在MyActivity(活动类)中使用,如此

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
     switch (requestCode) {
       case MyActivity.PERMISSION_CODE:
        if (grantResults.length > 0)
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    runReadContact();
    
            }
        }
        break;
      }
    }
    
  3. 如果要在片段中使用onRequestPermission() 我们需要照顾。

    1)android.support.v4.app.Fragment; => import android.app.Fragment

    2)FragmentCompat.requestPermissions(...

    3)你在片段中使用这个“onRequestPermissionsResult(...”。

答案 1 :(得分:0)

你的tarketSdkVersion是什么?

运行时许可需要(23> = tarketSdkVerison)

检查你的应用

移动"手机>设置>申请>你的应用>权限"

你拒绝了你的许可。然后你运行你的应用程序

答案 2 :(得分:0)

  1. 调试应用程序检查hasPermissions()函数是否返回 是真是假,那么你的应用程序已经有了 许可它不会显示弹出窗口。
  2. 检查您的Manifest.xml文件。如果它已经定义了权限并且您在API级别23以下运行,它仍然不会显示弹出窗口。
  3. 在API级别为23(棉花糖)及以上的设备上运行您的应用程序。
  4. 希望这能解决您的问题。