此代码用于测试应用程序在检查后是否具有Permission,如果您有权限则返回true,否则返回false。
但是当我运行应用程序停止工作时它不起作用,所以我想知道为什么它不起作用,如果这在代码中遗漏了一些东西:
EDIT2 - 对建议进行了一些更正,但仍无效:
public class CheckPermission extends Activity {
private final Context context;
private static final String MNC = "MNC";
int permReq = 0;
String permMan = "";
public CheckPermission(Context context) {
this.context = context;
}
//perMan can be any code number higher than 0
public void requestPermission(String permRequested){
switch (permRequested) {
case "CAMERA":
//Request for Camera
this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
this.permMan = Manifest.permission.CAMERA;
break;
case "INTERNET":
//Requesr for Internet
this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission.INTERNET);
this.permMan = Manifest.permission.INTERNET;
break;
case "STORAGE":
//Request for group Storage - Read_External_Storage & Write_External_Storage
this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission_group.STORAGE);
this.permMan = Manifest.permission_group.STORAGE;
break;
case "MICROPHONE":
//Request for group Microphone - Record_Audio
this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission_group.MICROPHONE);
this.permMan = Manifest.permission_group.MICROPHONE;
break;
case "LOCATION":
//Request for group Location - Acess_Fine_Location & Acess_Coarse_Location
this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission_group.LOCATION);
this.permMan = Manifest.permission_group.LOCATION;
break;
case "CALL":
//Requesr for call
this.permReq = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
this.permMan = Manifest.permission.CALL_PHONE;
break;
default:
break;
}
}
public boolean hasPermission( String permRequested){
final PackageManager pm = context.getPackageManager();
if(isMNC_Or_Higher()) {
requestPermission(permRequested);
Toast.makeText(this.context, "Is MNC - permMan: " + this.permMan + " Perm required: " + permReq, Toast.LENGTH_SHORT).show();
if (permReq != pm.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{this.permMan}, this.permReq);
return false;
}
}
return true;
}
//check if is 6.0 or higher
public boolean isMNC_Or_Higher(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return true;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == this.permReq) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permissão concedida", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permissão negada", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
ERROR:
PID:25504 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.content.Context android.content.Context.getApplicationContext()' 在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107) 在com.dev.kamui.patrulhacomunitaria.CheckPermission.hasPermission(CheckPermission.java:68) 在com.dev.kamui.patrulhacomunitaria.Pagina_Principal $ 1.onClick(Pagina_Principal.java:47) 在android.view.View.performClick(View.java:5198) 在android.view.View $ PerformClick.run(View.java:21147) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
requestPermissions需要像这样调用
requestPermissions(this, new String[]{this.permMan}, this.permReq);
如果您希望CheckPermission为Util类,则无需从Activity类继承它。也没有必要做" this.context = context;"在CheckPermission构造函数中。而是通过hasPermission方法将上下文发送到requestPermission方法。也可以在ActivityCompat.requestPermissions中使用它。然后在调用hasPermission的每个活动中实现onRequestPermissionsResult。
答案 1 :(得分:0)
活动不应该有你写的那样的构造函数。
活动本身是上下文的子类,因此您可以编写this
而不是context
:
this.permReq = ContextCompat.checkSelfPermission(this, Manifest.permission_group.STORAGE);
答案 2 :(得分:0)
在Android 6.0及更高版本中,您必须请求代码权限。
private static final int PERMISSION_REQUEST_CODE = 1;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_DENIED) {
Log.d("permission", "permission denied to SEND_SMS - requesting it");
String[] permissions = {Manifest.permission.SEND_SMS};
requestPermissions(permissions, PERMISSION_REQUEST_CODE);
}
}