我不知道爲什麼,但我的運行時權限的代碼不起作用。不出現彈出。進度條顯示沒有我接受權限。問題與運行時權限
誰能幫幫我?
THNAKS提前每個人!
我告訴你我的代碼:!
@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;
}
Code關於權限:
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();
}
}
}
}
你的目標sdk是什麼?您正在測試的設備或虛擬設備的api級別是什麼? – Sufian
如果你想要一個簡單易行的代碼,你可以添加一個類 https://github.com/mptrista/PermissionHelper – toshkinl