- 浏览: 278423 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (142)
- android (64)
- android team 应用开发流程 (0)
- android 个人 开发流程 (1)
- android UI 切换皮肤 (1)
- java (9)
- 敏捷开发 (1)
- git (1)
- 学习 (2)
- hibernate (0)
- jQuery (1)
- windows (2)
- tomcat (1)
- Spring (3)
- struts2 (5)
- mysql (4)
- linux (15)
- JBPM (2)
- maven (4)
- 企业管理 (1)
- Iphone (1)
- 工作计划 (0)
- news (1)
- MOVE (1)
- exception-android (1)
- RFID (1)
- 测试 (7)
- android基础 (1)
- Gson (1)
- Android中的单元测试 (1)
最新评论
-
jlees:
Nice post.i hope this will help ...
Business mobile application development. The developer’s insight. -
weisi2375:
确实很详细的开发流程。
Android应用开发全流程 -
mikefather:
不错不错
Android,谁动了我的内存 -
ylzyd12345:
mark一下,谢谢分享
android的一些开源项目 -
limingcai:
确实不行,2.2就不行了,虽说2.3了 只有1.6可以
Android完全关闭应用程序
唯一标识码这东西在网络应用中非常有用,例如检测是否重复注册之类的。
import android.provider.Settings.Secure; private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
我们在项目过程中或多或少会使用到设备的唯一识别码,我们希望能够得到一个稳定、可靠的设备唯一识别码。今天我们将介绍几种方式。
1. DEVICE_ID
假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。在以前,我们的Android设备是手机,这个DEVICE_ID可以同通过 TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码,但它在使用的过程中会遇 到很多问题:
- 非手机设备: 如果只带有Wifi的设备或者音乐播放器没有通话的硬件功能的话就没有这个DEVICE_ID
- 权限: 获取DEVICE_ID需要READ_PHONE_STATE权限,但如果我们只为了获取它,没有用到其他的通话功能,那这个权限有点大才小用
- bug:在少数的一些手机设备上,该实现有漏洞,会返回垃圾,如:zeros或者asterisks的产品
2. MAC ADDRESS
我们也可以通过手机的Wifi或者蓝牙设备获取MAC ADDRESS作为DEVICE ID,但是并不建议这么做,因为并不是所有的设备都有Wifi,并且,如果Wifi没有打开,那硬件设备无法返回MAC ADDRESS.
3. Serial Number
在Android 2.3可以通过android.os.Build.SERIAL获取,非手机设备可以通过该接口获取。
4. ANDROID_ID
ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,当设备被wipe后该数重置
ANDROID_ID似乎是获取Device ID的一个好选择,但它也有缺陷:
- 它在Android <=2.1 or Android >=2.3的版本是可靠、稳定的,但在2.2的版本并不是100%可靠的
- 在主流厂商生产的设备上,有一个很经常的bug,就是每个设备都会产生相同的ANDROID_ID:9774d56d682e549c
5. Installtion ID : UUID
以上四种方式都有或多或少存在的一定的局限性或者bug,在这里,有另外一种方式解决,就是使用UUID,该方法无需访问设备的资源,也跟设备类型无关。
这 种方式是通过在程序安装后第一次运行后生成一个ID实现的,但该方式跟设备唯一标识不一样,它会因为不同的应用程序而产生不同的ID,而不是设备唯一 ID。因此经常用来标识在某个应用中的唯一ID(即Installtion ID),或者跟踪应用的安装数量。很幸运的,Google Developer Blog提供了这样的一个框架:
public class Installation { private static String sID = null; private static final String INSTALLATION = "INSTALLATION"; public synchronized static String id(Context context) { if (sID == null) { File installation = new File(context.getFilesDir(), INSTALLATION); try { if (!installation.exists()) writeInstallationFile(installation); sID = readInstallationFile(installation); } catch (Exception e) { throw new RuntimeException(e); } } return sID; } private static String readInstallationFile(File installation) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); } private static void writeInstallationFile(File installation) throws IOException { FileOutputStream out = new FileOutputStream(installation); String id = UUID.randomUUID().toString(); out.write(id.getBytes()); out.close(); } }
总结:
综 合以上所述,为了实现在设备上更通用的获取设备唯一标识,我们可以实现这样的一个类,为每个设备产生唯一的UUID,以ANDROID_ID为基础,在获 取失败时以TelephonyManager.getDeviceId()为备选方法,如果再失败,使用UUID的生成策略。
重申下,以下方法是生成Device ID,在大多数情况下Installtion ID能够满足我们的需求,但是如果确实需要用到Device ID,那可以通过以下方式实现:
import android.content.Context; import android.content.SharedPreferences; import android.provider.Settings.Secure; import android.telephony.TelephonyManager; import java.io.UnsupportedEncodingException; import java.util.UUID; public class DeviceUuidFactory { protected static final String PREFS_FILE = "device_id.xml"; protected static final String PREFS_DEVICE_ID = "device_id"; protected static UUID uuid; public DeviceUuidFactory(Context context) { if( uuid ==null ) { synchronized (DeviceUuidFactory.class) { if( uuid == null) { final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0); final String id = prefs.getString(PREFS_DEVICE_ID, null ); if (id != null) { // Use the ids previously computed and stored in the prefs file uuid = UUID.fromString(id); } else { final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID); // Use the Android ID unless it's broken, in which case fallback on deviceId, // unless it's not available, then fallback on a random number which we store // to a prefs file try { if (!"9774d56d682e549c".equals(androidId)) { uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")); } else { final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId(); uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID(); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } // Write the value out to the prefs file prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit(); } } } } } /** * Returns a unique UUID for the current android device. As with all UUIDs, this unique ID is "very highly likely" * to be unique across all Android devices. Much more so than ANDROID_ID is. * * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a * usable value. * * In some rare circumstances, this ID may change. In particular, if the device is factory reset a new device ID * may be generated. In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2 * to a newer, non-buggy version of Android, the device ID may change. Or, if a user uninstalls your app on * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation. * * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT * change after a factory reset. Something to be aware of. * * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly. * * @see http://code.google.com/p/android/issues/detail?id=10603 * * @return a UUID that may be used to uniquely identify your device for most purposes. */ public UUID getDeviceUuid() { return uuid; } }
如何获取Android手机的唯一标识?
代码: 这里是你在Android里读出 唯一的 IMSI-ID / IMEI-ID 的方法。
Java:
String myIMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI); // within my emulator it returns: 310995000000000 String myIMEI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMEI); // within my emulator it returns: 000000000000000
注:android.os.SystemProperties的标签被打上@hide了,所以sdk中并不会存在。如果需要使用,需要有android的source code支持。
转自:http://hi.baidu.com/weizi/item/f1d6671030e7e68d88a95638
发表评论
-
Resource-type-->Color State List Resource
2013-04-22 10:50 1618Color State List Resource Col ... -
Business mobile application development. The developer’s insight.
2012-11-07 17:49 1608from: http://www.enterra-inc.co ... -
Android. Handling some SQLite issues.
2012-11-07 17:48 1749转载: http://www.enterra-inc.com/ ... -
git 获取android source
2012-08-15 12:52 3644在做android开发的时,在遇到某一问题,想看andro ... -
android listview adapter
2012-06-23 14:41 978listview 在什么情况下会刷新: 1. 当ada ... -
Android多线程下载详解
2012-06-20 18:31 903http://www.pin5i.com/showtopic- ... -
Unable to open sync connection!
2012-06-18 17:04 904把设置里的USB调试重新开了开,问题解决! -
android checkbox 定制(修改checkbox 的图片)
2012-06-18 14:30 3592转载:http://www.bangchui.org/read ... -
Android ProgressBar自定义图片进度,自定义渐变色进度条
2012-06-15 16:53 7550 -
Android应用开发全流程
2012-06-15 09:21 3709转载:http://blog.csd ... -
intent.setDataAndType
2012-06-13 18:24 74151. Intent open a picture ... -
Android操作HTTP实现与服务器通信
2012-06-03 14:47 1693本示例以Servlet为例,演示Android与Serv ... -
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thre
2012-06-03 12:00 8983当应用程序启动,创建了一个叫“main”的线程,用于管理 ... -
这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用。 1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开
2012-05-25 13:58 1464这篇文章是android开发人员的必备知识,是我特别为大 ... -
android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)
2012-05-04 10:33 1814Info.java : public class In ... -
android string xliff:g
2012-03-22 10:47 984这个主要用于程序中,动态的插入内容时候使用,例如, ... -
android的一些开源项目
2011-12-07 17:13 2123转自: http://www.uuroid.com ... -
Understanding the Android Build Process
2011-11-25 12:38 929http://www.alittlemadness.com/2 ... -
Android 命令行手动编译打包详解
2011-11-24 10:07 1214Android 命令行手动编译打包过程图 【详细步骤】: 1 ... -
Android ListView 自定义背景后 滚动时的背景变黑问题
2011-11-21 14:30 1506ListView是常用的显示控件, ...
相关推荐
Android设备获取唯一标识码的方法,可以用
获取手机唯一标识 拼接的方式获取手机唯一标识 第一种方式是获取IMEI,但是有的手机如果不是正品的话,就获取不到所以通过这一种方式还是会出现有的设备是没有唯一标识的 第二种方式获取手机卡的序列号,当然这种...
Android手机的唯一标识,Android手机没有一个确切的唯一标识,有的会缺失,这个demo把几种标识加在了一起,组成了一个唯一的标识
获取Android设备唯一标识码
1、支持Android6.0系统手机不打开权限 2、支持卸载重新安装
Android--史上最全、最完整,获取设备信息、获取手机唯一标识。 主要用于拿取用户手机信息做风控。
本文主要介绍Android 获取手机号的实现方法,这里提供了实现方法,和具体操作流程,并符实现代码,有需要的小伙伴可以参考下
Android程序获取手机号码归属地。例如用户输入“150......”程序立即显示此号码归属于中国移动号。
android 获取手机SIM卡唯一值 和号码 新版本的SIM卡的号码被运营商设置 有可能获取不到
android获取手机号码_获取手机运营商 导入eclipse即可
Android 获取手机号码
主要介绍了Android获取手机号码和运营商信息的方法,以实例形式完整讲述了获取手机号码和运营商信息的技巧,代码中包含完整的注释说明,需要的朋友可以参考下
支持Android6.0系统手机不打开权限获取手机唯一标识.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android获取手机本机号码的实现方法 反射TelephoneManager 获取本机号码,注意一下提供的接口有的SIM卡没写是获取不到的,该接口只适配Android5.0以上版本 public String getMsisdn(int slotId) { return ...
主要介绍了获取安卓的手机或者平板的唯一标识的方法,需要的朋友可以参考下
Android 获取来电号码和去电号码
Android获取手机短信
Uinty获取手机端设备唯一id(设备号) 使用说明 1,GetAndroidphoneId.java 这个文件放到Plugins/Android/下 2,AndroidInterface.cs是调用Java的(与java交互) 3,新建脚本->调用 AndroidInterface.GetInstance....
本篇文章主要介绍了浅谈android获取设备唯一标识完美解决方案,具有一定的参考价值,有兴趣的可以了解一下
Android判断单双卡(5.0以上),获取手机号码;