放入碎片

时间:2018-02-05 08:39:36

标签: java android android-fragments android-activity android-permissions

我尝试在runtime permission内使用fragment

这是我的自定义权限的类代码:

public class MyPermissions {
    private String[] permissions = null;
    private Context context;
    private int permCode;
    private EventHandler handler = null;
    public MyPermissions(Context context, int permCode, String[] permissions) {

        this.context = context;
        this.permCode = permCode;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        }

        List<String> perms = new ArrayList<>();

        if (permissions != null) {
            for (String perm : permissions) {
                if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) {
                    perms.add(perm);
                }
            }
        }
        if (perms.size() > 0) {
            this.permissions = perms.toArray(new String[0]);
        }
    }

    public void doIfHasPermissions(EventHandler eventHandler) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) {
            if (context instanceof Activity){
                ((Activity) context).requestPermissions(permissions, permCode);
            }
            this.handler = eventHandler;
        } else {
            eventHandler.handle();
        }
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){

        if (requestCode == permCode){
            boolean success = true;
            for (int grantResult : grantResults){
                if (grantResult != PackageManager.PERMISSION_GRANTED){
                    success = false;
                }
            }

            if (success && handler != null){
                handler.handle();
            }
        }
    }
    public interface EventHandler {
         void handle();
    }
}

我这样叫这个班:

 public void checkPermissions() {
    String[] permissions = new String[]{
            Manifest.permission.CALL_PHONE};
    myPermissions = new MyPermissions(getActivity(),
            PHONE_PERM_CODE, permissions);
    MyPermissions.EventHandler permHandler = new MyPermissions.EventHandler() {
        @Override
        public void handle() {

            makeCall();
        }
    };

    myPermissions.doIfHasPermissions(permHandler);
}

void makeCall() {
    try {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel: + 193 194"));
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这个MyPermissions类在放入Activity时正常工作,但在片段中它第一次不起作用。第二次一切都是正确的。

有谁能告诉我如何重写我的自定义权限类? 感谢

1 个答案:

答案 0 :(得分:0)

你必须在MyPermission类的自定义类中进行更改,就像在android.support.v4.app.Fragment中一样,你应该只使用requestPermissions(这是android.support.v4.app.Fragment的实例方法)

实施例::::

如果您在某个活动中,请致电

ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST_CAMERA);

如果在片段中,只需致电

requestPermissions(new String[]{Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST_CAMERA);

更新::::

请查看是否运行Android Marshmallow 6.0,API 23

Android "onRequestPermissionsResult" not being called