This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Android and the fly likes how to find out if code is running on the emulator? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "how to find out if code is running on the emulator?" Watch "how to find out if code is running on the emulator?" New topic
Author

how to find out if code is running on the emulator?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35249
    
    7
I've seen various suggestions how code can find out whether it runs on the emulator, but none are quite satisfactory, or "future-proof". For the time being I've settled on reading the device ID, which is all zeros for the emulator:

TelephonyManager telmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
boolean isEmulator = "000000000000000".equals(telmgr.getDeviceId());

But on a deployed app that requires the READ_PHONE_STATE permission, which otherwise wouldn't be necessary, and which may make people suspicious what the app is up to. What other techniques are people using? Is there a better solution?


Android appsImageJ pluginsJava web charts
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1365

I've seen various suggestions how code can find out whether it runs on the emulator, but none are quite satisfactory, or "future-proof".

Though I have not programmed conditionally like this before, AFAIK, there isn't any "advertised" field that makes this distinction. So, I guess it is safe to assume and say that none of the options found elsewhere are future proof. In absence of such a field, one has to resort to idiosyncrasies of the emulator which makes it different from the actual device. Device Id is one such thing.
But on a deployed app that requires the READ_PHONE_STATE permission, which otherwise wouldn't be necessary, and which may make people suspicious what the app is up to. What other techniques are people using? Is there a better solution?

If "better" solution, in your case, is finding a property that does not require a permission, see this thread.

[List of FAQs] | [Android FAQ] | [My Blog] | [Samuh Varta]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35249
    
    7
Thanks, that is a better solution. It returns null for the emulator, and an actual ID for a real device; I'm not sure why it doesn't work for the guy in that discussion.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: how to find out if code is running on the emulator?
 
Similar Threads
How to monitor sim card is changed?
Emulator takes long to start
Not able to find lot of controls in Android 3.0 Honey Comb
out of memory
Bluetooth app runs on WTK but not in real?