标题:
Android wifi 信号强度的显示以及周边信号的代码
[打印本页]
作者:
我是MT
时间:
2014-12-22 22:21
标题:
Android wifi 信号强度的显示以及周边信号的代码
先把activity_main.xml文件代码贴出来.TextView充满屏幕
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
复制代码
Android
Menifest.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_U
NI
TS;
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
,不写验证不予通过哟~
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0