Andrews permission to dynamically add more than 6.0 super-simple template

Today is "click", the "hole", "Boom," I do not know who marching in transit robbery. Really fast, tiger! ! ! Closer to home:

Description: The recently did a download apk from the server to the phone memory, then install and open the pdf file to download directly from the server to open a browser 2 small features.

Question: permission problems (Andrews 6.0 useless directly add permissions AndroidManifest.xml, must be dynamic access rights)

Specific code problems encountered:

                  1.Pk file or pdf file is not downloaded to the phone inside, and finally find out the reasons, because there is no store to get permission

                  After 2.pdf file is downloaded to your phone, Click for permission to store, display a blank page, you must return to the previous re-enter. (There is a small problem: directly open the pdf file, it will first display a blank page, and then display the pdf file contents)

Solution: storage rights (WRITE_EXTERNAL_STORAGE), for example, the beginning do not know what went wrong, the Internet to find a lot of debugging code (get a permission to write 50 lines of code, I really served me),

                 Finally he opened a thread, get special privileges, too complicated.

     Today, suddenly epiphany, angrily, delete all the original code, finally I wrote two very simple little method, but still successfully resolved previous issues.

Specific code:

public void checkPemission() {
int hasWritePermission = PermissionChecker.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWritePermission != PermissionChecker.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 9);
} else {
downFile();
}
}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 9 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
downFile();
}
}
 downFile () ---- download file their own way, not posted, as well as install apk files, directly open method pdf file is displayed, there is little need partners direct message or add qq: 2832440535 exchanges, learn from each other improve together!

Guess you like

Origin www.cnblogs.com/dmrbell/p/11263038.html