权限在设置中授予,但被忽略

时间:2017-06-08 16:49:38

标签: java android permissions

在全新安装时,当我点击按钮时,它将跳过所有内容,直到

Toast.makeText(getBaseContext(), "Unable to get Permission", Toast.LENGTH_LONG).show();

它应该请求权限,但由于某种原因,我第一次点击按钮它只是给我吐司,第二次尝试它请求,然后转到你可以启用权限的设置。启用权限后,当您再次单击该按钮时,它仍会要求启用它。我检查了手机的设置并启用了它,但仍然要求在每次点击时启用它,除了第一次。我在清单中也有<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>。我在API&gt; 23。 我不知道为什么它没有认识到授予权限,我也不知道为什么它在第一次点击时不起作用。任何想法为什么它不起作用?谢谢。 我遵循了本教程 http://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/

package com.example.brett.brightness;

import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.IBinder;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static SeekBar seekBar;
    private static TextView textView;
    private static final int SYSTEM_ALERT_WINDOW_PERMISSION_CONSTANT = 100;
    private static final int REQUEST_PERMISSION_SETTING = 101;
    private SharedPreferences permissionStatus;
    private boolean sentToSettings = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    permissionStatus = getSharedPreferences("permissionStatus", MODE_PRIVATE);
    setContentView(R.layout.activity_main);
    seekBar = (SeekBar) findViewById(R.id.seekBar);
    textView = (TextView) findViewById(R.id.progressText);
    seekBar.setMax(255);




    Button fab = (Button) findViewById(R.id.button2);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW)) {
                    //Show Information about why you need the permission
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Need Storage Permission");
                    builder.setMessage("This app needs storage permission.");
                    builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, SYSTEM_ALERT_WINDOW_PERMISSION_CONSTANT);
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    builder.show();
                } else if (permissionStatus.getBoolean(Manifest.permission.SYSTEM_ALERT_WINDOW, false)) {
                    //Previously Permission Request was cancelled with 'Dont Ask Again',
                    // Redirect to Settings after showing Information about why you need the permission
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Need Overlay Permission");
                    builder.setMessage("This app needs overlay permission.");
                    builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            sentToSettings = true;
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                            intent.setData(uri);
                            startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                            Toast.makeText(getBaseContext(), "Go to Permissions to Grant Overlay", Toast.LENGTH_LONG).show();
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    builder.show();
                } else {
                    //just request the permission
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, SYSTEM_ALERT_WINDOW_PERMISSION_CONSTANT);
                }


                SharedPreferences.Editor editor = permissionStatus.edit();
                editor.putBoolean(Manifest.permission.SYSTEM_ALERT_WINDOW, true);
                editor.commit();


            } else {
                //You already have the permission, just go ahead.
                proceedAfterPermission();
            }
        }
    });


}


private void proceedAfterPermission() {
    //We've got the permission, now we can proceed further
    Toast.makeText(getBaseContext(), "We got the Overlay Permission", Toast.LENGTH_LONG).show();
    //Log.d(TAG, "sending message");
    Intent intent = new Intent(this, DrawOverAppsService.class);
    startService(intent);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == SYSTEM_ALERT_WINDOW_PERMISSION_CONSTANT) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //The External Storage Write Permission is granted to you... Continue your left job...
            proceedAfterPermission();
        } else {
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SYSTEM_ALERT_WINDOW)) {
                //Show Information about why you need the permission
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Need Overlay Permission");
                builder.setMessage("This app needs overlay permission");
                builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();


                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, SYSTEM_ALERT_WINDOW_PERMISSION_CONSTANT);


                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.show();
            } else {
                Toast.makeText(getBaseContext(), "Unable to get Permission", Toast.LENGTH_LONG).show();
            }
        }
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_PERMISSION_SETTING) {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SYSTEM_ALERT_WINDOW) == PackageManager.PERMISSION_GRANTED) {
            //Got Permission
            proceedAfterPermission();
        }
    }
}


@Override
protected void onPostResume() {
    super.onPostResume();
    if (sentToSettings) {
        if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.SYSTEM_ALERT_WINDOW) == PackageManager.PERMISSION_GRANTED) {
            //Got Permission
            proceedAfterPermission();
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果您只是希望识别您的权限并且不需要在运行时请求它们,我找到了一个临时解决方案。右键单击您的应用程序文件夹,然后选择“打开模块设置”。转到“Flavors”选项卡并将“Target Sdk Version”设置为API 22.据我所知,这将授予您安装和运行应用程序的权限。到目前为止,它已经运行良好,没有任何缺点。如果我有问题,我会更新。