博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AndroidTV 网络机顶盒 Wi-Fi设置
阅读量:6714 次
发布时间:2019-06-25

本文共 11322 字,大约阅读时间需要 37 分钟。

hot3.png

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

这位博主的。

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

110356_q0wp_166646.png

110357_3L8f_166646.png

 

 

//1、设置的Activitypackage 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
 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("
")||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
 wifiListResult; private ScanResult mScanResult;  //构造方法 public WifiMessItemAdapter(Context c,List
 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; } }

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

 下载地址:

转载于:https://my.oschina.net/zengliubao/blog/305832

你可能感兴趣的文章
【眼力测试】看图识字
查看>>
设计模式小结
查看>>
快播关闭服务器,你怎么看?
查看>>
免费好用的阿里云云盾证书服务(https证书)申请步骤
查看>>
2017杭州云栖大会100位大咖视频+讲义全分享
查看>>
【云栖大会】持续拥抱开源阿里云计算能力三大突破
查看>>
在linux下制作静态库和动态链接库的方法
查看>>
ZeroMQ试用笔记之REQ & ROUTER
查看>>
PowerDesigner列名、注释内容互换
查看>>
[译] 利用 Immutability(不可变性)编写更为简洁高效的代码
查看>>
云终端推动证券网系统升级
查看>>
Alibaba Cloud Network Attached Storage Now Available
查看>>
Schema命名空间示例
查看>>
ASP.NET Web API路由系统:路由系统的几个核心类型
查看>>
XSKY加盟“未来就绪企业云联盟”
查看>>
CloudCC:如何将CRM变成企业发展战略
查看>>
某研究院dell存储 raid5 XFS磁盘阵列数据丢失恢复案例
查看>>
Java 访问 C++ 方法 JavaCPP
查看>>
关注数据中心“减负” WD助力绿色存储
查看>>
中国人工智能学会通讯——AI如何深度应用于消费金融的风控决策 1.2 在人工智能的业务实践中,长尾数据和特征能很大提升信用评分的准确性。...
查看>>