我有问题。我想创建一个PreferenceActivity,其中安装在设备中的每个应用程序相对于Preference和Summary编写了应用程序所需的各种权限。我编写了这段代码,但它没有用,因为当我启动应用程序时没有显示任何内容。
public class Permission extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.layout.permission);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
Preference preference = new Preference(this);
preference.setTitle(applicationInfo.packageName);
try {
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
if(requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
preference.setSummary(requestedPermissions[i]);
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
布局是这个
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
</PreferenceScreen>
如果这个问题看起来很愚蠢,我很抱歉,但是我发疯了才找到解决方案。提前感谢您的帮助。