Detect user is me - android

I'm sure this will have been asked before, but I don't know what to search for...
Anyway I have an app which I want to behave in a certain way except when being operated by me or on my specific phone - this is for debugging purposes. I can think of all sorts of clumsy ways of doing this, but I suspect there may be a standard way... is there?
EDIT: this code is one possible solution:
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
if (getSimSerialNumber.equals("my_serial_number"))
{
// yes its me!
}
The only problem is that it requires an extra permission.

I think that the best way to do this could be by simply using a unique device identifier. It can change or be null in some cases, but since you only need it to make sure that this is YOUR device, you can use it without fearing of some issues.
Here is how to use that(taken from Is there a unique Android device ID?:
import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
You can print that(so you know what the ID of your device is) and later compare it with that result(I'm not sure of the correct if() statement syntax, so I'll leave that up to you).

You can create a file in SD card, and check the presence of that file. If the file is present, you can assume that it is yours and print extra debugging information etc

Check the phones Id and phone number. Write your own web script to check whether the phone is allowed to access your app. You can use a database for this.

Related

IMEI Number on non-network devices

I'm currently working on an application where the code uses the device IMEI number as a variable to store something in the DB. We need to see what device the reading originated from.
ie:
String IMEI = telephonyManager.getDeviceId();
However, I'm not sure how this will behave on phones without network access, such as a wifi only tablet. Will is return a unique value? Will it return null?
I know there are alternatives to using IMEI, and I don't need any explanation on what alternative values I can use. I only need to know what will happen when I call this on a phone with no network access. Please do not provide suggestions on alternative IDs or anything other than what I have asked. I appreciate you taking the time to read this question.
Thanks,
-Mark
You get null.
Just ran into this on an app I wrote 3 years ago, and only just converted to tablet format. I had forgotten we were using IMEI (against the advice you and I both received ;-) ) and we started sending a whole bunch of null device IDs to the server.
Sadly it took a whole lot of network logs before I tracked it down to that little hack.

Retrieving phone number from SIM card error

This is my code
TelephonyManager mTelephonyMgr =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String sDeviceID = mTelephonyMgr.getDeviceId();
String sSimSerial = mTelephonyMgr.getSimSerialNumber();
String sSimlineNumber = mTelephonyMgr.getLine1Number();
with the help of this i want to read user phone number from his/her SIM card i also add READ_PHONE_STATE permission in manifest file. Eclipse showing no error in my code but still i get nothing in the end. Is there anything wrong with my code?
See this thread for more information on this issue. Specifically, the below comment on the accepted answer:
Actually, not so perfect. Last time I tried this method, it reported the phone number that my phone originally had, before my old mobile number was ported over to it. It probably still does, as the Settings app still shows that defunct number. Also, there are reports that some SIMs cause this method to return null. That being said, I'm not aware of a better answer. – CommonsWare
It seems that getting a user's phone number is not as simple as asking the OS to hand it over, for reasons beyond the control of the developer. An alternative is to populate whichever field you need with the number the OS gives, but also let the user edit the phone number in case the OS is wrong.

Is android.os.Build.SERIAL unique?

I would like to use a unique id for android device that works for phone and Tablet.
(IMEI doesn't work with no SIM card device and sometime MAC Address return null)
I'm not sure is android.os.Build.SERIAL unique or not.
Does anyone know about this?
Thanks,
Regards.
Yes, but note that it was only added in API level 9, and it may not be present on all devices. To get a unique ID on earlier platforms, you'll need to read something like the MAC address or IMEI.
Generally, try reading all the possible IDs, and use whichever are available. See this article for guidance.
You can use Build serial and android ID to make your own unique id.
String serial = Build.SERIAL;
String android_id =Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
String myKey=serial +android_id ;
Serial was only exposed in API:9. but you can get it in older versions using reflection. However the docs mention "if available" so I guess don't rely on it.
String deviceSerial = (String) Build.class.getField("SERIAL").get(
null);
I think for unique Id you should used android Id.
following is code to get Android Id.
String android_id = Secure.getString(this.getContentResolver(),
Secure.ANDROID_ID);
Log.d("Android","Android ID : "+android_id);
If it is there, then it is expected to be unique. But there is no guarantee that this property is set. Also it is API 9. Unfortunately there's no easy way to uniquely identify the device. Some properties like said SERIAL can be present, others like ANDROID_ID as NOT unique, some other like MAC depends on presence of WIFI or its state (if wifi module is off, the you may not be able to read its MAC). Some like IMEI cannot be read even device got phone module. So the best approach is to gather as much data as you can, and try to build something you could most likely consider unique device ID
What about a combination of MAC, IMEI and SERIAL ?
You just have to deal with the fact that they all could be non existant esp. on older phones without SIM.
I just find it odd that MAC would return null. This should not be possible imho, as it makes no sense that a mobile device has no MAC.
There are 2 MAC addresses possible but they can be non accessible in some cases.

Android phone number

The following code does not give me the phone number of the device ,how can i get the phone number
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
Toast.makeText(getApplicationContext(),"Telephone number: "+ number,
Toast.LENGTH_SHORT).show();
ALso i have tried the following..but the number doest show up
Programmatically obtain the phone number of the Android phone
How to get the mobile number of current sim card in real device?
The only method available from Android API as all the comments suggest is getLine1Number(). However I have never been able to obtain it since your operator must provide you with a SIM that supports letting phone read internal phone number. That seems not very common, so I am afraid that you could be left without means of knowing it.
That's why some programs that use your phone number to identify you (i.e. Whatsapp) do ask user about his/her phone number, because there is no sure way to getting it programatically.
the code provided:
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
should work fine, however, it will not if you forgot the permission "READ_PHONE_STATE".
Hope this works now :)
Try this:
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
Hope it help you
[In case if anyone still stumbles here looking for this still existing age-old problem]
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimised with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.

Why can't the TelephonyManager get the phone number from the SIM?

I use the TelephonyManager to do this and you can access TelephonyManager s methods as given below
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = telephonyManager.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 available.
I have also given application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
The number is always blank or null...
While I have also crosschecked calling and i'm able to call from that number(i.e inserted sim no)
I have gone through numerous documentations but nothing help me out to get the phone number from sim so as to be used in my app..
With Regards,
Arpit Garg
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimized with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
you should try this
TelephonyManager phoneManager = (TelephonyManager)
getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();

Categories

Resources