• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic