此问题仅在安装后第一次运行应用程序时存在,在授予访问文件权限后登录按钮不起作用,在下一个应用程序运行时登录按钮正常工作。谁能告诉我与此相关的问题是什么。
为登录 Activity 提供代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
int hasWriteContactsPermissionW = ContextCompat.checkSelfPermission(login.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteContactsPermissionW != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(login.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to Files", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(login.this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS);
}
});
return;
}
ActivityCompat.requestPermissions(login.this,
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
loginbtn = findViewById(R.id.loginbtn);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MMMMMM", "CLICKEDDD");
loginbtn.setVisibility(View.GONE);
EditText emailtxt = findViewById(R.id.emailtxt);
String email = emailtxt.getText().toString();
EditText passtxt = findViewById(R.id.passwordtxt);
String pass = passtxt.getText().toString();
GlobalVar.hash = String.valueOf(hashing.md5encrypt(email + pass));
Connect();
}
});
最佳答案
您的代码一开始没有运行。这是因为权限检查器在第一次运行时授予权限时停止执行。我已经复制了您的代码 fragment 并注释了该行
if (hasWriteContactsPermissionW != PackageManager.PERMISSION_GRANTED) {
if(.....) {
// .... your code
}
return; // this statement terminates (stops ?) the function
// solution is to remove this line
}
// these lines are never reached because of the return; above
loginbtn = findViewById(R.id.loginbtn);
解决方案:
- 在
onCreate
结束时检查权限 - 从检查权限的 block 中删除
return;
修改后的代码:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (Build.VERSION.SDK_INT >= 24) {
try {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
} catch (Exception e) {
e.printStackTrace();
}
}
// moved this above
loginbtn = findViewById(R.id.loginbtn);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MMMMMM", "CLICKEDDD");
loginbtn.setVisibility(View.GONE);
EditText emailtxt = findViewById(R.id.emailtxt);
String email = emailtxt.getText().toString();
EditText passtxt = findViewById(R.id.passwordtxt);
String pass = passtxt.getText().toString();
GlobalVar.hash = String.valueOf(hashing.md5encrypt(email + pass));
Connect();
}
});
// moved this below
int hasWriteContactsPermissionW = ContextCompat.checkSelfPermission(login.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteContactsPermissionW != PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(login.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to Files", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(login.this, new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, REQUEST_CODE_ASK_PERMISSIONS);
}
});
return;
}
ActivityCompat.requestPermissions(login.this,
new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
}
关于android - 允许访问 Android 应用程序中授予的文件后,登录按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273485/