在另一个应用程序中显示对话框并将其置于其他应用程序之上 [英] Show Dialog box in another application and bring on front on top of other application

查看:68
本文介绍了在另一个应用程序中显示对话框并将其置于其他应用程序之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 aidl 将数据从一个应用程序发送到另一个应用程序.发送数据的应用程序在前面,接收数据的应用程序在后台向用户显示一个对话框.现在的问题是该对话框在我切换回该应用程序之前不可见.他们无论如何我可以显示对话框而无需切换回其他应用程序.就像来自 android 的电池电量不足对话框一样,它是一切的首要任务.

I am sending data from one application to another by using aidl. The application which sends the data is in front and the application which receives the data shows a dialog box to the user is in background. Now problem is that the dialog box is not visible until i switch back to that application. Is their anyway i can show the dialog without switching back to other application. Like a battery low dialog from android which comes on top off everything.

推荐答案

您可以使用 WindowManager 来实现这一点,接收数据的应用程序应该有一个 后台服务 .如果您要发布广播事件以将数据传递给其他应用程序,那么当应用程序接收到数据时,启动服务以显示对话框.

You can achieve this using WindowManager , the application which receives the data should have a background service . If you are posting a broadcast event to pass data to other application, then when the application receives the data , start a service to display the dialog.

dialog 可以是自定义对话框,需要将其作为视图添加到 WindowManager

The dialog can be a custom dialog , which need to be added as a view to WindowManager

好的,这是一个带有自定义对话框的示例.您可以使用默认的 AlertDialog

Ok, Here is a sample with a custom dialog. You can try same approach with the default AlertDialog

当应用程序收到事件时,启动服务并在 Service 中,当 onStartCommand 调用时,显示对话框.

When the application receive the event , start the service and In Service when onStartCommand called , show the dialog.

按照以下步骤.....

Follow this steps…..

获取WindowManager

     WindowManager  windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

膨胀自定义视图

     View dialogView = View.inflate(getBaseContext(),R.layout.dialog_layout,null);

向视图添加侦听器.

  dialogView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                windowManager.removeView(dialogView);
                dialogView = null;
            }
        });

设置窗口管理器参数

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
                PixelFormat.TRANSLUCENT);

     params.gravity = Gravity.CENTER | Gravity.CENTER;

将视图添加到Window Manager

   windowManager.addView(dialogView, params);

同时在清单中添加SYSTEM_ALERT_WINDOW权限

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

这篇关于在另一个应用程序中显示对话框并将其置于其他应用程序之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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