Is there a way I can get IMEI(GSM)/MEID(CDMA) without implementing run time permission?
I have included the following permission in Android, however for some devices I get the IMEI number and for some Android devices I am getting my IMEI number and for some devices I get Exception {Java.Lang.SecurityException: getDeviceId at Java.Interop.JniEnvironment+InstanceMethods...
Why is that I have to handle the permission in the runtime despite listing them out in Manifest?
var telephonyManager = (TelephonyManager)Forms.Context.GetSystemService(Android.Content.Context.TelephonyService);
if (telephonyManager != null)
{
sIMEI = telephonyManager.DeviceId;
}
If there is no way other than giving permission at runtime and then getting IMEI, could someone suggest the best way to get Unique ID for the android device programmatically. (The device ID should be seen from device as well)
I tried the below but few post suggests that AndroidId is not the same everytime.
var android_id = Android.Provider.Settings.Secure.GetString(Forms.Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
Answer is NO, If you're running on Marshmallow or above version then you need android.permission.READ_PHONE_STATE permission to access the device id.
Alternatively you can generate unique id using below code -
String uniqueID = UUID.randomUUID().toString();
OR you can follow below link for more options
deviceID
Related
Before Android 10, I was using the TelephonyManager API to retrieve that info, but it's no longer working in Android 10.
I'm facing the same problem, but, in my case, I have a kind of "backup" code that returns a UUID.
Here is a code that you could use:
String uniqueID = UUID.randomUUID().toString();
This code is usefull if you want a "instalation unique identifier" but, doesn't works as a device unique identifier because if the user unistall and re install your app, the UUID returned will be different than the last one.
In my case, I use the UUID.nameUUIDFromBytes to generated a UUID by a given "name" and I'm using the Settings.Secure.ANDROID_ID as the "name" for the UUID. Using this method you "grantee" the returned UUID will be the same, UNLESS the user do a factory reset.
Here is the code:
String androidId = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ANDROID_ID);
UUID androidId_UUID = UUID
.nameUUIDFromBytes(androidId.getBytes("utf8"));
String unique_id = androidId_UUID.toString();
Until here, everything seens ok, but the problem is: since Android 10 was released, Google doesn't recommend the uses of any kind of "hardware indentifier" and this includes the Settings.Secure.ANDROID_ID. This actually is my concern, because in the company that I work for, we use the IMEI or this UUID to identify our customers users and define if an user is trying to log in more than one device, which is not allowed by our rules, and to build some statics. If the UUID isn't unique for the same device we'll have to review all of our user access control.
Here is the Android Developers link about unique identifiers good pratices.
https://developer.android.com/training/articles/user-data-ids
And here is the same link, but with an anchor where Google describes some use cases and the best option of unique identifier for each one.
https://developer.android.com/training/articles/user-data-ids#common-use-cases
None of use cases fit with mine, so I'm still loking for a better solution.
I hope this could help someone.
From the Android Developers Documentation Website
Starting in Android 10, apps must have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access the device's non-resettable identifiers, which include both IMEI and serial number.
Third-party apps installed from the Google Play Store cannot declare privileged permissions.
If your app doesn't have the permission and you try asking for information about non-resettable identifiers anyway, the platform's response varies based on target SDK version:
If your app targets Android 10 or higher, a SecurityException occurs.
If your app targets Android 9 (API level 28) or lower, the method returns null or placeholder data if the app has the READ_PHONE_STATE permission. Otherwise, a SecurityException occurs.
If you try to access it, it throws the below exception:
java.lang.SecurityException: getImeiForSlot: The user 10180 does not meet the requirements to access device identifiers.
In app I want to get unique phone id via TelephonyManager.getDeviceId(). To use this I need this permission READ_PHONE_STATE. Problem is with runtime permission on Android 6. In runtime permission popup dialogue, it asks to grant permission "To make and manage phone calls" which can scary users from using app. What can be done? Or can I get any other unique identifier for device without using such big permission?
TelephonyManager TM = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = TM.getDeviceId();
I am using this as a unique device identifier in my app and i don't need any run time permission.
String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
or You can use this
String uuid = UUID.randomUUID().toString();
How can I get mobile number automatically soon after user installs my android application. When registered in my application without entering manually. I am using the following code.
TelephonyManager manager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = manager.getLine1Number();
Make sure you have the permission READ_PHONE_STATE in manifest
also please look here for more information!
I want to make an application which has to be very secured. So f I install an apk in a phone it should only work in that phone. When apk is shared it should not work.Can any one help me in implementing this.
One idea from my side is using an algorithm to generate password using device mac address and so the password won't work for two different devices to log in.Is there any way to get the MAC address( or something unique to device) in android from java? .Expecting alternate solutions!!
If you are distributing the app through Google play store, you can make use of the Google Play Application licensing.
You may read Identifying App Installations on Android Developer's Blog for a discussion about how to uniquely identify a device.
I implemented with IMEI number. So my apk is designed based on IMEI number.So it will check whether IMEI given matches with the device IMEI then only it will launch the new Activity, else it will exit.
TextView tx = (TextView) findViewById(R.id.tx);
String ts = Context.TELEPHONY_SERVICE;
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(ts);
String imei = mTelephonyMgr.getDeviceId();
if (imei.equals("<what we given inthe source>")) {
// Launch the activity
} else {
// show an alert dialog and exit
}
I have to make an application which shows the contact no of the SIM card that is being used in the cell. For that I need to use TelephonyManager class. Can I get details on its usage?
You can use the TelephonyManager to do this:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be unavailable.
You'll need to give your application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(You shouldn't use TelephonyManager.getDefault() to get the TelephonyManager as that is a private undocumented API call and may change in future.)
Hi Actually this is my same question but I didn't get anything.Now I got mobile number and his email-Id from particular Android real device(Android Mobile).Now a days 90% people using what's App application on Android Mobile.And now I am getting Mobile no and email-ID Through this What's app API.Its very simple to use see this below code.
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
for (Account ac : accounts)
{
acname = ac.name;
if (acname.startsWith("91")) {
mobile_no = acname;
}else if(acname.endsWith("#gmail.com")||acname.endsWith("#yahoo.com")||acname.endsWith("#hotmail.com")){
email = acname;
}
// Take your time to look at all available accounts
Log.i("Accounts : ", "Accounts : " + acname);
}
and import this API
import android.accounts.Account;
import android.accounts.AccountManager;
I have to make an application which shows the Contact no of the SIM card that is being used in the cell. For that I need to use Telephony Manager class. Can i get details on its usage?
Yes,
You have to use Telephony Manager;If at all you not found the contact no. of user;
You can get Sim Serial Number of Sim Card and Imei No. of Android Device by using the same Telephony Manager Class...
Add permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Import:
import android.telephony.TelephonyManager;
Use the below code:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
imei = tm.getDeviceId();
// get SimSerialNumber
simSerialNumber = tm.getSimSerialNumber();
Well, all could be temporary hacks, but there is no way to get mobile number of a user. It is against ethical policy.
For eg, one of the answers above suggests getting all accounts and extracting from there. And it doesn't work anymore!
All of these are hacks only.
Only way to get user's mobile number is going through operator. If you have a tie-up with mobile operators like Aitel, Vodafone, etc, you can get user's mobile number in header of request from mobile handset when connected via mobile network internet.
Not sure if any manufacturer tie ups to get specific permissions can help - not explored this area, but nothing documented atleast.
Sometimes you can retreive the phonenumber with a USSD request to your operator.
For example I can get my phonenumber by dialing *116#
This can probably be done within an app, I guess, if the USSD responce somehow could be catched.
Offcourse this is not a method I would recommend to use within an app that is to be distributed, the code may even differ between operators.
As many said:
String phoneNumber = TelephonyManager.getDefault().getLine1Number();
The availability depends strictly on the carrier and the way the number is encoded on the SIM card. If it is hardcoded by the company that makes the SIMs or by the mobile carrier itself. This returns the same as in Settings->about phone.