如何在uiThread中使用Alert Dialogue?

时间:2016-03-24 05:54:12

标签: android multithreading

以下是 ConnectActivity 的代码截图,它扩展了尝试创建AlertDialogue的AppCompatActivity

new AsyncTask < Void, Void, Boolean > () {
  @Override
  protected Boolean doInBackground(Void...params) {
   try {
    Runnable r = new Runnable() {
     public void run() {

      while (true) {
       Socket socket = null;
       try {
        socket = new Socket("192.168.0.32", 11311);
        Log.i("CON", "Connected!");
        socket.close();
       } catch (Exception e) {
        Log.i("CON", "Disconnected!");

        runOnUiThread(new Runnable() {
         public void run() {
          AlertDialog alertDialog;
          alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
          alertDialog.setTitle("Network error");
          alertDialog.setMessage("Check network connection and try again.");
          alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
            finish();
           }
          });
          alertDialog.show();
         }
        });

        break;

       }
       try {
        wait(5000);
       } catch (Exception e) {

       }

      }

     }
    };
    Thread t = new Thread(r);
    t.start();
    return true;
   } catch (Exception e) {

    return false;
   }
  }

AlertDialog.Builder(getApplicationContext()).create();行会出错:

java.lang.IllegalStateException:您需要对此活动使用Theme.AppCompat主题(或后代)。

所以我尝试用AlertDialog.Builder(this).create();替换此行,但this这里代表Runnable而不是上下文。我该如何更正此代码?

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/PrimaryColor</item>
        <item name="colorPrimaryDark">@color/PrimaryDarkColor</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">#ffffff</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
</resources>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="edu.academy.cs573.netg">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        tools:replace="android:icon"
        android:icon="@mipmap/ic_launch"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication"
        android:theme="@style/SplashTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>



        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme"/>
        <activity android:name=".AboutActivity"
            android:theme="@style/AppTheme"/>
        <activity android:name=".LicenseInfoActivity"
            android:theme="@style/AppTheme"/>

        <activity
            android:name=".ConnectActivity"
            android:label="@string/app_name"
            android:launchMode="standard"
            android:theme="@style/AppTheme"/>

        <service android:name=".NodeMainExecutorService" >
            <intent-filter>
                <action android:name=".NodeMainExecutorService" />
            </intent-filter>
        </service>



    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

像这样传递活动上下文

alertDialog = new AlertDialog.Builder(YourActivityName.this).create();

如果您想传递应用程序上下文,请为您的应用程序使用app comapact主题。这是在styles.xml文件中明确定义的

但您需要在onProgressUpdate()方法中显示对话框。你不需要运行ui线程。当异步任务已经有一个方法在ui上显示一些输出时。

答案 1 :(得分:0)

在调用AlertDialog之前调用create AsyncTask是件好事。 您可以在拨打电话之前检查网络连接。

alertDialog = new AlertDialog.Builder(YourAcitvity.this, android.R.style.Theme_Material_Light_Dialog_Alert).create();

此外,您可以提供主题提醒对话框(android.R.style.Theme_Material_Light_Dialog_Alert).

答案 2 :(得分:0)

尝试在应用标记

中添加此内容
android:theme="@style/Theme.AppCompat.Light"

在你的清单中