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列表显示
时间: 2024-11-08 14:03:06