-2

我正在使用下面的代码连接到 wifi。当应用程序加载时,它只显示黑屏,然后按钮将显示与 wifi 断开连接。有没有办法让按钮立即可见?还有一种方法可以显示应用程序加载的进度,这样用户就不会认为它挂了?

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {
/** Called when the activity is first created. 
 * @return */



@Override
public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Button OffWifi = (Button)findViewById(R.id.offwifi);
    OffWifi.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) {                 
          WifiManager wifiManager = (WifiManager)getBaseContext().getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(false);          
          }        
      });



    TextView tv = new TextView(this);       
    TextView status = new TextView(this);                  


    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    WifiConfiguration wc = new WifiConfiguration();  
    wc.SSID = "\"Test\""; //IMP! This should be in Quotes!! 

    wc.hiddenSSID = true; 
    wc.status = WifiConfiguration.Status.ENABLED;      
    wc.priority = 10; 
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);  
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    wc.preSharedKey = "\"Password\""; 
    WifiManager  wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE); 
    boolean res1 = wifiManag.setWifiEnabled(true); 
    int res = wifi.addNetwork(checkPreviousConfiguration(wc)); 
    Log.d("WifiPreference", "add Network returned " + res ); 
    boolean es = wifi.saveConfiguration(); 
    Log.d("WifiPreference", "saveConfiguration returned " + es ); 
    boolean b = wifi.enableNetwork(res, true);    
    Log.d("WifiPreference", "enableNetwork returned " + b );   

    tv.setText("You are now connected!  " +
            "Version 1.1");

    status.setText("The was an error connecting, please try again.");

       //@Override

    try {

        Thread.sleep(5000);

         ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

         if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){  
                //You are connected, do something online. 
                setContentView(tv);

            }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {              
                //Not connected.         
               setContentView(status);
            }  

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }





}

public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
 List<WifiConfiguration> configs = wifi.getConfiguredNetworks();     
    for(WifiConfiguration config : configs) {         
        if(config.SSID.equals(wc.SSID)) return config;     
        }     
    return wc; 
    } 

}
4

2 回答 2

3

我想这Thread.sleep(5000)是延迟的原因。

基本上,您是在告诉应用程序停止 5 秒钟。

于 2012-04-19T19:50:25.920 回答
1

如上所述,它是导致延迟的 Thread.sleep(5000) 。因为 OnCreate 是从 UI 线程调用的,所以整个事情都暂停了。如果您创建一个可运行对象,然后在新线程的实例中使用它,您可以在不阻塞 UI 线程的情况下随时调用 sleep()。这是许多“XXX 没有响应”错误的主要原因。或者,如果您不想进入线程,请查看 Android 提供的 ASyncTask。

编辑:

如果您只想显示一条消息,那么 Toast 是最好的选择。从另一个线程访问 UI 有点麻烦,但并非不可能。一种方法是将整个 try 块打包成一个可运行文件,如下所示:

Runnable r = new Runnable(){
    @Override
    public void run(){
            try {

                Thread.sleep(5000);

                ConnectivityManager connec =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

                if (connec != null && (connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) ||(connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)){  
                    //You are connected, do something online. 
                    //Have to edit the UI on the UI thread
                    Runnable tempRun = new Runnable(){
                        @Override
                        public void run(){
                            setContentView(tv);
                        }
                    }
                    runOnUiThread(r);
                }else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {              
                    //Not connected. 
                    //NEEDS TO BE SAME AS ABOVE.        
                    setContentView(status);
                }  

           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
     }
}

然后你可以调用Thread t = new Thread(r);and t.start();。我没有测试过,但我很确定那是准确的。setContentView 调用将更改整个布局,这可能不是您想要的,但超出了这个问题的范围。

于 2012-04-19T20:17:15.810 回答