pop up wiindow with notification even apllication is not running(即使应用程序未运行,也会弹出带有通知的窗口)
问题描述
这是我的代码,我会按时收到通知,但应用程序在弹出过程中出现 ctrash
here is my code and I am getting my notification on time but the application is being ctrash during pop up
private void showCustomPopupMenu()
{
WindowManager windowManager2 = (WindowManager) App.getAppContext().getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater=(LayoutInflater)App.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.window_popup_medicine, null);
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
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);
params.gravity=Gravity.CENTER|Gravity.CENTER;
params.x=0;
params.y=0;
assert windowManager2 != null;
windowManager2.addView(view, params);
}
我得到了这样的错误:
Unable to add window android.view.ViewRootImpl$W@46b5050 -- permission denied for window type 2038
我已添加所有权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
tools:ignore="ProtectedPermissions" />
请解决这个问题,非常感谢您的回答并提前感谢您
please resolve this I would really appreciate your answer and thank you in advance
推荐答案
您需要有 ACTION_MANAGE_OVERLAY_PERMISSION 权限才能打开/显示 Alert
You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission
android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
tools:ignore="ProtectedPermissions" />
设置警报类型为TYPE_APPLICATION_OVERLAY".
set alert type of "TYPE_APPLICATION_OVERLAY".
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
将您的 TYPE_PHONE
更改为 TYPE_SYSTEM_ALERT
您还应该参考 这个 回答.如果您仍有疑问,请告诉我.
You should also refer this answer. If still you have doubt let me know.
现在当通知到达时,检查一下:
Now when notification arrived, check this:
public void notificationArrived(String myMsg){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
final boolean overlayEnabled = Settings.canDrawOverlays(MyFirebaseMessagingService.this);
Global.printLog("showTaskDetailPopup==", "overlayEnabled" + overlayEnabled);
if (!overlayEnabled) return;
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
final Dialog dialog = new Dialog(MyFirebaseMessagingService.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.window_popup_medicine);
dialog.setCancelable(true);
TextView tv_msg = dialog.findViewById(R.id.tv_msg);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
} else {
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
dialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
这篇关于即使应用程序未运行,也会弹出带有通知的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:即使应用程序未运行,也会弹出带有通知的窗口
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01