获取android设备的识别码(合成版本)

今天同事问了怎么获取andorid设备的识别码,我百度了一下,有各种不同的方法,但每一种都有一定缺陷。所以专门总结归纳了一下。大家需要的话可以看看。

package com.example.getdeviceid;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings.Secure;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	TextView txt;
	Button btn;
	Handler handler;
	static String uuid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initData();
		setListen();
	}

	@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;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public  static String getDeviceID(Context context) {

		StringBuilder deviceID = new StringBuilder();
		deviceID.append("a");
		// wifiMac--------------------- 获取wifi的Mac地址 ---------------------
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		String wifiMac = wifiManager.getConnectionInfo().getMacAddress();
		if (!isEmpty(wifiMac)) {
			deviceID.append("\n");
			deviceID.append("wifi");
			deviceID.append(wifiMac);
			Log.i("wifiMac : ", wifiMac);
			//return deviceID.toString();
		}
		//IMEI-------------------- 获取设备码IMEI ----------------------
		TelephonyManager telephonyManager;
		telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			String imei = null;
			imei=telephonyManager.getDeviceId();
			if(!isEmpty(imei)){
				deviceID.append("\n");
				deviceID.append("imei");
				deviceID.append(imei);
				//return deviceID.toString();
				Log.i("imei : ", imei);
			}
		//sn------------在SIM卡能正确读取时,获取SN码---------------------
			if(telephonyManager.getSimState()==TelephonyManager.SIM_STATE_READY){
				String sn = telephonyManager.getSimSerialNumber();
				if(!isEmpty(sn)){
					deviceID.append("\n");
					deviceID.append("sn");
					deviceID.append(sn);
					//return deviceID.toString();
					Log.i("sn : ", sn);
				}
			}
			
		//get Random UUID-------获取随机的UUID码缓存-------------------------
		uuid = getUUID(context);
		if(!isEmpty(uuid)){
			deviceID.append("\n");
			deviceID.append("UUID");
			deviceID.append(uuid);
		}
		return deviceID.toString();
	}

	public static boolean isEmpty(String str) {
		if (str == null || "" == str) {
			return true;
		} else
			return false;
	}

	/**
	 * @Description 
	 * UUID is an immutable representation of a 128-bit universally unique identifier (UUID). 
     * There are multiple, variant layouts of UUIDs, 
     * but this class is based upon variant 2 of RFC 4122, 
     * the Leach-Salz variant. This class can be used to model alternate variants, 
     * but most of the methods will be unsupported in those cases; see each method for details.
	 */
	public static String getUUID(Context context) {
		if (uuid == null) {
			final SharedPreferences preferences = context.getSharedPreferences(
					"DEFUALT_DEVICEID", 0);
			final String id = preferences.getString("device_id", null);
			if (id != null) {
				uuid = id;
			} else {
				final String androidID = Secure.getString(
						context.getContentResolver(), Secure.ANDROID_ID);
				try {
					if (!"9774d56d682e549c".equals(androidID)) {
						uuid = UUID.nameUUIDFromBytes(
								androidID.getBytes("UTF-8")).toString();
					} else {
						uuid = UUID.randomUUID().toString();
					}

				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				preferences.edit().putString("device_id", uuid).commit();
			}
		}

		return uuid;
	}

	private void initData() {
		txt = (TextView) findViewById(R.id.txt);
		btn = (Button) findViewById(R.id.btn);
		handler = new Handler() {

			public void handleMessage(Message msg) {

				txt.setText(getDeviceID(MainActivity.this));
			}
		};
	}

	private void setListen() {

		btn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				handler.sendMessage(handler.obtainMessage());
			}
		});
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值