2008年9月8日 星期一

取得手機的相關資訊

How to get the telephony information?

在手機平台上,為了產生註冊碼,應用程式通常會要求使用者提供手機的 IMEI 碼。每個手機,都有個長 15 位數字的唯一 IMEI 碼,這可以說是手機的身分證號碼。通常在手機的背面或手機的包裝盒上,都會印有這個 IMEI 碼。你可以在手機上按下 *#06#,IMEI 碼就會顯示在你的手機螢幕上。

在 Android 的程式中,要如何取得這 IMEI 碼?

在 0.9 beta 之前的版本 (m3 or m5),你可以簡單地用 android.os.SystemProperties.get(TelephonyProperties.PROPERTY_IMEI) 取得這 IMEI 碼。

不過,在 0.9 beta 之後,這部份做了不小的更動。在 v0.9-r1 的 "API changes overview" 文件上,他提到:

You can now access these phone/subscriber properties through the new PhoneSubInfoManager, using the IPhoneSubInfo interface. Note that, to get access phone/subscriber information through the IPhoneSubInfo interface, you must add the permission ACCESS_PHONE_SUB_INFO to your application's manifest file.

吼,這段話真是錯得一塌糊塗,沒有一個是對的。在上面,我標示粗體的部份,都是不對的。

正確的方法在這啦。首先,你是要透過 TelephonyManager 這個類別,來取得資訊才對。 底下是一個簡單的範例,用來取得所有手機相關的資訊。

在模擬器上執行的結果如下:

要讓這個程式能正確執行,千萬要記住,還要在 AndroidManifest.xml 上, 加上下面這段才行。

9 則留言:

Unknown 提到...

How to set the Line1Number and SimSerialNumber? Thanks.

samlu 提到...

These information are read from either the sim card or phone hardware, so I don't think that there are any APIs opened for developers to change them.

IceskYsl@1sters! 提到...

在新版本的SDK中,你的方法好像不生效了。
注意过么?

samlu 提到...

用 1.1r1 測試都是 ok 的,是哪一段不行呢?

perrot 提到...

I was tried to get the phone number in a real device.But the function "getLine1Number" always return a empty string.Why?
How do I get the phone number in a read device?

samlu 提到...

Tried to google your 1st question first.
http://stackoverflow.com/questions/2443524/getline1number-return-null-but-not-always

As to the second question, as far as I know, there is not a reliable API to get the phone number.

perrot 提到...

Samlu, thanks for your reply.
The information is helpful.

匿名 提到...

請問一下使用Android模擬器時,他的imei碼是:000000000000000,不知要去哪個地方修改,才能改成自己所要IMEI碼?請高手解惑一下....

阿鼎 提到...

老師你好,我用你的code在我這邊跑 結果出現錯誤耶 我找不出是哪邊錯誤。 我權限也有給,怎麼會這樣= =

張貼留言