Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android : Get or create unique id for each device

I am developing an application using api 14 (android 4.0).

in manifest:

 <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

I want to get an unique id from each device (or create one) that could be the same even after reboot the device. But it is important that the id be different even for 2 same devices. How can i do that?

like image 206
m n Avatar asked Mar 01 '26 10:03

m n


2 Answers

You can use device's IMEI number as unique Id.

You want to call android.telephony.TelephonyManager.getDeviceId().

This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA).

You'll need the following permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
like image 105
Sahil Munjal Avatar answered Mar 03 '26 01:03

Sahil Munjal


You can generate different device token using GCM.... And this device token will remain same even if you will uninstall and again installed the application or after factory setting.. you have to follow some steps...... Create a new project at Google Developers Console . At this step, for simplicity, you just need to take note of 2 values: Project Number, which will be used as SENDER_ID in the client project; and API server key (created at Credentials), which will be used as API_KEY in the server project. Create a new simple Android project for server side (with basic source code as my answer in the following links).

Create a new simple Android project for client side (with basic source code as my answer in the following links, I customized from the original source at Google Cloud Messaging - GitHub).

Run the client app, you will get the registration token (means that your device has successfully registered). Then, paste (hard-code) this token at CLIENT_REGISTRATION_TOKEN variable in server app (or write code to send this token to server app). You can read more at the following questions, one of them you have read before with one of your previous questions:

How to implement a GCM Hello World for Android using Android Studio Adding Google Cloud Messagin (GCM) for Android - Registration process

like image 37
S.Singh Avatar answered Mar 03 '26 01:03

S.Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!