配置文件
<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:background="@drawable/background"
tools:context=".Readelmx" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_marginTop="74dp"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="40dp"
android:layout_marginTop="18dp"
android:layout_toRightOf="@+id/textView1"
android:gravity="bottom|center_horizontal"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="@string/Strat" />
</RelativeLayout>
主类
package com.example.readelmx;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Readelmx extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// System.out.println("12312312321"+getUniqueID());
final TextView myTextView = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
//绑定匿名的监听器,并执行您所要在点击按钮后执行的逻辑代码
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myTextView.setText("设备识别号:"+getUniqueID());
}
});
}
/*
* 取设备的唯一序列 lmx 2013.02.01
*/
private String getUniqueID() {
// 1 compute IMEI
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// String imei = TelephonyMgr.getDeviceId(); // Requires
// READ_PHONE_STATE
String imei = TelephonyMgr.getSubscriberId();
if (null != imei && !imei.equals("")) {
// return getMd5(imei);
return imei;
}
// 2 android ID - unreliable
String androidID = Secure.getString(getContentResolver(),
Secure.ANDROID_ID);
if (null != androidID && !androidID.equals("")) {
// return getMd5(androidID);
return androidID;
}
// 3 we make this look like a valid IMEI, 13 digits
String devIDShort = "35" + Build.BOARD.length() % 10
+ Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10
+ Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10
+ Build.HOST.length() % 10 + Build.ID.length() % 10
+ Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10
+ Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10
+ Build.TYPE.length() % 10 + Build.USER.length() % 10;
// return getMd5(devIDShort);
return devIDShort;
}
}