androd 获得wifi列表
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yanlei.wifi" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 连接<a href="http://www.it165.net/news/nhlw/" target="_blank" class="keylink">互联网</a>Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WifiListActivity" > <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </RelativeLayout>
item_wifi_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_marginBottom="14dp"
android:layout_toRightOf="@+id/imageView"
android:text="TextView" /> <TextView
android:id="@+id/signal_strenth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentRight="true"
android:text="TextView" /> </RelativeLayout>
MainActivity.java
package com.example.yanlei.wifi; import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; import java.util.List; public class MainActivity extends AppCompatActivity { private WifiManager wifiManager;
List<ScanResult> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
} private void init() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
openWifi();
list = wifiManager.getScanResults();
ListView listView = (ListView) findViewById(R.id.listView);
if (list == null) {
Toast.makeText(this, "wifi未打开!", Toast.LENGTH_LONG).show();
}else {
listView.setAdapter(new MyAdapter(this,list));
} } /**
* 打开WIFI
*/
private void openWifi() {
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
} } public class MyAdapter extends BaseAdapter { LayoutInflater inflater;
List<ScanResult> list;
public MyAdapter(Context context, List<ScanResult> list) {
// TODO Auto-generated constructor stub
this.inflater = LayoutInflater.from(context);
this.list = list;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
view = inflater.inflate(R.layout.item_wifi_list, null);
ScanResult scanResult = list.get(position);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(scanResult.SSID);
TextView signalStrenth = (TextView) view.findViewById(R.id.signal_strenth);
signalStrenth.setText(String.valueOf(Math.abs(scanResult.level)));
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
//判断信号强度,显示对应的指示图标
if (Math.abs(scanResult.level) > 100) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.a0));
} else if (Math.abs(scanResult.level) > 80) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1));
} else if (Math.abs(scanResult.level) > 70) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.a0));
} else if (Math.abs(scanResult.level) > 60) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1));
} else if (Math.abs(scanResult.level) > 50) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.a0));
} else {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.a1));
}
return view;
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
来自:android wifi讲解 wifi列表显示
androd 获得wifi列表的更多相关文章
- android 获取wifi列表,如果你忽略了这个细节,可能你的软件会崩溃
一:业务描述 最近公司有一个小需求,用户点击wifi扫描按钮(注意:是用户主动点击wifi扫描按钮),app去扫描附近的wifi,显示在listView中,仅此而已,app都不用去连接某个wifi,看 ...
- android开发-获取wifi列表
近期博主在学frangment框架,因此想着想着就想通过listfragment完毕对wifi列表的获取. 好! 如今就不说废话了. 一.wifi的基础知识 在Android的官方文档中定义了例如以下 ...
- IOS零碎技术整理(3)-获取wifi列表
1. 该功能实现基于MobileApple80211框架来进行开发,而目前该框架成为了私有框架,其中的API均为私有API. 如果使用这些API可能导致应用不能上app store或者ios版本升 ...
- 开关WI-Fi显示列表
实现效果: 使用方法: Show-NetList #显示Wi-Fi列表 Show-NetList -off #关闭显示 (如图) 实现代码: function Show-NetList { P ...
- 【Win10 应用开发】扫描和连接Wi-fi网络
老周今天带大家去“扫雷”了,别当真,是扫描并连接指定无线网络,时尚一点叫Wi-fi. 所以,今天的任务要求你的设备至少有1张无线网卡,目前老周没看到过有N张无线网卡的设备.像笔记本.平板等设备都可以, ...
- 移动互联网实战--wifi定位和架构
前言: 非常幸运, 接触过一个与定位服务有些关联的项目. 虽不清楚定位服务内部的实现机制, 但对定位的几种方式也有较清晰的了解. 定位不在局限于GPS, 基站这种需要硬件支持的, 基于wifi的方式更 ...
- 获取wifi信息
最近有个项目需要获取手机附近wifi列表,查了许多资料发现,现在只能查到wifi的SSID,并且用到的是私有api,无法通过app store审核,这里记录一下,方便学习,新手勿喷,欢迎大神指教(wi ...
- 转-Android中自动连接到指定SSID的Wi-Fi
最近在做一个项目,其中涉及到一块“自动连接已存在的wifi热点”的功能,在网上查阅了大量资料,五花八门,但其中一些说的很简单,即不能实现傻瓜式的拿来就用,有些说的很详细,但其中不乏些许错误造成功能无法 ...
- ubuntu给手机建wifi
声明 笔者近期意外的发现 笔者的个人站点http://tiankonguse.com/ 的非常多文章被其他站点转载,可是转载时未声明文章来源或參考自 http://tiankonguse.com/ 站 ...
随机推荐
- 【Luogu】P4103大工程(虚树DP)
题目链接 我貌似发现这类DP就是先别管什么虚树……把树形DP搞出来套上虚树板子就好了 这个树形DP就是设sum为答案,sumd为子树内所有点的深度和(当然指的是被询问的点),maxi指子树内最深的点的 ...
- [AGC005D] ~K Perm Counting [dp]
题面 传送门 思路 首先可以明确的一点是,本题中出现不满足条件的所有的数,都是分组的 只有模$K$意义下相同的数之间才会出现不满足条件的情况,而且仅出现在相邻的情况 那么我们考虑把这个性质利用起来 我 ...
- JAVA File方法各类文件复制操作
import java.io.*; public class AllFile { public static void main(String[] args) throws Exception {// ...
- Tomcat学习笔记(七)
Tomcat载入器(一) 在了解tomcat的载入器时,首先需要了解的是java的类加载部分的内容. 在java体系中,系统分为3中类型的加载器 1.启动类加载器(Bootstrap ClassLoa ...
- 结构型设计模式之代理模式(Proxy)
结构 意图 为其他对象提供一种代理以控制对这个对象的访问. 适用性 在需要用比较通用和复杂的对象指针代替简单的指针的时候,使用P r o x y 模式.下面是一 些可以使用P r o x y 模式常见 ...
- new Date在ios下的兼容bug
今天发现在ios下new Date("2019-03-06").getTime()返回NaN 原因是ios下不支持“-”必须用"/" 记录备忘 var d = ...
- OpenGL函数思考-glColor
http://blog.csdn.net/shuaihj/article/details/7231980 OpenGL函数思考-glColor 函数原型: glColor3b,glColor ...
- C/51单片机
1. 串口也可以有多根线的,但是各线之间没有协调同步发送,而是各自是独自发送的.并口是同步发送,同步一次8位同时成功同时失败,类比事务. 2. ASCII码的前32位是通讯预留的编码即使现 ...
- python笔记-冒泡排序【转载】
本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python/ 前言 面试的时候经常有面试官喜欢问如何进行冒泡排序?这个问题相信能难倒一 ...
- springBoot springCloud
微服务功能的主要体现: 1)服务的注册与发现 Eureka ,Consul ,Zookeeper 2)服务的负载均衡 Ribbon 3)服务的容错 Hystrix 4)服务的网关 微服务中常用的网关组 ...