从非 UI 线程弹出对话框

2023-12-11

我正在开发一个面向团体的网络应用程序。问题是,当我要加入一个组时,它首先检查该组是否安全,如果是,它会要求输入用户名和密码。获得组安全性可能需要几秒钟,因此我为整个过程生成一个新线程。我想弹出一个对话框,以防该组需要安全性。我认为这可能与后台线程有关,它们可能无法弹出对话框...但问题是我需要检查后台线程中的组安全性,因为这需要一些时间。

希望任何人都可以在群组安全的情况下提出解决方案或任何询问用户/通行证的方法。这是后台线程:

public void run() {

 secInf = mGroupId.getSecurityInformation();
 if (secInf.getAdmissionLevel() == CreateGroupDialog.PRIVATE_KEY_ACCESS) {
  showUserPasswordDialog();
 } else {
  mService.joinGroup(mGroupId);
  // Notifies handler to dismiss ProgresDialog and start activity
  mHandler.sendMessage(Message.obtain(mHandler,
      GroupsActivity.JOIN_SUCCESSFUL));
 }

showUserPasswordDialog 的作用(mActivity 是生成此线程的活动):

 private void showUserPasswordDialog() {
  AlertDialog dialog;
  // add this to your code
  // This example shows how to add a custom layout to an AlertDialog
  LayoutInflater factory = LayoutInflater.from(mActivity);
  final View textEntryView = factory.inflate(
    R.layout.alert_dialog_text_entry, null);

  AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
  builder.setIcon(R.drawable.alert_dialog_icon);
  builder.setTitle(R.string.ask_user_password);
  builder.setView(textEntryView);
  builder.setPositiveButton(R.string.ok_text,
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      String userName = ((EditText) mActivity
        .findViewById(R.id.username_edit_alert_dialog))
        .getText().toString();
      String password = ((EditText) mActivity
        .findViewById(R.id.password_edit_alert_dialog))
        .getText().toString();
      Credentials cred = new CredentialsL1(userName, password);

      mSmeppService.joinGroup(mGroupId, cred);

      mHandler.sendMessage(Message.obtain(mHandler,
        GroupsActivity.JOIN_SUCCESSFUL));
    });
  builder.setNegativeButton(R.string.cancel_text,
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int whichButton) {
      dialog.cancel();
      mHandler.sendMessage(Message.obtain(mHandler,
        GroupsActivity.DISMISS_PROGRESS_DIALOG));
     }
    });

  dialog = builder.create();

  /* I found this somewhere, but didn't work either */
  // Window window = dialog.getWindow();
  // WindowManager.LayoutParams lp = window.getAttributes();
  // lp.token = textEntryView.getWindowToken();
  // lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
  // window.setAttributes(lp);
  // window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

  dialog.show();

 }

我收到这个异常:

12-13 19:18:31.823: ERROR/AndroidRuntime(1702): FATAL EXCEPTION: Thread-38
12-13 19:18:31.823: ERROR/AndroidRuntime(1702): android.view.InflateException: Binary XML file line #17: Error inflating class android.widget.EditText
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at org.pfc.threads.GroupJoinerThread.showUserPasswordDialog(GroupJoinerThread.java:76)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at org.pfc.threads.GroupJoinerThread.run(GroupJoinerThread.java:52)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702): Caused by: java.lang.reflect.InvocationTargetException
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.widget.EditText.<init>(EditText.java:51)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at java.lang.reflect.Constructor.constructNative(Native Method)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     ... 8 more
12-13 19:18:31.823: ERROR/AndroidRuntime(1702): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.os.Handler.<init>(Handler.java:121)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.text.method.MetaKeyKeyListener$MetaKeyDropbackHandler.<init>(MetaKeyKeyListener.java:605)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.text.method.MetaKeyKeyListener.<init>(MetaKeyKeyListener.java:96)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.text.method.BaseKeyListener.<init>(BaseKeyListener.java:25)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.text.method.TextKeyListener.<init>(TextKeyListener.java:66)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.text.method.TextKeyListener.getInstance(TextKeyListener.java:83)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.widget.TextView.<init>(TextView.java:806)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     at android.widget.EditText.<init>(EditText.java:55)
12-13 19:18:31.823: ERROR/AndroidRuntime(1702):     ... 12 more

XML布局文件是:

   <EditText
       android:id="@+id/username_edit_alert_dialog"
       android:enabled="false"
       android:layout_height="wrap_content"
       android:layout_width="fill_parent"
       android:layout_marginLeft="20dip"
       android:layout_marginRight="20dip"
       android:scrollHorizontally="true"
       android:autoText="false"
       android:capitalize="none"
       android:gravity="fill_horizontal"
       android:textAppearance="?android:attr/textAppearanceMedium" />
  <!-- Password -->
   <TextView
       android:id="@+id/password_view"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_marginLeft="20dip"
       android:layout_marginRight="20dip"
       android:text="@string/password_view_text"
       android:gravity="left"
       android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
       android:id="@+id/password_edit_alert_dialog"
       android:enabled="false"
       android:layout_height="wrap_content"
       android:layout_width="fill_parent"
       android:layout_marginLeft="20dip"
       android:layout_marginRight="20dip"
       android:scrollHorizontally="true"
       android:autoText="false"
       android:capitalize="none"
       android:gravity="fill_horizontal"
       android:password="true"
       android:textAppearance="?android:attr/textAppearanceMedium" />

您无法从后台线程执行 UI 操作,这是正确的。这样做的方法是使用Handler您已经实现了弹出对话框。所以像这样:

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
        case GroupsActivity.DISMISS_PROGRESS_DIALOG:
            //your existing dismiss code
            break;
        case GroupsActivity.CREATE_PROGRESS_DIALOG:
            //create the dialog
            break;
        }
    }
};

那么你的运行方法将是:

public void run() {

    secInf = mGroupId.getSecurityInformation();
    if (secInf.getAdmissionLevel() == CreateGroupDialog.PRIVATE_KEY_ACCESS) {
        // do stuff
        mHandler.sendMessage(Message.obtain(mHandler,
            GroupsActivity.CREATE_PROGRESS_DIALOG));
        // do more stuff
    } else {
        mService.joinGroup(mGroupId);
        // Notifies handler to dismiss ProgresDialog and start activity
        mHandler.sendMessage(Message.obtain(mHandler,
            GroupsActivity.JOIN_SUCCESSFUL));
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从非 UI 线程弹出对话框 的相关文章

随机推荐

  • 邻接表和邻接矩阵能够在逻辑上呈现非线性数据结构

    邻接表和邻接矩阵如何能够在逻辑上呈现非线性数据结构 即使它们本身是线性的 有人请解释一下 堆栈 队列 列表 向量 数组 它们都是线性存储数据 因为项目是一个接一个地存储的 有一个概念 A 项来了 before项目 B 和 项目 C 来了af
  • 第 5 次迭代后阶乘循环结果不正确

    我目前正在学习微积分预科课程 并认为我会编写一个快速程序 给出阶乘 10 的结果 在测试它时 我注意到在第 5 次迭代后我得到了不正确的结果 然而 前 4 次迭代是正确的 public class Factorial public stat
  • 使用 C# 中的正则表达式返回包含匹配项的整行

    假设我有以下字符串 string input Hello world n Hello foobar world n Hello foo world n 我有一个正则表达式模式 由我正在编写的工具的用户指定 foobar 我想返回每行的整行i
  • 获取嵌套字典中所有键的列表

    我想获取包含列表和字典的嵌套字典中所有键的列表 我目前有这段代码 但似乎缺少向列表添加一些键 并且还重复添加了一些键 keys list def get keys d or l keys list if isinstance d or l
  • NSDate initWithString

    将 Xcode 更新到版本 4 2 后 我在当前项目中收到以下警告 警告 NSDate 可能不会响应 initWithString 我必须做什么 此方法仅在 Mac OSX 页面的文档中注明 在 iOS 中未注明 我不清楚为什么苹果有不同的
  • 如何将索引处的行插入排序的ag-grid

    我有一个启用排序的网格设置 每行都有一个重复按钮 复制行时 我想在复制的行下方插入新行 这适用于默认排序 但如果您对列进行排序 例如状态 它会将该行随机插入到网格中 从而很难找到 我注意到网格在保存过程中的某个时候会进行排序 但在它得到分配
  • android httpclient 和 utf-8

    我正在尝试连接到一个网络服务 我的查询中保存了一些数据 不好的是 这些数据包含utf 8字符 这会出现问题 如果我只是使用普通字符串调用 HttpGet 则会出现 非法字符 异常 所以我用谷歌搜索并尝试了一些 utf 8 魔法 HttpCl
  • jQuery 按类对列表项进行分组

    我有一个具有相同类列表项的无序动态列表 我想将相同的类列表项分组到主 ul 中的一个 ul 中 如何对相同类别的列表项进行分组 我想转换下面的动态列表 ul li class a1 Some Content li li class a1 S
  • 使用c#搜索文本文件并显示行号和包含搜索关键字的完整行

    我需要帮助使用 c 搜索文本文件 日志文件 并显示行号和包含搜索关键字的完整行 这是对以下内容的轻微修改 http msdn microsoft com en us library aa287535 28VS 71 29 aspx int
  • 如何为多个进程缓存 eToken PIN

    我有一个 NET c 应用程序 它使用 我的 证书存储中的 x509Certificate2 最初来自 eToken 设备 当我使用证书 解密数据或将其用作 Web 请求的客户端证书 时 它会询问设备 PIN 一次 之后 它会被缓存 用户不
  • Glassfish V3.x 和远程独立客户端

    连接到a绝对没问题ActiveMQ作为独立客户端 您唯一需要做的就是添加activemq all 5 4 1 jar就这样 prop put Context SECURITY AUTHENTICATION system prop put C
  • 仅接受使用 scanf 输入的数值

    如何确保用户仅输入数字值而不是字母数字或任何其他字符 另外 要寻找什么来插入不正确输入的错误消息 include
  • Laravel 5.5:会话不起作用

    我在用着Session put client id 设置一个会话值 该值保留在控制器内和应用程序内的其他位置 但我通过 Vue 的 API 路由调用的控制器除外 我已经进行了编辑 driver gt env SESSION DRIVER d
  • 如何使用 C 以编程方式在 Windows 7 上设置 IP 地址

    我正在开发一个需要能够设置 IP 地址的应用程序 使用命令提示符 Netsh接口IP设置地址 它可以工作 但是 c 中的等效项是什么 Thanks 我认为您需要 AddIPAddress API 参考添加IP地址函数MSDN 文档中有一个很
  • 将word文档插入另一个word文档而不改变格式VBA

    首先 我通过用户窗体上的按钮将 Word 文档 doc1 复制到新的 Word 文档 并采用其格式 其次 我在这个word文档 填充有doc1 的末尾插入一个新的word文档doc2 doc1和doc2有文本和表格以及各种颜色 每次我按下另
  • JSP/HTML 页面到 PDF 的转换 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 如何将 JSP HTML 文件转换为 PDF 我想将网页的特定部分转换为 PDF 文件 是否可以 是的 好好看看展位阿帕奇FOP and iText
  • 如何在 Excel 中查找拼写错误的文本之间的匹配项?

    我有两列数据 其中有一百个名字 我需要找到匹配项 问题是第二列上的名称与第一列上的名称不完全相同 很难用一百个名字来匹配他们 excel中是否有任何公式至少可以给出数据的公差 例如 Setyadi 与 Setiadi 或 Tak Jelan
  • 时间戳/日期作为 cassandra 列族/hector 的关键

    我必须创建并查询一个复合键为 timestamp long 的列族 还 查询时我想触发时间戳范围查询 例如 xxx 和 yyy 之间的时间戳 这可能吗 目前我正在做一些非常有趣的事情 我知道这是不正确的 我为给定范围创建带有时间戳字符串的键
  • Codeigniter simple_query 与查询生成器(插入、更新和删除)

    根据文档 simple query不会返回任何数据库结果集 也不会设置查询计时器 或编译绑定数据 或存储查询以进行调试 正如在我的 CodeIgniter 中一样 我使用 CI 提供的查询生成器来生成查询 那么 这些用于插入 更新和删除的查
  • 从非 UI 线程弹出对话框

    我正在开发一个面向团体的网络应用程序 问题是 当我要加入一个组时 它首先检查该组是否安全 如果是 它会要求输入用户名和密码 获得组安全性可能需要几秒钟 因此我为整个过程生成一个新线程 我想弹出一个对话框 以防该组需要安全性 我认为这可能与后