我一直在研究类似于 Facebook 聊天头的功能 - 即使用户离开我们的应用程序去查看电子邮件等,该功能也需要为我们的用户激活。它需要能够通过 javascript 创建 AlertDialogs - 当用户在我们的应用程序中切换 Activity 时,不会受到影响的地方。当用户离开应用程序去做其他事情时关闭/隐藏该功能是可以的 - 但是当他们回来时它必须仍然存在。
除了使用服务之外,我一直想不出其他方法让用户在他们不在做其他事情时与我们互动。当我使用服务通过 WindowManager 创建 webview 时,webview 的 JsDialogHelper 检测到上下文不是 Activity 并阻止它显示 AlertDialogs。
任何想法对完成这项工作都是最有帮助的。我们需要允许 AlertDialogs 弹出并进行交互,以便此功能有用。我们需要 webview 在 Activity 转换之间停留。
是否可以扩展 JsDialogHelper 以便我可以让它与我的代码一起工作?有没有更好的方法来让 facebook 聊天头像功能一样在我还没有想到的 android 应用程序中使用 webviews?如果它获得我正在寻找的用户体验,我愿意完成重写。
最佳答案
您可以通过将窗口类型设置为 TYPE_SYSTEM_ALERT
来显示服务中的 Dialog
。请记住使用传递给 onJsAlert
的 JsResult
实例反馈用户采取的操作。
// 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();
}
}
将所需的权限添加到 list 文件:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
替代解决方案:
如果可以构建特定于 android webview 的 web 应用程序,那么您可以创建 Javascript 接口(interface),这将允许 Javascript 代码直接调用 Android 代码,Android 代码反过来为您显示对话框。这避免了 JsDialogHelper
路由。
- 定义一个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(); } }
- 绑定(bind) Javascript 和 Android 代码之间的接口(interface),使用 addJavascriptInterface .
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
- 在 Javascript 代码中使用接口(interface)。
<input type="button" value="Display dialog" onClick="displayDialog()" /> <script> function displayDialog() { //alert("Javascript Dialog"); Android.showDialog(); } </script> </input>
关于android - 在 Activity 之外的 Webview 中显示 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26044179/