Android displays code for WiFi signal strength and peripheral signals

Source: Internet
Author: User

First put the Activity_main.xml file code out. TextView Full Screen

?
12345678910111213141516171819202122232425 <LinearLayout 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=".MainActivity"    <!--          <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@android:color/white" />  -->     <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:textColor="@android:color/black"        android:textSize="25sp" />    </LinearLayout>

Androidmenifest.xml, add two lines, function: Get permission

?
12 <uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"/>

Finally, the program in Mainactivity.java:

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 package com.example.wifistrength;   import java.util.List;   import android.net.wifi.ScanResult; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.widget.TextView; import android.widget.Toast;   public class MainActivity extends Activity {           TextView tv;       @Override    protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                   String wserviceName = Context.WIFI_SERVICE;         WifiManager wm = (WifiManager) getSystemService(wserviceName);                    WifiInfo info = wm.getConnectionInfo();            int strength = info.getRssi();         int speed = info.getLinkSpeed();           String units = WifiInfo.LINK_SPEED_UNITS;           String ssid = info.getSSID();                     tv  = (TextView) findViewById(R.id.textView1);           List<ScanResult> results = wm.getScanResults();         String otherwifi = "The existing network is: \n\n"                  for (ScanResult result : results) {               otherwifi += result.SSID  + ":" + result.level + "\n"                          String text = "We are connecting to " + ssid + " at " + String.valueOf(speed) + "   " + String.valueOf(units) + ". Strength : " + strength;         otherwifi += "\n\n"        otherwifi += text;                   tv.setText(otherwifi);                       @Override    public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.main, menu);         return true          }

Result diagram:

Android displays code for WiFi signal strength and peripheral signals

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.