当应用程序在后台时如何显示类似弹出通知的whatsapp? [英] How to show whatsapp like popup notification when app is in background?

查看:80
本文介绍了当应用程序在后台时如何显示类似弹出通知的whatsapp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to set reminder in my app. So, Once the reminder time is reached , app has to show one popup window (Even if the app is not running) , just like how WhatsApp shows messages in popup when it is not running

在点击按钮时,我还必须启动我的应用程序.如何从背景显示一个弹出窗口?有样品吗?预先感谢

On tapping the button I have to launch my app also. How can I display one popup from background? Is there any samples available? Thanks in advance

推荐答案

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

You can use SYSTEM_ALERT_WINDOW from your BroadcastReceiver to show one dialog window , which will be shown on top of all other apps.

首先添加权限

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

在清单中,然后在您的 onReceiver 中,如下创建一个AlertDialog

in Manifest , then in your onReceiver , Create one AlertDialog as follows

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

这篇关于当应用程序在后台时如何显示类似弹出通知的whatsapp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆