BaseFragment 权限的反射方法修复
This commit is contained in:
parent
d3e5a6c72e
commit
73a6392629
|
|
@ -118,7 +118,7 @@ public abstract class BaseFragment extends Fragment {
|
|||
}
|
||||
|
||||
private void requestPermission(String[] permission, boolean show_doNotAskAgain, Listener listener) {
|
||||
List<IPermission> iPermissionList = convertToIPermissionArray(requireActivity(),permission);
|
||||
List<IPermission> iPermissionList = convertToIPermissionArray(permission);
|
||||
if (iPermissionList.isEmpty()){
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,61 +7,43 @@ import com.hjq.permissions.permission.base.IPermission;
|
|||
import com.hjq.toast.Toaster;
|
||||
import com.tfq.library.R;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PermissionUtils {
|
||||
|
||||
public static IPermission invokeGetCachePermission(Context context, String permission) {
|
||||
public static IPermission invokeGetCachePermission(String permission) {
|
||||
try {
|
||||
Class<?> xxPermissionsClass = Class.forName("com.hjq.permissions.permission.PermissionLists");
|
||||
// 尝试获取方法,可能需要调整方法名和参数
|
||||
Method method = null;
|
||||
try {
|
||||
// 尝试获取静态方法
|
||||
method = xxPermissionsClass.getDeclaredMethod("getCachePermission", Context.class, String.class);
|
||||
} catch (NoSuchMethodException e1) {
|
||||
try {
|
||||
// 尝试获取实例方法
|
||||
method = xxPermissionsClass.getDeclaredMethod("getCachePermission", Context.class, String.class);
|
||||
} catch (NoSuchMethodException e2) {
|
||||
// 尝试其他可能的方法名
|
||||
String[] possibleMethodNames = {
|
||||
"getPermissionCache",
|
||||
"getCachedPermission",
|
||||
"checkCachedPermission"
|
||||
};
|
||||
for (String methodName : possibleMethodNames) {
|
||||
try {
|
||||
method = xxPermissionsClass.getDeclaredMethod(methodName, Context.class, String.class);
|
||||
break;
|
||||
} catch (NoSuchMethodException e3) {
|
||||
// 继续尝试下一个方法名
|
||||
}
|
||||
}
|
||||
if (method == null) {
|
||||
throw new NoSuchMethodException("No suitable cache method found");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 设置可访问
|
||||
// 修正1: 使用正确的类名 PermissionUtils
|
||||
Class<?> permissionUtilsClass = Class.forName("com.hjq.permissions.permission.PermissionUtils");
|
||||
// 修正2: 方法只需要一个String参数
|
||||
Method method = permissionUtilsClass.getDeclaredMethod("getCachePermission", String.class);
|
||||
// 设置可访问(因为方法是private的)
|
||||
method.setAccessible(true);
|
||||
// 调用方法
|
||||
// 修正3: 调用时传入null(静态方法)和permission参数
|
||||
Object result = method.invoke(null, permission);
|
||||
// 处理返回值
|
||||
if (result instanceof IPermission) {
|
||||
return (IPermission) result;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
} catch (ClassNotFoundException e) {
|
||||
Log.e("Reflection", "PermissionUtils class not found", e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
Log.e("Reflection", "getCachePermission method not found", e);
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e("Reflection", "Illegal access to method", e);
|
||||
} catch (InvocationTargetException e) {
|
||||
Log.e("Reflection", "Method invocation failed", e);
|
||||
} catch (Exception e) {
|
||||
Log.e("Reflection", "Failed to invoke cache method", e);
|
||||
return null;
|
||||
Log.e("Reflection", "Unexpected error", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<IPermission> convertToIPermissionArray(Context context, String[] permissions) {
|
||||
public static List<IPermission> convertToIPermissionArray(String[] permissions) {
|
||||
// 将String[]转换为List<IPermission>
|
||||
List<IPermission> permissionList = new ArrayList<>();
|
||||
|
||||
|
|
@ -71,7 +53,7 @@ public class PermissionUtils {
|
|||
for (int i = 0; i < permissions.length; i++) {
|
||||
String permission = permissions[i];
|
||||
// 1. 尝试通过反射获取缓存的IPermission
|
||||
IPermission cachedPermission = invokeGetCachePermission(context, permission);
|
||||
IPermission cachedPermission = invokeGetCachePermission(permission);
|
||||
if (cachedPermission != null) {
|
||||
permissionList.add(cachedPermission);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ public class Activity_Font extends BaseActivity {
|
|||
selectedScaleIndex(4);
|
||||
}
|
||||
|
||||
IPermission iPermission = PermissionUtils.invokeGetCachePermission(this, Manifest.permission.WRITE_SETTINGS);
|
||||
IPermission iPermission = PermissionUtils.invokeGetCachePermission(Manifest.permission.WRITE_SETTINGS);
|
||||
assert iPermission != null;
|
||||
boolean granted = XXPermissions.isGrantedPermission(this, iPermission);
|
||||
if (!granted) {
|
||||
|
|
@ -96,7 +96,7 @@ public class Activity_Font extends BaseActivity {
|
|||
}
|
||||
|
||||
private void onClick(View view) {
|
||||
IPermission iPermission = PermissionUtils.invokeGetCachePermission(this, Manifest.permission.WRITE_SETTINGS);
|
||||
IPermission iPermission = PermissionUtils.invokeGetCachePermission(Manifest.permission.WRITE_SETTINGS);
|
||||
assert iPermission != null;
|
||||
boolean granted = XXPermissions.isGrantedPermission(this, iPermission);
|
||||
int viewId = view.getId();
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1 +1 @@
|
|||
f0eda3a10182298e5b5a7eb08d8b5be2
|
||||
ed7a88b64be20ded1998126dd6631842
|
||||
|
|
@ -1 +1 @@
|
|||
1dc088f66268b24631ff07de18d01653da892cdd
|
||||
b217048d6df289c811e4cc301d51e987ae425efe
|
||||
|
|
@ -1 +1 @@
|
|||
4cefbfc564c782e9184dc9c20a61fe26283f400f0d7348aa65dfe8b89c220844
|
||||
a552d964e098aac65b1fc88b4584a1fc0695d36865a60f7d7724bc19efb9b7cd
|
||||
|
|
@ -1 +1 @@
|
|||
b4c4dd1014aa3e9309c6e5cc331d0f61124b63e36d6c4036736893647bdaad0a387a3cc3c51abae8f02d7e791ff5ef981341c377c19f9fe640daa2683d890c2a
|
||||
4e2c98f15c47bf6ae40857075d4ced8cb651ecea8375b1483e18ecf4dc028efe775adfe306909215ee163b933327ed46d486f7973f01f37c0f5f95bb71794a0f
|
||||
|
|
@ -9,6 +9,6 @@
|
|||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
</versions>
|
||||
<lastUpdated>20251120071036</lastUpdated>
|
||||
<lastUpdated>20251120073059</lastUpdated>
|
||||
</versioning>
|
||||
</metadata>
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
a1cb3163f7ad33a5fa553a734f7ac26b
|
||||
895299e42034a995a2d06c1332709ff8
|
||||
|
|
@ -1 +1 @@
|
|||
08fffc3d17a1b097aa7cc6f33e7cda6e9e8b0ec8
|
||||
d5bf2a48888eba42d253b74ec112263bdd1fc45e
|
||||
|
|
@ -1 +1 @@
|
|||
66de8d40ce49c840f6ef77323221838b1e74d2650dfec1020d224a0f174fce0d
|
||||
e19f6fa0a92ad456670d63a5623b935077e7c42ff5461b1142f67813c37d4177
|
||||
|
|
@ -1 +1 @@
|
|||
88dd8c130d66ff16de744ca0b6b942b22e4b73025e255605ad01b02d4b869dc190fbed217e7b5dc738dea68867ae8ef7ad0bb2c12f90bae08e2cb403400d38c7
|
||||
cc7bd44d0ceb26b591acf7780d97917c9498051256b05f1294c716facdf459c62e573455f892fcfe93f78a837a1e71bedcec66fcb50bb619b685ef1d442bd10a
|
||||
Loading…
Reference in New Issue