Android gets the wifi list and androdwifi list

Source: Internet
Author: User

Android gets the wifi list and androdwifi list

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"/> <! -- Create and delete file permissions in SDCard --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Connect to <a href = "http://www.it165.net/news/nhlw/" target = "_ blank" class = "keylink"> Internet </a> Internet permissions --> <uses-permission android: name = "android. permission. INTERNET "/> <! -- GPS Positioning permission --> <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. w Idget. 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 is not enabled! ", Toast. LENGTH_LONG ). show ();} else {listView. setAdapter (new MyAdapter (this, list);}/*** enable 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 = 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); // determines the signal strength and displays the corresponding indicator icon 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 );}}

From: Display wifi list for android wifi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.