Android 中的无限进度对话框导致应用程序崩溃

发布于 2024-11-06 22:56:11 字数 3480 浏览 4 评论 0原文

我希望在我的代码启用 WiFi 并连接到特定 SSID 时显示 ProgressDialog。我确实在启动另一个线程时看到了其他问题。我是否需要启动一个单独的线程才能在执行 show() 和 hide() 之间的代码时看到 ProgressDialog?我连接的 Wifi 已知存在并且没有安全密钥。

package com.connectionmanager.app;

import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FoundSavedUser extends Activity implements OnClickListener{

    private WifiManager WifiInfo;
    private ProgressDialog dialog;
    private List<ScanResult> results;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect_to_gogo);

        //logView is temporary for displaying messages regarding connectivity.
        TextView logView = (TextView) findViewById(R.id.logTextView);

        WifiInfo = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        //Is WiFi on?
        if(!WifiInfo.isWifiEnabled())
        {
            //Show the acknowledgment button that will allow user to turn on WiFi.
            Button wifiButton = (Button) findViewById(R.id.wifiButton);
            wifiButton.setVisibility(View.VISIBLE);
            wifiButton.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub

        if(v.getId() == (R.id.wifiButton))
        {
            //Setup a ProgressDialog until WiFi is switched on.
            dialog = ProgressDialog.show(FoundSavedUser.this, "", "Loading. Please wait...", true);
            dialog.show();

            //Switch on WiFi
            WifiInfo.setWifiEnabled(true);

            //Get list of the results in object format ( like an array )
            results = WifiInfo.getScanResults();
            int i = 0;
            String[] ssid = new String[results.size()];
            for (ScanResult result : results) 
            {
                ssid[i] = result.SSID;
                i++;
                Log.v("Hello","Printing result.SSID: " + result.SSID);
            }
            Log.v("Hello","Connecting to: myWiFi");

            WifiConfiguration wifiConfig = new WifiConfiguration();
            wifiConfig.SSID = "\"myWiFi\"";
            wifiConfig.priority = 1;
            wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfig.status=WifiConfiguration.Status.ENABLED;
            int netId = WifiInfo.addNetwork(wifiConfig);
            if (WifiInfo.enableNetwork(netId, true))
            {
             Log.v("Hello","Connection enabled");
            }
            else
            {
             Log.v("Hello","Connection not enabled");
            }
            Log.v("Hello","Print netId: " + netId);
            Log.v("Hello","Connection success!");

            //Hide the ProgressDialog
            dialog.hide();

        }
    }
}

I want ProgressDialog to show while my code enables WiFi and connects to a particular SSID. I did see other questions with starting another thread. Is it necessary for me to start a separate thread to be able to see the ProgressDialog while the code between show() and hide() is executed? The Wifi that i connect to is known to exist and has no security key.

package com.connectionmanager.app;

import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FoundSavedUser extends Activity implements OnClickListener{

    private WifiManager WifiInfo;
    private ProgressDialog dialog;
    private List<ScanResult> results;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connect_to_gogo);

        //logView is temporary for displaying messages regarding connectivity.
        TextView logView = (TextView) findViewById(R.id.logTextView);

        WifiInfo = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

        //Is WiFi on?
        if(!WifiInfo.isWifiEnabled())
        {
            //Show the acknowledgment button that will allow user to turn on WiFi.
            Button wifiButton = (Button) findViewById(R.id.wifiButton);
            wifiButton.setVisibility(View.VISIBLE);
            wifiButton.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v) 
    {
        // TODO Auto-generated method stub

        if(v.getId() == (R.id.wifiButton))
        {
            //Setup a ProgressDialog until WiFi is switched on.
            dialog = ProgressDialog.show(FoundSavedUser.this, "", "Loading. Please wait...", true);
            dialog.show();

            //Switch on WiFi
            WifiInfo.setWifiEnabled(true);

            //Get list of the results in object format ( like an array )
            results = WifiInfo.getScanResults();
            int i = 0;
            String[] ssid = new String[results.size()];
            for (ScanResult result : results) 
            {
                ssid[i] = result.SSID;
                i++;
                Log.v("Hello","Printing result.SSID: " + result.SSID);
            }
            Log.v("Hello","Connecting to: myWiFi");

            WifiConfiguration wifiConfig = new WifiConfiguration();
            wifiConfig.SSID = "\"myWiFi\"";
            wifiConfig.priority = 1;
            wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
            wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfig.status=WifiConfiguration.Status.ENABLED;
            int netId = WifiInfo.addNetwork(wifiConfig);
            if (WifiInfo.enableNetwork(netId, true))
            {
             Log.v("Hello","Connection enabled");
            }
            else
            {
             Log.v("Hello","Connection not enabled");
            }
            Log.v("Hello","Print netId: " + netId);
            Log.v("Hello","Connection success!");

            //Hide the ProgressDialog
            dialog.hide();

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-11-13 22:56:11

正确,它必须在单独的线程中运行。

看看这个解决方案,Android: ProgressDialog does not show

Correct, it has to run in a separate thread.

Take a look at this solution, Android: ProgressDialog doesn't show

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文