如何在应用程序处于后台时显示类似弹出通知的whatsapp?

时间:2016-02-29 09:52:22

标签: android android-popupwindow

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

enter image description here

点击按钮我也必须启动我的应用程序。如何从背景中显示一个弹出窗口?有样品吗?提前致谢

1 个答案:

答案 0 :(得分:2)

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

首先添加权限

<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);
}