我使用了这个Topic
我尝试了此代码,但是没有用:
PACKAGE_NAME = context.getApplicationContext()。getPackageName();
try {
pi = context.getPackageManager().getPackageInfo(PACKAGE_NAME, PackageManager.GET_PERMISSIONS);
for (String perm : pi.requestedPermissions) {
Log.e("Foo", perm);
}
} catch (Exception e) {
}
但这不能帮助我。我有应用程序列表,我想获得在每个应用程序上使用的权限。 我该如何处理?
更新: 就像照片一样,当单击“دسترسیها”时,我想获得该应用程序中使用的权限。(例如,电报中:Internet,存储,通话,摄像头...)
更新2:
我将分享我的问题的适配器代码
我的适配器:
class AppViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
AppViewHolder(View itemView, Context context, List<App> apps) {
super(itemView);
txt_show_permission = itemView.findViewById(R.id.txt_show_permission);
/*The String Buffer For Permissions*/
appNameAndPermissions = new StringBuffer();
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
Log.d(TAG, "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
appNameAndPermissions.append(packageInfo.packageName + "*******:\n");
//Get Permissions
requestedPermissions = packageInfo.requestedPermissions;
if (requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
Log.d(TAG, requestedPermissions[i]);
appNameAndPermissions.append(requestedPermissions[i] + "\n");
}
appNameAndPermissions.append("\n");
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
在onBindViewHolder中的txt_show_permission上设置点击监听器:
holder.txt_show_permission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(String.valueOf(appNameAndPermissions));
}
});
适配器类中对话框的方法:
public void showDialog(String txtPermission) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.show_permission_dialog);
TextView txt_permission = dialog.findViewById(R.id.txt_permission);
Button btn_ok = dialog.findViewById(R.id.btn_ok);
txt_permission.setText(txtPermission);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
答案 0 :(得分:2)
您可以循环浏览所有应用程序名称,并获得它们的权限,并将其存储在字符串缓冲区中,如下所示: https://stackoverflow.com/a/14672557/10058326
或者由于您希望在单击按钮时显示权限,因此可以在OnButtonClickListener中为每个应用程序添加使用正确的应用程序名称尝试过的代码
编辑:有关如何为“回收者视图”创建OnItemClickListener
的信息,请参见以下链接。您可以获取被单击的行的位置,并通过该行获取该行中的应用名称,然后将其传递给另一个函数。然后在该函数中编写代码以获取传递的应用名称的权限并显示它
https://antonioleiva.com/recyclerview-listener/
https://gist.github.com/riyazMuhammad/1c7b1f9fa3065aa5a46f
编辑2:
无需将appNameAndPermissions
传递给包含整个列表的showDialog
,您需要从字符串缓冲区中提取特定应用程序的权限。方法如下:
String app_name = itemView.findViewById(R.id.app_name_text_view).getText().toString();
int indexOfApp = appNameAndPermissions.indexOf(app_name);
int indexOfLastPermission = appNameAndPermissions.indexOf("\n", indexOfApp);
String permissions = appNameAndPermissions.substring(indexOfApp, indexOfLastPermission);