我有一个平板电脑用户将登录的基本登录屏幕,我想在应用程序启动时在屏幕上显示该特定设备的 MAC 地址。
后来,随着我对 Android 开发的了解越来越多,而且我学得越来越慢,我想通过 RESTful Web 服务将用户名、登录时间和 MAC 地址插入数据库。但现在我只是想显示 MAC 地址。
我找到了一些用于从 WIFI 管理器获取 MAC 地址的代码,您将在下面的代码中看到并将其放入方法中。似乎当我在 onCreate 方法中调用此方法时,应用程序会爆炸。什么是正确的工作流程?我应该从 onCreate() 方法中调用 showMACAddress() 吗?它应该在尝试/捕获中吗?
顺便说一句,我正在使用 Android 4.0 的 7 英寸 Kenton 平板电脑上进行测试,并且我在 Android Manifest 文件中指定该应用程序仅为横向。先感谢您。
这是我的java类
package za.co.crcode.Inspector;
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;
public class DeviceLogin extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_login);
showMACAddress();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_device_login, menu);
return true;
}
public void showMACAddress(){
WifiManager wifiMan = (WifiManager) this.getSystemService(
Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();
TextView loginDeviceMACAddress = (TextView)findViewById(R.id.LoginDeviceMACAddress);
CharSequence macAddy = (String) macAddr.toString();
loginDeviceMACAddress.setText(macAddy);
}
}
这是我的 Android GUI 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"
tools:context=".DeviceLogin" >
<LinearLayout
android:id="@+id/LoginFieldsLayoutLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="50dp"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:id="@+id/LoginScreenHeading"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:text="@string/loginScreenHeading"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/LoginFirstName"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/loginFirstNameHint"
android:inputType="textPersonName"
android:text="@string/loginFirstName" />
<EditText
android:id="@+id/LoginLastName"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/loginLastNameHint"
android:inputType="textPersonName"
android:text="@string/loginLastName" />
<EditText
android:id="@+id/LoginEmployeeID"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/loginEmployeeIDHint"
android:inputType="number"
android:text="@string/loginEmployeeID" />
<EditText
android:id="@+id/loginPassword"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/loginPasswordHint"
android:inputType="textPassword" >
</EditText>
<TextView
android:id="@+id/LoginMACAddressLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="@string/LoginMACAddressLabel"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/LoginDeviceMACAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="@string/LoginMACAddress"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout
android:id="@+id/LoginFieldsLayoutRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/LoginIdPic"
android:contentDescription="@string/EmployeeImage"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="200dp"
android:maxHeight="200dp"
android:scaleType="fitCenter"
android:src="@drawable/idpic" >
</ImageView>
<Button
android:id="@+id/LoginScreenButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loginButtonText"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
这是我的 Strings.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Inspector</string>
<string name="hello_world">Hello World!</string>
<string name="menu_settings">Settings</string>
<string name="loginScreenHeading">Device Login</string>
<string name="loginFirstName">First Name</string>
<string name="loginFirstNameHint">Enter your first name</string>
<string name="loginLastName">Last Name</string>
<string name="loginLastNameHint">Enter your last name</string>
<string name="loginEmployeeID">Employee ID</string>
<string name="loginEmployeeIDHint">Enter your employee ID</string>
<string name="loginPasswordHint">Password</string>
<string name="LoginMACAddressLabel">Device ID</string>
<string name="LoginMACAddress">00:00:00:00</string>
<string name="loginButtonText">Login</string>
<string name="EmployeeImage">Employee_image</string>
</resources>