最近要做些物联网类的应用,所以就必须要把这个搞懂,才能做一些实用的物联网设备的控制。点击获取WIFI热点信息最终效果如下:
对比查看手机的WIFI热点信息,是一致的:
简单的怎么创建一个Android app的工程就不说了,接下来说一下我的获取步骤:
1、设置用户权限
因为我们要操作Android的一些管理服务,所以一定要有权限才能去操作它。
打开AndroidManifest.xml,添加权限
当然也可以通过界面来添加:
2、画界面,添加布局:
修改布局文件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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:text="@string/connect_tip" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:text="@string/Get_AP_INFO" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"
android:text="" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="35dp"
android:text="" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:layout_marginTop="33dp"
android:text="" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_centerVertical="true"
android:text="" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginTop="25dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:layout_marginTop="29dp" />
</RelativeLayout>
3、编写代码
package com.example.android_get_ap_info;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;
//由于点击按键需要onClick方法,而这个方法是一个接口,所以要实现该接口
public class MainActivity extends Activity implements View.OnClickListener {
private TextView wifi_addr,wifi_mask,wifi_gateway,wifi_dns1,wifi_dns2,wifi_server ;
private Button Get_info ;
private WifiManager __WifiManager;
private DhcpInfo __DhcpInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件ID===>R.id.xxx 就是对应布局里的控件
Get_info = (Button)findViewById(R.id.button1);
wifi_addr = (TextView)findViewById(R.id.textView2);
wifi_mask = (TextView)findViewById(R.id.textView3);
wifi_gateway = (TextView)findViewById(R.id.textView4);
wifi_server = (TextView)findViewById(R.id.textView5);
wifi_dns1 = (TextView)findViewById(R.id.textView6);
wifi_dns2 = (TextView)findViewById(R.id.textView7);
//获取系统服务==>wifi
__WifiManager = ((WifiManager) getSystemService("wifi"));
//获取动态节点信息
__DhcpInfo = __WifiManager.getDhcpInfo();
//设置按钮监听
Get_info.setOnClickListener(this);
}
@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;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(R.id.button1 == arg0.getId())
{
wifi_addr.setText("IP地址:" + intToIp(__DhcpInfo.ipAddress));
wifi_mask.setText("掩码:" + intToIp(__DhcpInfo.netmask));
wifi_gateway.setText("网关:" + intToIp(__DhcpInfo.gateway));
wifi_server.setText("服务器:" + intToIp(__DhcpInfo.serverAddress));
wifi_dns1.setText("DNS1:" + intToIp(__DhcpInfo.dns1));
wifi_dns2.setText("DNS2:" + intToIp(__DhcpInfo.dns2));
}
}
//转换地址
private String intToIp(int paramInt) {
return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
+ (0xFF & paramInt >> 24);
}
}
4、连接手机,运行apk到手机上
略。
为什么要先知道这些东西呢?因为它可以解决我目前设计APP的一个弊端,众观市面上一些别人写的例程,通常要我去输入一个ip和端口号(一般端口号直接固定为8080),例如下图所示。为了避免人为去输入增加时间成本,使用Systemserver直接获取服务器ip这样偷懒的方法就可以避免去输入这样的麻烦步骤了。