Android wifi 信号强度的显示以及周边信号的代码
时间:08-30
来源:互联网
点击:
先把activity_main.xml文件代码贴出来.TextView充满屏幕
AndroidMenifest.xml中,加入两行,作用:获取权限
最后是MainActivity.java中的程序:
查看源码打印?
结果图:

希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:278744577,验证码:eec,不写验证不予通过哟~
- 01
- 02
- xmlns:tools="http://schemas.android.com/tools"
- 03
- android:layout_width="match_parent"
- 04
- android:layout_height="match_parent"
- 05
- android:paddingBottom="@dimen/activity_vertical_margin"
- 06
- android:paddingLeft="@dimen/activity_horizontal_margin"
- 07
- android:paddingRight="@dimen/activity_horizontal_margin"
- 08
- android:paddingTop="@dimen/activity_vertical_margin"
- 09
- tools:context=".MainActivity" >
- 10
- 11
- 17
- 18
- android:id="@+id/textView1"
- 19
- android:layout_width="match_parent"
- 20
- android:layout_height="match_parent"
- 21
- android:gravity="center"
- 22
- android:textColor="@android:color/black"
- 23
- android:textSize="25sp" />
- 24
- 25
AndroidMenifest.xml中,加入两行,作用:获取权限
最后是MainActivity.java中的程序:
查看源码打印?
- 01
- package com.example.wifistrength;
- 02
- 03
- import java.util.List;
- 04
- 05
- import android.net.wifi.ScanResult;
- 06
- import android.net.wifi.WifiInfo;
- 07
- import android.net.wifi.WifiManager;
- 08
- import android.os.Bundle;
- 09
- import android.app.Activity;
- 10
- import android.content.Context;
- 11
- import android.view.Menu;
- 12
- import android.widget.TextView;
- 13
- import android.widget.Toast;
- 14
- 15
- public class MainActivity extends Activity {
- 16
- 17
- TextView tv;
- 18
- 19
- @Override
- 20
- protected void onCreate(Bundle savedInstanceState) {
- 21
- super.onCreate(savedInstanceState);
- 22
- setContentView(R.layout.activity_main);
- 23
- 24
- String wserviceName = Context.WIFI_SERVICE;
- 25
- WifiManager wm = (WifiManager) getSystemService(wserviceName);
- 26
- 27
- WifiInfo info = wm.getConnectionInfo();
- 28
- int strength = info.getRssi();
- 29
- int speed = info.getLinkSpeed();
- 30
- String units = WifiInfo.LINK_SPEED_UNITS;
- 31
- String ssid = info.getSSID();
- 32
- 33
- tv = (TextView) findViewById(R.id.textView1);
- 34
- 35
- List results = wm.getScanResults();
- 36
- String otherwifi = "The existing network is: \n\n";
- 37
- 38
- for (ScanResult result : results) {
- 39
- otherwifi += result.SSID + ":" + result.level + "\n";
- 40
- }
- 41
- 42
- String text = "We are connecting to " + ssid + " at " + String.valueOf(speed) + " " + String.valueOf(units) + ". Strength : " + strength;
- 43
- otherwifi += "\n\n";
- 44
- otherwifi += text;
- 45
- 46
- tv.setText(otherwifi);
- 47
- 48
- }
- 49
- 50
- 51
- @Override
- 52
- public boolean onCreateOptionsMenu(Menu menu) {
- 53
- // Inflate the menu; this adds items to the action bar if it is present.
- 54
- getMenuInflater().inflate(R.menu.main, menu);
- 55
- return true;
- 56
- }
- 57
- 58
- }
结果图:

希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:278744577,验证码:eec,不写验证不予通过哟~
- 传联想明年将推全球第3款Android手机OPhone/图(11-15)
- 详解Android组件的使用(01-07)
- 基于Android平台的双网双待的设计方法(09-10)
- Android平台手机防盗系统的安全解决方案(06-10)
- 13个基于Android平台的设计汇总,包括通信客户端、手机游戏等(02-10)
- 面向三网融合的互联网电视关键技术与发展趋势(11-24)
