英文:
Android: Issues with my asking for permissions. Code inside
问题
以下是您提供的代码的翻译部分:
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Button getPermissions = (Button) getView().findViewById(R.id.get_permissions);
TextView permissionsText = (TextView) getView().findViewById(R.id.permissions_text);
getPermissions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
permissionsText.setText("已授权");
permissionsText.setTextColor(Color.GREEN);
} else {
requestPermission();
}
}
});
}
private void requestPermission() {
ActivityCompat.requestPermissions(getActivity(), new String[]
{
ACCESS_FINE_LOCATION,
ACCESS_COARSE_LOCATION,
ACCESS_BACKGROUND_LOCATION,
CALL_PHONE,
INTERNET,
READ_EXTERNAL_STORAGE,
VIBRATE,
USE_FULL_SCREEN_INTENT
}, RequestPermissionCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean fineLocPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean coarseLocPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean backgroundLocPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
boolean phonePermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;
boolean internetPermission = grantResults[4] == PackageManager.PERMISSION_GRANTED;
boolean readExternalStoragePermission = grantResults[5] == PackageManager.PERMISSION_GRANTED;
boolean vibratePermission = grantResults[6] == PackageManager.PERMISSION_GRANTED;
boolean fullScreenIntentPermission = grantResults[7] == PackageManager.PERMISSION_GRANTED;
if (fineLocPermission && coarseLocPermission && backgroundLocPermission &&
phonePermission && internetPermission && readExternalStoragePermission &&
vibratePermission && fullScreenIntentPermission)
{
Toast.makeText(getActivity(), "已授予权限", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getActivity(), "权限已拒绝", Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int FirstPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_FINE_LOCATION);
int SecondPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_COARSE_LOCATION);
int ThirdPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_BACKGROUND_LOCATION);
int FourthPermissionResult = ContextCompat.checkSelfPermission(getContext(), CALL_PHONE);
int FifthPermissionResult = ContextCompat.checkSelfPermission(getContext(), INTERNET);
int SixthPermissionResult = ContextCompat.checkSelfPermission(getContext(), READ_EXTERNAL_STORAGE);
int SeventhPermissionResult = ContextCompat.checkSelfPermission(getContext(), VIBRATE);
int EighthPermissionResult = ContextCompat.checkSelfPermission(getContext(), USE_FULL_SCREEN_INTENT);
return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
FourthPermissionResult == PackageManager.PERMISSION_GRANTED &&
FifthPermissionResult == PackageManager.PERMISSION_GRANTED &&
SixthPermissionResult == PackageManager.PERMISSION_GRANTED &&
SeventhPermissionResult == PackageManager.PERMISSION_GRANTED &&
EighthPermissionResult == PackageManager.PERMISSION_GRANTED;
}
请注意,以上是代码的翻译,其中的注释、变量名等未包含在内。
英文:
I am trying to give the user an option to grant permission in my code as per Google best practices. The code below is what I have tried. The first time I ran it on the emulator it asked for a permission. Nothing else was done. No toast showed up, no green "Granted" text, etc. Logcat is silent on the issue. Does anyone see what have I missed? (Note: This code is inside a fragment).
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Button getPermissions = (Button)getView().findViewById(R.id.get_permissions);
TextView permissionsText = (TextView)getView().findViewById(R.id.permissions_text);
getPermissions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkPermission()){
permissionsText.setText("Granted");
permissionsText.setTextColor(Color.GREEN);
}
else {
requestPermission();
}
}
});
}
private void requestPermission() {
ActivityCompat.requestPermissions(getActivity(), new String[]
{
ACCESS_FINE_LOCATION,
ACCESS_COARSE_LOCATION,
ACCESS_BACKGROUND_LOCATION,
CALL_PHONE,
INTERNET,
READ_EXTERNAL_STORAGE,
VIBRATE,
USE_FULL_SCREEN_INTENT
}, RequestPermissionCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RequestPermissionCode:
if (grantResults.length > 0) {
boolean fineLocPermission = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean coarseLocPermission = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean backgroundLocPermission = grantResults[2] == PackageManager.PERMISSION_GRANTED;
boolean phonePermission = grantResults[3] == PackageManager.PERMISSION_GRANTED;
boolean internetPermission = grantResults[4] == PackageManager.PERMISSION_GRANTED;
boolean readExternalStoragePermission = grantResults[5] == PackageManager.PERMISSION_GRANTED;
boolean vibratePermission = grantResults[6] == PackageManager.PERMISSION_GRANTED;
boolean fullScreenIntentPermission = grantResults[7] == PackageManager.PERMISSION_GRANTED;
if (fineLocPermission && coarseLocPermission && backgroundLocPermission && phonePermission && internetPermission && readExternalStoragePermission && vibratePermission && fullScreenIntentPermission)
{
Toast.makeText(getActivity(),"Permission Granted",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getActivity(),"Permission Denied",Toast.LENGTH_LONG).show();
}
}
break;
}
}
public boolean checkPermission() {
int FirstPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_FINE_LOCATION);
int SecondPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_COARSE_LOCATION);
int ThirdPermissionResult = ContextCompat.checkSelfPermission(getContext(), ACCESS_BACKGROUND_LOCATION);
int FourthPermissionResult = ContextCompat.checkSelfPermission(getContext(), CALL_PHONE);
int FifthPermissionResult = ContextCompat.checkSelfPermission(getContext(), INTERNET);
int SixthPermissionResult = ContextCompat.checkSelfPermission(getContext(), READ_EXTERNAL_STORAGE);
int SeventhPermissionResult = ContextCompat.checkSelfPermission(getContext(), VIBRATE);
int EigthPermissionResult = ContextCompat.checkSelfPermission(getContext(), USE_FULL_SCREEN_INTENT);
return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
ThirdPermissionResult == PackageManager.PERMISSION_GRANTED &&
FourthPermissionResult == PackageManager.PERMISSION_GRANTED &&
FifthPermissionResult == PackageManager.PERMISSION_GRANTED &&
SixthPermissionResult == PackageManager.PERMISSION_GRANTED &&
SeventhPermissionResult == PackageManager.PERMISSION_GRANTED &&
EigthPermissionResult == PackageManager.PERMISSION_GRANTED;
}
答案1
得分: 1
在Fragment中使用requestPermissions(arrayOfPermissions, REQ_CODE)
代替ActivityCompat.requestPermissions()
:
一个Fragment有一个名为requestPermissions()
的方法,我不是指你在代码中命名的方法。
然后,在Fragment中会正确地调用onRequestPermissionsResult
。
示例代码:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQ_CODE_PERMISSION);
}
英文:
In a Fragment use requestPermissions(arrayOfPermissions, REQ_CODE)
instead of ActivityCompat.requestPermissions()
:
A Fragment has a method named requestPermissions()
and I don't mean the method you named it in your code.
onRequestPermissionsResult
is then invoked correctly in the Fragment.
Sample Code:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQ_CODE_PERMISSION);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论