2011年7月2日土曜日

Android detecting devices

デバイスを検出する方法が Google I/O 2011 の

"Android Protips: Advanced Topics for Expert Android App Developers"

で紹介されていたので、メモしておきます。

---

なぜデバイスを検出するのか?

  • デバイスが通話機能をサポートしていない場合、TelephonyManager.getDeviceId() は null を返す。
  • Bluetooth や WiFi が見つからない(もしくはオフの)場合、MAC アドレスは使えないことがある
  • デバイスがワイプされたときに変わらないので、新しいユーザーでも同じデバイス ID になってしまう。
  • Settings.Secure.ANDROID_ID はワイプ時にリセットされるが、Android 2.2 以前では信頼性がない。


UUID.randomUUID().toString()

を使う

  1. private static String uniqueID = null;  
  2. private static final String PREF_UNIQUE_ID = "PREF_UNIQUE_ID";  
  3.   
  4. public synchronized static String id(Context context) {  
  5.     if (uniqueID == null) {  
  6.         SharedPreferences sp = context.getSharedPreferences(PREF_UNIQUE_ID,  
  7.             Context.MODE_PRIVATE);  
  8.         uniqueID = sp.getString(PREF_UNIQUE_ID, null);  
  9.           
  10.         if(uniqueID == null) {  
  11.             uniqueID = UUID.randomUUID().toString();  
  12.             Editor editor = sp.edit();  
  13.             editor.putString(PREF_UNIQUE_ID, uniqueID);  
  14.             editor.commit();  
  15.         }  
  16.     }  
  17.     return uniqueID;  
  18. }  



 

0 件のコメント:

コメントを投稿