0

I want to access Android API classes outside an Android project. For example is it possible to get an object to the Context of the device connected to a machine or the running emulator?

This will allow access to a system services like PowerManager or ActivityManager outside an Android device. If not via Context object, is there any other way to access the system services for a device/avd outside Android?

wowamit
  • 3
  • 4

3 Answers3

3

No way. Distributed android API classes are merely stubs good enough to compile against them. Even most innocent stuff is stubbed out to throw RuntimeException on invocation. If you like to know status of the system, you will have to use SDK tools. Or write app exposing android objects via some remote access technology

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • Ok. SDK tools is a use as-is way and I would have really liked to base some actions on the status of the device. I guess I will have to make something sit on the device and make it communicate outside remotely. Another way am evaluating is using adb. However am not sure if all things are possible, for example finding running processes. – wowamit Mar 25 '12 at 16:50
  • you can get from adb everything you can get from shell. running ps is possible. And I think sources of ADB are available - so just look what protocols it is using. – Konstantin Pribluda Mar 25 '12 at 18:13
0

I very much doubt that it is possible. The distributed SDK classes do not include many parts of the internal API. See, for example, this thread. Besides, what use would there be to have a system service object like PowerManager without a system (or an emulation of one) to back it up?

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • First thing, the system service object is required for a system that _is_ running (connected device or running avd). Second, it would help know the status of the device/emulator system outside the actual system. – wowamit Mar 25 '12 at 16:16
  • 1
    @wowamit - I think the best you can do is write a service that exposes particular functionality to an outside connection (e.g., a web service hosted on the phone). You cannot write a Java desktop program that direclty links to classes in the Android API. – Ted Hopp Mar 25 '12 at 16:24
  • Thanks! Looks like remote communication is the only way. Though was hopeful of using at least objects related to system status outside of Android scope. – wowamit Mar 25 '12 at 16:52
0

It sounds like what you're trying to do is not really access things on the device, as much as remotely control the device. In this case, there are some external tools that you should look into. The tools are mainly focused on testing, and are based on instrumentation for apps. You can look at robotium and monkeyrunner, to start with, as they provide a bit of functionality that might help you accomplish what you want. Other than that, you can also do what those tools do and write an app which listens for intents from adb, performs actions based on those intents, etc..., but you'll obviously be doing a lot of communication at a high level, so this might not be the most efficient (and I'm not sure how you'd transfer much data, which would be required for real RPC, which it sounds like you want to do).

Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34
  • Couple of issues that I have with Robotium and Monkeyrunner. One, it is specific for an application. Second, even for that, they assume access to the code which might not be the case every time. Though do agree with your point about a lot of communication that will have to happen, exactly the reason I wanted to avoid going via on-device option. – wowamit Mar 25 '12 at 17:09
  • you'll certainly need something on the device running, as the device and Android OS don't support this sort of thing currently... – Kristopher Micinski Mar 25 '12 at 17:15
  • Guess will have to find a way somehow to handle that. Thanks anyway for the pointers. – wowamit Mar 25 '12 at 17:22