显示一个AlertDialog从web视图的活动之外 [英] Showing an AlertDialog from a Webview outside of an Activity

查看:147
本文介绍了显示一个AlertDialog从web视图的活动之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在类似Facebook的聊天头的功能 - 一种特性,它需要被激活为我们的用户,即使他们离开我们的应用程序去检查电子邮件等,这需要能够创建通过javascript AlertDialogs - 从用户切换我们的应用程序内的活动未受到影响的地方。这是确定的功能,用户离开应用程序时做别的事情关闭/隐藏 - 但它仍必须present他们回来的时候

除了使用服务我一直没能想到另一种方式来让用户与我们互动,而他们开做其他的事情。当我使用一个服务窗口管理,通过创建web视图中的WebView的JsDialogHelper检测的情况下是无法从显示AlertDialogs活动和prevents它。

任何想法,将围绕使这项工作最有帮助。我们需要让AlertDialogs弹出并成为互动式的,这个特性是有帮助的。我们需要的WebView坚持围绕活动的转换之间。

是否有可能延长JsDialogHelper这样我就可以把它与我的code的工作?有没有更好的办法让喜欢用webviews在一个Android应用程序,我还没想到的功能,Facebook的聊天头?我愿意完成重写,如果它得到了用户体验我在寻找。

解决方案

您可以显示对话框从服务,通过设置窗口类型为 TYPE_SYSTEM_ALERT 。请记住,沟通使用回传给 onJsAlert JsResult 例如用户所采取的行动。

  //维持引用传递给onJsAlert,为了JsResult实例
    //通信回由用户采取的动作。
    私人JsResult mResult;

    webView.setWebChromeClient(新WebChromeClient(){
        @覆盖
        公共布尔onJsAlert(web视图查看,URL字符串,字符串消息,JsResult结果){
            mResult =结果;
            AlertDialog对话框=新AlertDialog.Builder(MyService.this)
                    .setTitle(自定义对话框)
                    .setMessage(这是我们的自定义对话框,而不是一个接JsDialogHelper服务)
                    .setOnCancelListener(新CancelListener())
                    .setNegativeButton(取消,新CancelListener())
                    .setPositiveButton(OK,新PositiveListener())
                    。创建();
            。dialog.getWindow()的setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialog.show();

            返回true;
        }
    });

    私有类CancelListener实现DialogInterface.OnCancelListener,
        DialogInterface.OnClickListener {

        @覆盖
        公共无效OnCancel的(DialogInterface dialogInterface){
            mResult.cancel();
        }

        @覆盖
        公共无效的onClick(DialogInterface dialogInterface,int i)以{
            mResult.cancel();
        }
    }

    私有类PositiveListener实现DialogInterface.OnClickListener {

        @覆盖
        公共无效的onClick(DialogInterface dialogInterface,int i)以{
            mResult.confirm();
        }
    }
 

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

 <使用-权限的Andr​​oid:名称=android.permission.SYSTEM_ALERT_WINDOW/>
 

替代的解决方案: 如果Web应用程序可以建立具体到Android的WebView,那么你就可以创建Javascript的接口,这将使Javascript的code直接调用了Android code这反过来又为你显示的对话框。这就避免了 JsDialogHelper 路线。

  1. 定义的JavaScript接口:

 公共类WebAppInterface {
    上下文语境;

    WebAppInterface(上下文C){
        上下文= C;
    }

    //注释所需的API> = 17
    @JavascriptInterface
    公共无效的ShowDialog(){
        AlertDialog对话框=新AlertDialog.Builder(上下文)
                .setTitle(自定义对话框)
                .setMessage(这是我们的自定义对话框,而不是一个接JsDialogHelper服务)
                。创建();
        。dialog.getWindow()的setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();
    }
}
 

<醇开始=2>
  • 在使用绑定之间的Javascript和Android code接口,<一个href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object,%20java.lang.String)"相对=nofollow> addJavascriptInterface 。
  •   

    webView.addJavascriptInterface(新WebAppInterface(本),机器人);

    <醇开始=3>
  • 使用在Javascript code接口。
  •  &LT;输入类型=按钮值=显示对话框的onClick =displayDialog()/&GT;
        &LT;脚本&GT;
            传播displayDialog(){
                //警报(Javascript的对话);
                Android.showDialog();
            }
        &LT; / SCRIPT&GT;
    &LT; /输入&GT;
     

    I've been working on a feature similar to Facebook's chat heads - a feature which needs to be active for our users even if they leave our app to go check email, etc. It needs to be able to create AlertDialogs via javascript - from a place that isn't impacted when the user switches Activities inside our app. It's OK for the feature to close/hide when the user leaves the app to do something else - however it must still be present when they come back.

    Other than using a Service I haven't been able to think of another way to let users interact with us while they're off doing other things. When I use a service to create the webview via WindowManager the webview's JsDialogHelper detects that the context is not an activity and prevents it from showing AlertDialogs.

    Any ideas would be most helpful around making this work. We need to allow AlertDialogs to pop up and be interactive for this feature to be helpful. We need the webview to stick around between activity transitions.

    Is it possible to extend JsDialogHelper so I can get it to work with my code? Is there a better way to have a facebook chat heads like feature using webviews in an android app that I haven't thought of yet? I'm open to complete rewrites if it gets the user experience I'm looking for.

    解决方案

    You can display the Dialog from the service, by setting the window type as TYPE_SYSTEM_ALERT. Remember to communicate back the action taken by the user using the JsResult instance passed to 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();
            }
        }
    

    Add the required permissions to the manifest file:

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

    Alternate solution: If the web application can be built specific to android webview, then you can create Javascript interfaces which will allow Javascript code to directly invoke the Android code which in turn displays the dialog for you. This avoids the JsDialogHelper route.

    1. Define a javascript interface:

    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. Bind the interface between Javascript and Android code, using addJavascriptInterface.

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

    1. Use the interface in Javascript code.

    <input type="button" value="Display dialog" onClick="displayDialog()" />
        <script>
            function displayDialog() {
                //alert("Javascript Dialog");
                Android.showDialog();
            }
        </script>
    </input>
    

    这篇关于显示一个AlertDialog从web视图的活动之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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