我正在创建一个Dialog,显示特定应用程序所需的权限。问题是我不能在不扩展Activity的类中使用getIntent()和getPackageInfo,所以我该怎么办?代码就是这个。
public class DialogPermissions extends DialogFragment{
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
String value2;
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_permissions, null);
builder.setView(view);
TextView permissions = (TextView)view.findViewById(R.id.permissions);
Bundle extras = getIntent().getExtras();
if(extras!=null) {
value2 = getIntent().getStringExtra("packageApplication");
}
try {
PackageInfo packageInfo = getPackageInfo(value2, PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if ( requestedPermissions != null ) {
for (int i = 0; i < requestedPermissions.length; i++) {
}
}
}
catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
builder.setTitle("Permission")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}
}
答案 0 :(得分:4)
您可以像这样使用它 -
Intent i = getActivity().getIntent();
和 -
getActivity().getPackageManager().getPackageInfo(packageName, flags)