gpt4 book ai didi

android - Android中的Indefinite ProgressDialog崩溃应用程序

转载 作者:行者123 更新时间:2023-12-03 17:42:48 24 4
gpt4 key购买 nike

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

}
}
}

最佳答案

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

看看这个解决方案,Android: ProgressDialog doesn't show

关于android - Android中的Indefinite ProgressDialog崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5969509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com