从 Activity 外部的 Web 视图显示 AlertDialog

问题描述 投票:0回答:3

我一直在开发一个类似于 Facebook 聊天头的功能 - 即使用户离开我们的网站,该功能也需要为他们启用

android service webview android-alertdialog
3个回答
4
投票

您可以通过将窗口类型设置为

Dialog
,从服务中显示
TYPE_SYSTEM_ALERT
。请记住使用传递给
JsResult
onJsAlert
实例来传达用户所采取的操作。

    // maintain reference to JsResult instance passed to onJsAlert, in order
    // communicate back the action taken by the user.
    private JsResult mResult; 

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            mResult = result;
            AlertDialog dialog = new AlertDialog.Builder(MyService.this)
                    .setTitle("Custom Dialog")
                    .setMessage("This is our custom dialog, not the one served by JsDialogHelper")
                    .setOnCancelListener(new CancelListener())
                    .setNegativeButton("Cancel", new CancelListener())
                    .setPositiveButton("Ok", new PositiveListener())
                    .create();
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialog.show();

            return true;
        }
    });

    private class CancelListener implements DialogInterface.OnCancelListener,
        DialogInterface.OnClickListener {

        @Override
        public void onCancel(DialogInterface dialogInterface) {
            mResult.cancel();
        }

        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            mResult.cancel();
        }
    }

    private class PositiveListener implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            mResult.confirm();
        }
    }

将所需的权限添加到清单文件中:

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

替代解决方案: 如果可以构建特定于 android webview 的 Web 应用程序,那么您可以创建 Javascript 接口,该接口将允许 Javascript 代码直接调用 Android 代码,而 Android 代码又会为您显示对话框。这避免了

JsDialogHelper
路线。

  1. 定义 JavaScript 接口:
public class WebAppInterface {
    Context context;

    WebAppInterface(Context c) {
        context = c;
    }

    // Annotation required for api >= 17
    @JavascriptInterface
    public void showDialog() {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle("Custom Dialog")
                .setMessage("This is our custom dialog, not the one served by JsDialogHelper")
                .create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }
}
  1. 使用 addJavascriptInterface 绑定 Javascript 和 Android 代码之间的接口。

webView.addJavascriptInterface(new WebAppInterface(this), "Android");

  1. 在 Javascript 代码中使用该接口。
<input type="button" value="Display dialog" onClick="displayDialog()" />
    <script>
        function displayDialog() {
            //alert("Javascript Dialog");
            Android.showDialog();
        }
    </script>
</input>

2
投票

您无法在

AlertDialog
之外显示
Activity

但是,您可以构建一个自定义的

View
,它看起来像您想要显示的对话框,并通过
WindowManager
将其显示在窗口中。

例如:

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

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

params.gravity = Gravity.CENTER;

windowManager.addView(yourView, params);

上面的代码在屏幕中央显示您的自定义视图,位于其他所有内容之上。您可以通过

LayoutInflater
来放大视图。

您还需要在清单中添加权限才能显示视图。

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

1
投票

我想您可以创建一个

JavaScript Interface
,以便在
activity
WebView
之间进行双向通信。我自己没有尝试过该解决方案,但之前曾遇到过这个问题。此处发布的SO问题可能会对您有所帮助。

我想你的

WebView
也将驻留在一个活动中,你可以使用活动的上下文来弹出
AlertDialog
。您只需要在活动中调用一个要在 Web 视图中调用的方法(我只需在
addJavascriptInterface
方法中传递 Activity 对象)。

另一种方法是使用服务并启动一个新的活动,该活动在其中实现

Theme.dialog
以用作AlertDialog。

如果这不能解决您的问题,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.