gpt4 book ai didi

android - 当应用程序在后台时,如何像弹出通知一样显示 whatsapp?

转载 作者:可可西里 更新时间:2023-11-01 11:42:34 26 4
gpt4 key购买 nike

我必须在我的应用程序中设置提醒。因此,一旦到达提醒时间,应用程序必须显示一个弹出窗口(即使应用程序未运行),就像 WhatsApp 未运行时如何在弹出窗口中显示消息一样

enter image description here

点击按钮后,我还必须启动我的应用程序。如何从后台显示一个弹出窗口?有 sample 吗?提前致谢

最佳答案

您可以使用 SYSTEM_ALERT_WINDOW从您的 BroadcastReceiver 显示一个对话框窗口,该窗口将显示在所有其他应用程序的顶部。

先添加权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

在 Manifest 中,然后在你的 onReceiver 中,如下创建一个 AlertDialog

@Override
public void onReceive(final Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
builder.setView(dialogView);
final AlertDialog alert = builder.create();
alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alert.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
}

关于android - 当应用程序在后台时,如何像弹出通知一样显示 whatsapp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35696441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com