如何在片段中使用公共方法

发布于 2025-01-17 11:05:15 字数 1731 浏览 1 评论 0原文

我正在拨打电话拦截器。我正在尝试运行在片段中的 mainActivity 中工作的代码。

    @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_READ_PHONE_STATE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getActivity(), "Permission granted" +PERMISSION_REQUEST_READ_PHONE_STATE , Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getActivity(), "Permission NOT granted: " + PERMISSION_REQUEST_READ_PHONE_STATE, Toast.LENGTH_SHORT).show();
            }

            return;
        }
    }
}

onCreateView=

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_call, container, false);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        if (getActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED || getActivity().checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED) {
            String[] permissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE};
            requestPermissions(permissions, PERMISSION_REQUEST_READ_PHONE_STATE);
        }
    }

    onRequestPermissionsResult();

    return view;
}

问题是 public void onRequestPermissionsResult 上面有一行,当我将鼠标悬停在它上面时,“onRequestPermissionsResult”显示此消息。我尝试了几种方法和途径,希望有人能帮我解决。

i am making a call blocker. I'm trying to run a code that works in an mainActivity in a fragment.

    @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_READ_PHONE_STATE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getActivity(), "Permission granted" +PERMISSION_REQUEST_READ_PHONE_STATE , Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getActivity(), "Permission NOT granted: " + PERMISSION_REQUEST_READ_PHONE_STATE, Toast.LENGTH_SHORT).show();
            }

            return;
        }
    }
}

onCreateView=

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_call, container, false);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        if (getActivity().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED || getActivity().checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED) {
            String[] permissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE};
            requestPermissions(permissions, PERMISSION_REQUEST_READ_PHONE_STATE);
        }
    }

    onRequestPermissionsResult();

    return view;
}

the problem is public void onRequestPermissionsResult There is a line above it, when I hover over it, "onRequestPermissionsResult" shows this message. I tried several methods and ways, I hope someone can help me solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2025-01-24 11:05:15

那个活动是主持您的片段吗?如果是这种情况,则可以在片段中使用getActivity(),然后将其施加到mainActivity

((MainActivity)getActivity()).onRequestPermissionsResult();

但是,如果不是这种情况,请提供更多详细信息,因为您不应该尝试访问片段没有相关的任何活动。

Is that Activity hosting your Fragment? If that's the case, you can use getActivity() in your Fragment and cast it to MainActivity.

((MainActivity)getActivity()).onRequestPermissionsResult();

However, if that's not the case, please provide more details, as you should not try to access any activities to which your fragment's not related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文