Android App - 讀Device ID, WiFi MAC
當需要知道多少人使用過我們開發的 APPS,或想知道利用 APPS到我們網站下載資料的實際人數,便需要讀取每部手機的唯一身份號碼,其中讀取手機的Device ID 或 WiFi MAC 址址都是可行的方法, 詳情可參考下面程式
ReadmobilenoActivity.java 程式內容
package sample.mobileno;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class ReadmobilenoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final WifiManager wm = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
final TextView finalstring = (TextView)findViewById(R.id.textView1); //Show the text number
final TextView finalstring2 = (TextView)findViewById(R.id.textView2); //Show the text number
final String tmDevice, tmWifiMac;
tmDevice = "Device ID is : " + tm.getDeviceId();
finalstring.setText(tmDevice);
tmWifiMac = "Mac of Wifi is : " + wm.getConnectionInfo().getMacAddress();
finalstring2.setText(tmWifiMac);
}
}
AndroidManifest.xml 內容
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sample.mobileno"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> //要加入這句才能讀取Device ID
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> //要加入這句才能讀取 WiFi MAC
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ReadmobilenoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
main.xml 內容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
strings.xml 內容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Read Device ID</string>
<string name="app_name">Read Device ID</string>
</resources> |