AndroidTV 网络机顶盒 Wi-Fi设置

          Wi-Fi的操作接口封装类网上很多,系统源码也可以找出来,这里的主题不在这里,主要是wifi的实际操作应用!可以参考

http://smallwoniu.blog.51cto.com/3911954/1334951这位博主的。

          先上2张效果图:(整体还不完善,从项目中裁剪下来的!)

AndroidTV 网络机顶盒 Wi-Fi设置_第1张图片

AndroidTV 网络机顶盒 Wi-Fi设置_第2张图片

 

 

//1、设置的Activity
package com.setting.wifi;
import java.util.List;
import com.setting.wifi.R;
import android.app.Activity;
/**
 * WiFi 网络设置的Activity
 * @author iSnowFlake 
 *
 */
public class MainWiFiAcitivity extends Activity implements OnClickListener,OnItemClickListener {
 
 private Button wifiResultTips;
 private ImageButton refreshBtn;
 private int netId=-1;
 private TextView wifiInfo;
 private WifiAdminUtils mWifiAdmin;
 private ListView mAllAppsGridView;
 private List<ScanResult> listResult;
 private Dialog dialog ;
 private Switch wifiSwitch; 
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();       
     UiMangerHandler.sendEmptyMessageDelayed(WIFI_STATE_INIT, 1000);
     UiMangerHandler.sendEmptyMessageDelayed(WIFI_INFO, 3000);
        Toast.makeText(MainWiFiAcitivity.this, "沟通交流请联系:QQ:480474041,谢谢!", Toast.LENGTH_SHORT).show(); 
 }
 /**
  * 按钮等控件的初始化
  * 
  */
 public void init() {
  
  mWifiAdmin = new WifiAdminUtils(MainWiFiAcitivity.this);
  dialog = new AlertDialog.Builder(MainWiFiAcitivity.this).create();
  
  wifiResultTips=(Button) findViewById(R.id.wifi_result_tips);
  wifiInfo=(TextView) findViewById(R.id.wifi_info);
  refreshBtn=(ImageButton)findViewById(R.id.refresh_btn);
  refreshBtn.setOnClickListener(MainWiFiAcitivity.this);
     mAllAppsGridView = (ListView)findViewById(R.id.allapps);
     mAllAppsGridView.setOnItemClickListener(this); 
      
     wifiSwitch = (Switch) findViewById(R.id.toggleButton);  
     wifiSwitch.setOnClickListener(new OnClickListener() {    
            public void onClick(View v) {          
                if (wifiSwitch.isChecked()) { // 当按钮第一次被点击时候响应的事件     
              openNetCard();
              mWifiAdmin.startScanWifi();
              listResult =mWifiAdmin.getWifiList();
                   UiMangerHandler.sendEmptyMessageDelayed(SCAN_WIFI, 1000);
                Toast.makeText(MainWiFiAcitivity.this, "手动打开 Wi-Fi", Toast.LENGTH_SHORT).show();  
                 }else {    // 当按钮再次被点击时候响应的事件
                   closeNetCard();
                   UiMangerHandler.sendEmptyMessage(CLOSE_WIFI);
                   Toast.makeText(MainWiFiAcitivity.this, "手动关闭 Wi-Fi", Toast.LENGTH_SHORT).show();  
                 }      
             }  
        }); 
 }
 public void openNetCard() {
  mWifiAdmin.openWifi();
 }
 public void closeNetCard() {
  mWifiAdmin.closeWifi();
 }
 public void scanWifi() {
  mWifiAdmin.startScanWifi();
 }
 /**
  * 
  * 
  */
 public void getScanResult() { 
  
//        Toast.makeText(MainWiFiAcitivity.this, "wifi网卡状态:"+mWifiAdmin.checkWifiState(), Toast.LENGTH_LONG).show();  
  mWifiAdmin.startScanWifi();
  listResult =mWifiAdmin.getWifiList();
//  if(listResult.size()!=0){
      mAllAppsGridView.setAdapter(new WifiMessItemAdapter(MainWiFiAcitivity.this, listResult));
//  }
 }

 public void disconnect() {
  
  mWifiAdmin.disconnectWifi(mWifiAdmin.getNetworkId());
 }
 public void checkNetWorkState() {
  mWifiAdmin.checkWifiState();
 }     
 
 final static int WIFI_STATE_INIT=0x0000;
 final static int CLOSE_WIFI=0x0001;
 final static int SCAN_WIFI =0x0002;
 final static int WIFI_INFO =0x0003;
 /**
     * message dispose
     *  
     */
    final Handler UiMangerHandler = new Handler(){   
  @Override   
  public void handleMessage(Message msg) {  
  // TODO Auto-generated method stub
  switch(msg.what){ 
      case WIFI_STATE_INIT:  
       int wifiState=mWifiAdmin.checkWifiState();
                Toast.makeText(MainWiFiAcitivity.this, "Wi-Fi状态:"+wifiState, Toast.LENGTH_SHORT).show();  
       if(wifiState==mWifiAdmin.getWifiManager().WIFI_STATE_DISABLED){  //wifi不可用啊
        wifiResultTips.setText("WiFi 网卡关闭了!");
       }else if(wifiState==mWifiAdmin.getWifiManager().WIFI_STATE_UNKNOWN){//wifi 状态未知
        wifiResultTips.setText("WiFi 网卡状态未知!");
       }else if(wifiState==mWifiAdmin.getWifiManager().WIFI_STATE_ENABLED){//OK 可用
           wifiSwitch.setChecked(true);
        mWifiAdmin.startScanWifi();
        listResult =mWifiAdmin.getWifiList();
           UiMangerHandler.sendEmptyMessageDelayed(SCAN_WIFI, 1000);
        if(mWifiAdmin.isWifiEnable()){
                 Toast.makeText(MainWiFiAcitivity.this, "Wi-Fi 已经打开", Toast.LENGTH_SHORT).show(); 
        }else { 
                Toast.makeText(MainWiFiAcitivity.this, "请 开启 Wi-Fi", Toast.LENGTH_SHORT).show();  
        }           
       }
       
      break;
      case CLOSE_WIFI:
       getScanResult();
       wifiResultTips.setText("WiFi 网卡已经关闭 !");
       break;
      case SCAN_WIFI:
       getScanResult();
       wifiResultTips.setText("正在扫描可用的WiFi ...");
       if(listResult==null){
        UiMangerHandler.sendEmptyMessageDelayed(SCAN_WIFI, 1000);
       }else if(listResult.size()==0){
        wifiResultTips.setText("正在扫描可用的WiFi ...");
        getScanResult();
        UiMangerHandler.sendEmptyMessageDelayed(SCAN_WIFI, 1000);
       }else{
        wifiResultTips.setText("可用 WiFi 扫描结果列表 ");
        mAllAppsGridView.setAdapter(new WifiMessItemAdapter(MainWiFiAcitivity.this, listResult));
       }     
       break;   
      case WIFI_INFO:
//       Log.e("wifi","ssid=="+mWifiAdmin.getSSID()+"==");
       if(mWifiAdmin.getSSID().equals("<unknown ssid>")||mWifiAdmin.getSSID().equals("")){
        mWifiAdmin.getWifiConnectInfo();
        wifiInfo.setText("没有Wi-Fi 连接 !");
        UiMangerHandler.sendEmptyMessageDelayed(WIFI_INFO, 2500);
       }else if(mWifiAdmin.getSSID().equals("0x")){
        mWifiAdmin.getWifiConnectInfo();
        wifiInfo.setText("无连接,请选择合适的WiFi连接");
        UiMangerHandler.sendEmptyMessageDelayed(WIFI_INFO, 2500);
       }else{
        mWifiAdmin.getWifiConnectInfo();
        if(mWifiAdmin.getlocalIp().equals("null")){
         UiMangerHandler.sendEmptyMessageDelayed(WIFI_INFO, 2500);
        }
        wifiInfo.setText(mWifiAdmin.getSSID()+"  #  ip:"+mWifiAdmin.getlocalIp());
       }
       break;
   } 
      super.handleMessage(msg);
  } 
    };  
 
    /**
     * 
     * 
     */
 @Override
 public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
     // TODO Auto-generated method stub
  LayoutInflater inflaterDl = LayoutInflater.from(MainWiFiAcitivity.this);
        RelativeLayout layout = (RelativeLayout)inflaterDl.inflate(R.layout.wifi_connect_dialog, null );
        TextView wifiName=(TextView)layout.findViewById(R.id.wifi_name);
        final EditText wifiCode=(EditText)layout.findViewById(R.id.wifi_code);
        wifiName.setText(listResult.get(position).SSID);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        lp.x = 0;
        lp.y = 0; 
        lp.width = 750; 
        lp.height = 400; 
        lp.alpha = 1.0f; 
        lp.softInputMode=0;
        dialogWindow.setAttributes(lp);    
        dialog.show();
        Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);
        btnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                     dialog.dismiss();                                
            }
        });  
        Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
        btnOK.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) { 
                   String wifiCodeStr=wifiCode.getText().toString();
                   Log.e("wifi",":"+"密码输入后的返回值:"+netId);
                   netId=mWifiAdmin.addNetwork(mWifiAdmin.CreateWifiInfo
                              (listResult.get(position).SSID, wifiCodeStr, 3)); 
                   if(netId==0){
                       Toast.makeText(MainWiFiAcitivity.this, "WIFI_STATE_DISABLING !", Toast.LENGTH_LONG).show();  
                   }else if(netId==1){
                       Toast.makeText(MainWiFiAcitivity.this, "密码错误,请重试 !", Toast.LENGTH_LONG).show();  
                    }else if(netId==2){
                      Toast.makeText(MainWiFiAcitivity.this, "正在连接 !", Toast.LENGTH_LONG).show();  
                       dialog.dismiss();                                
                    }else if(netId==-1){
                       Toast.makeText(MainWiFiAcitivity.this, "连接失败 !", Toast.LENGTH_LONG).show();  
                    }else{
                       Toast.makeText(MainWiFiAcitivity.this, "正在连接 !", Toast.LENGTH_LONG).show();  
                       dialog.dismiss();   
                    }
                 UiMangerHandler.sendEmptyMessageDelayed(WIFI_INFO, 2000);
                   
              }
        });  
        
        wifiCode.setOnFocusChangeListener(new OnFocusChangeListener() {
   
   public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    if(hasFocus){
           dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }else{
     
    } 
   }
  });
         
 }
 
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.refresh_btn:
   if(mWifiAdmin.checkWifiState()==mWifiAdmin.getWifiManager().WIFI_STATE_ENABLED){
    listResult.clear();
    openNetCard();
//    if(listResult.size()!=0){
        mAllAppsGridView.setAdapter(new WifiMessItemAdapter(MainWiFiAcitivity.this, listResult));
//    }
    wifiSwitch.setChecked(true);
    UiMangerHandler.sendEmptyMessage(SCAN_WIFI);
   }else{
       wifiResultTips.setText("请开启WiFi网卡 !");
             Toast.makeText(MainWiFiAcitivity.this, "请开启WiFi网卡 !", Toast.LENGTH_LONG).show();  
   }
   break;
  default:
   
   break;
  }
 }
}

2、对用ListView 的item 适配器

package com.setting.wifi;
import java.util.List;
import com.setting.wifi.R;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class WifiMessItemAdapter extends BaseAdapter{
 private Context context;
 private List<ScanResult> wifiListResult;
 private ScanResult mScanResult;
 
 //构造方法
 public WifiMessItemAdapter(Context c,List<ScanResult> wifiListResult){
  this.context = c;
  this.wifiListResult = wifiListResult;
 }
 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  if(wifiListResult==null) return 0;
  return wifiListResult.size();
 }
 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return null;
 }
 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return 0;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
     
  convertView = LayoutInflater.from(context).inflate(R.layout.wifi_set_grid_item, null);
  TextView wifiIndex = (TextView)convertView.findViewById(R.id.wifi_index);
  TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title);       //wifi ssid
  TextView app_icon  =  (TextView)convertView.findViewById(R.id.wifi_code_type); //加密方式
  mScanResult=wifiListResult.get(position);
  
  wifiIndex.setText((position+1)+". ");
  app_tilte.setText(mScanResult.SSID);
  app_icon.setText(mScanResult.capabilities);
 
  return convertView;
 }
 
}

  布局文件不上传了, 还是上传工程吧,啥也不说了!

 下载地址:http://download.csdn.net/detail/isnowflake/7805893

你可能感兴趣的:(AndroidTV 网络机顶盒 Wi-Fi设置)