无法添加 window android.view.ViewRootImpl$W@3660b6 -- window 类型 2038 的权限被拒绝
Unable to add window android.view.ViewRootImpl$W@3660b6 -- permission denied for window type 2038
您好,在下面的代码中,我遇到了 windowmanger 的点击错误。
对于下面的代码,我得到这个 error.trying 来点击我无法打开的按钮 window
任何人都可以。Window 管理器添加视图引发错误
无法添加 window android.view.ViewRootImpl$W@3660b6 -- window 类型 2038
的权限被拒绝
private void createPdf(int position){
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
try {
WindowManager windowManager = (WindowManager)getActivity().getSystemService(WINDOW_SERVICE);
//here is all the science of params
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.pdf_layout, null);
LinearLayout pdf=popupView.findViewById(R.id.pdf);
TextView account_name=popupView.findViewById(R.id.account_name);
TextView street=popupView.findViewById(R.id.street);
TextView city=popupView.findViewById(R.id.city);
TextView district=popupView.findViewById(R.id.district);
TextView state=popupView.findViewById(R.id.state);
TextView opp_no=popupView.findViewById(R.id.opp_no);
TextView date=popupView.findViewById(R.id.date);
TextView contact_name=popupView.findViewById(R.id.contact);
String account=listSalesStageOpportunity.get(position).getRelated();
account_name.setText(account);
String opp=listSalesStageOpportunity.get(position).getPotentialNo();
opp_no.setText("Ref No:" + opp);
String cureent_date = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()).format(new Date());
date.setText("Date:" +cureent_date);
String contactname=listSalesStageOpportunity.get(position).getContact();
contact_name.setText("Kind Attn :" +contactname);
SynFields synFields = productList.get(position);
String product_id= String.valueOf(synFields.getValue());
Log.d("product_id",product_id);
Handler hand=new Handler();
windowManager.addView(popupView, params);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(params.x, params.y, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
// bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);
// paint.setColor(Color.GRAY);
//canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
// write the document content
String targetPdf = "/sdcard/pdffromlayout2.pdf";
File filePath;
filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
Toast.makeText(getActivity(), "PDF is created!!!", Toast.LENGTH_SHORT).show();
openGeneratedPDF();
} catch (Exception e) {
Handler hand = new Handler();
e.printStackTrace();
}
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
WindowManager windowManager = (WindowManager) getActivity().getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.pdf_layout, null);
params.gravity = Gravity.TOP | Gravity.CENTER;
params.x = ((getContext().getResources().getDisplayMetrics().widthPixels) / 2);
params.y = ((getContext().getResources().getDisplayMetrics().heightPixels) / 2);
windowManager.addView(popupView, params);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(params.x, params.y, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
// Canvas canvas = page.getCanvas();
// Paint paint = new Paint();
// canvas.drawPaint(paint);
// bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);
// paint.setColor(Color.GRAY);
//canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
// write the document content
String targetPdf = "/sdcard/pdffromlayout2.pdf";
File filePath;
filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
Toast.makeText(getActivity(), "PDF is created!!!", Toast.LENGTH_SHORT).show();
openGeneratedPDF();
}
}
可能您没有在清单中请求 android.permission.SYSTEM_ALERT_WINDOW
许可。
您应该将其添加到应用程序中 AndoridManifest.xml
的 manifest
标签下:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
并且在 运行 时间你应该直到用户授予你权限:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + context.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
您好,在下面的代码中,我遇到了 windowmanger 的点击错误。
对于下面的代码,我得到这个 error.trying 来点击我无法打开的按钮 window
任何人都可以。Window 管理器添加视图引发错误
无法添加 window android.view.ViewRootImpl$W@3660b6 -- window 类型 2038
的权限被拒绝private void createPdf(int position){
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
try {
WindowManager windowManager = (WindowManager)getActivity().getSystemService(WINDOW_SERVICE);
//here is all the science of params
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.pdf_layout, null);
LinearLayout pdf=popupView.findViewById(R.id.pdf);
TextView account_name=popupView.findViewById(R.id.account_name);
TextView street=popupView.findViewById(R.id.street);
TextView city=popupView.findViewById(R.id.city);
TextView district=popupView.findViewById(R.id.district);
TextView state=popupView.findViewById(R.id.state);
TextView opp_no=popupView.findViewById(R.id.opp_no);
TextView date=popupView.findViewById(R.id.date);
TextView contact_name=popupView.findViewById(R.id.contact);
String account=listSalesStageOpportunity.get(position).getRelated();
account_name.setText(account);
String opp=listSalesStageOpportunity.get(position).getPotentialNo();
opp_no.setText("Ref No:" + opp);
String cureent_date = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()).format(new Date());
date.setText("Date:" +cureent_date);
String contactname=listSalesStageOpportunity.get(position).getContact();
contact_name.setText("Kind Attn :" +contactname);
SynFields synFields = productList.get(position);
String product_id= String.valueOf(synFields.getValue());
Log.d("product_id",product_id);
Handler hand=new Handler();
windowManager.addView(popupView, params);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(params.x, params.y, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
// bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);
// paint.setColor(Color.GRAY);
//canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
// write the document content
String targetPdf = "/sdcard/pdffromlayout2.pdf";
File filePath;
filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
Toast.makeText(getActivity(), "PDF is created!!!", Toast.LENGTH_SHORT).show();
openGeneratedPDF();
} catch (Exception e) {
Handler hand = new Handler();
e.printStackTrace();
}
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
WindowManager windowManager = (WindowManager) getActivity().getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.pdf_layout, null);
params.gravity = Gravity.TOP | Gravity.CENTER;
params.x = ((getContext().getResources().getDisplayMetrics().widthPixels) / 2);
params.y = ((getContext().getResources().getDisplayMetrics().heightPixels) / 2);
windowManager.addView(popupView, params);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(params.x, params.y, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
// Canvas canvas = page.getCanvas();
// Paint paint = new Paint();
// canvas.drawPaint(paint);
// bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);
// paint.setColor(Color.GRAY);
//canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
// write the document content
String targetPdf = "/sdcard/pdffromlayout2.pdf";
File filePath;
filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
Toast.makeText(getActivity(), "PDF is created!!!", Toast.LENGTH_SHORT).show();
openGeneratedPDF();
}
}
可能您没有在清单中请求 android.permission.SYSTEM_ALERT_WINDOW
许可。
您应该将其添加到应用程序中 AndoridManifest.xml
的 manifest
标签下:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
并且在 运行 时间你应该直到用户授予你权限:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + context.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);