一 .实验题目
使用Android SDK开发环境和工具,开发一个网络程序,能获取附近wifi热点的MAC地址和当前的GPS位置,要求运行在自己的手机(或者android手机模拟器)上。
二 实验步骤:
1.前期准备
1.1安装配置环境
首先在网址点击打开链接找到ADT-BUNDLE,下载适合自己电脑版本即可。ADT-BUNDLE与Andriod studio相比,是集成好的IDE,比较小,用起来比较方便,环境配置也不会太繁琐.
然后参考教程点击打开链接进行简单的安装,配置即可。
接着就是创建一个应用,然后编写代码,运行测试就好。
2.关键代码
2.1MainActivity类
这个类主要是显示界面,点击GPS的button可以跳转到获取GPS和网络位置的界面;点击MAC的button可以跳转到获取MAC地址的界面。
package widget.tool.rth.liying;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btnGPS,btnMac;
TextView textview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGPS = (Button) findViewById(R.id.button_gps);
btnMac= (Button) findViewById(R.id.button_mac);
btnGPS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,GPS.class);
startActivity(intent);
}
});
btnMac.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MAC.class);
startActivity(intent);
}
});
}
}
2.2GPS类
这个类里设置了一个有两个按钮的界面,一个按钮是通过GPS定位,该种方式定位比较慢,另外一个是通过网络定位。GPS功能实现的思想是要获得一个LocationManager实例,通过该实例的requestLocationUpdates()方法实现,
该方法的说明如下:
voidandroid.location.LocationManager.requestLocationUpdates(String provider, longminTime, float minDistance, LocationListener listener)
provider即定位方式,
本项目采用了GPS定位(LocationManager.GPS_PROVIDER)和网络定位(LocationManager.NETWORK_PROVIDER)。
minTime是位置更新的间隔时间。
listener是位置改变的监听器,
自己定义一个LocationListener(),
重写onLocationChanged(),加入位置改变时的动作,
本项目是直接调用显示位置的方法:showLocation(location);
package widget.tool.rth.liying;
import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class GPS extends Activity {
/** Called when the activity is first created. */
Button button,button2;
TextView textview;
LocationManager manager;
Location location;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
textview = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//判断GPS是否可用
System.out.println("state=" + manager.isProviderEnabled(LocationManager.GPS_PROVIDER));
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, locationLinstener);
textview.setText("正在获取GPS定位信息,请稍等");
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 1, locationLinstener);
textview.setText("正在获取网络定位信息,请稍等");
}
});
}
LocationListener locationLinstener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
showLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
private void showLocation(Location location) {
String currentPosition="latitude is"+location.getLatitude()+"\n"+"longitude is"+location.getLongitude();
textview.setText(currentPosition);
}
}
2.3MAC类
这个类设置的主界面只有一个按钮,点击,即可获得Mac地址。主要实现了
获取Wifi的控制类WifiManager:
WifiManager wm=(WifiManager)getSystemService(Context.WIFI_SERVICE);
然后调用getScanResults()返回wifilist,getSSID()获取无线信号提供者的名称,getBSSID()获取接入点的mac地址。
package widget.tool.rth.liying;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MAC extends Activity {
public static String IP; //本机IP
public static String MAC; //本机MAC
Button btn=null;
TextView textView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mac);
btn=(Button)findViewById(R.id.btn);
textView= (TextView) findViewById(R.id.text);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String wserviceName = Context.WIFI_SERVICE;
WifiManager wm = (WifiManager) getSystemService(wserviceName);
List<ScanResult> wifiList = wm.getScanResults();
String text="";
for (int i = 0; i < wifiList.size(); i++) {
ScanResult result = wifiList.get(i);
Log.d("dada", "bssid=" + result.BSSID+" ssid:"+result.SSID);
text=text+"wifi名:"+result.SSID+"\n"+"mac地址:"+result.BSSID+"\n";
}
textView.setText(text);
}
});
}
}
(相关权限要加入)(AndroidManifes.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="widget.tool.rth.liying"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/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>
<activity
android:name=".MAC"
android:label="@string/app_name" >
</activity>
<activity
android:name=".GPS"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
(界面,其实如果显示内容过多,应该设置滚动条的)
(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="110dp"
android:text="Li Ying's App"
/>
<Button
android:id="@+id/button_mac"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"
android:text="MAC"
/>
<Button
android:id="@+id/button_gps"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="70dp"
android:text="GPS"
/>
</LinearLayout>
(gps.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="110dp"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="30dp"
android:text="点击获取GPS信息"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="70dp"
android:text="点击获取网络定位信息" />
</LinearLayout>
(mac.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="110dp"
android:text="显示周围WiFi的MAC地址信息"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginTop="40dp"
android:text="点击获取MAC地址"
/>
</LinearLayout>
运行结果: