如何在片段中使用公共方法
我正在拨打电话拦截器。我正在尝试运行在片段中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那个活动是主持您的片段吗?如果是这种情况,则可以在片段中使用
getActivity()
,然后将其施加到mainActivity
。但是,如果不是这种情况,请提供更多详细信息,因为您不应该尝试访问片段没有相关的任何活动。
Is that Activity hosting your Fragment? If that's the case, you can use
getActivity()
in your Fragment and cast it toMainActivity
.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.