Can one access Android API in a Java Application outside Android? - android

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?

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

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?

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).

Related

Calling Android's cpp code from your app

I want to call functions provided in com_android_bluetooth_hid.cpp in android source from my own app. Target Android versions are >=4.4 and <=5.x. I understand JNI, and can compile my own code and call from my own app.
Would it be possible to call android's library from my app? How
Also, to call functions in that specific cpp file, are BLUETOOTH and BLUETOOTH_ADMIN permissions sufficient or something else (other permission, or root) would be required?
The cleanest way to call Android's CPP code is, generally, copying the relevant pieces to your repository and building as part of your app. Following this approach, your app will only call the public APIs - either Java or C/C++.
But this may not be relevant in cases when this code must run with special permissions, as system service, like mediaserver or Bluetooth stack.
I am afraid that the code you wish to run belongs to this second category, therefore none of the available tricks will not help.
On the other hand, a custom ROM may be a solution you are looking for. It may happen that you don't need to forge a full system. It may be enough to use a rooted device and replace the bluetooth service with your custom one.
I would still advise you to provide the missing callbacks for the service, instead of calling the functions directly from your app: you better not mix contexts between a user-space app and system service.

Is it possible to write applications on android using only C?

Now wait just one moment before you mark this as a duplicate, because this is a very specific question.
I'm not asking if you can write an application using another language which is a binding using the NDK; what I want to know is:
At a hardware level, how does dalvik interact with the Linux kernel on android devices?
The answer to (1) as I understand it, is that because android is fundamentally a Linux system, this is done via syscalls. Which is to say, at some level the davlik/art VM must interact with the C Linux kernel through a C API.
If you had root permission on a device, why could you not do the same thing from a native system binary?
So, certainly it would be a painful experience, but technically is there any reason why it would not be possible to write an application purely in C, without using the android run-time at all?
(Note: Not without the run-time; obviously the run-time must be present to do various things like device initialization; but a stand alone binary that did not interact with the run-time).
(I can think of a few reasons why this might be the case, specifically the run-time requiring exclusive hardware access to various hardware, but I can't find any specific documentation about it)
It is possible, this is how daemons work on Android (think RILD for example). However you cannot gain access to the Android facilities (graphics, location, etc) as there is no API from C.
Note that in order to speak to the Android API, your process needs to be a child of zygote. If you spawn a process from an ADB shell, or from init you will not be a fork() of zygote and will not have direct access to the JVM.

Turn On GPS using NDK

I have read a lot of posts about managing GPS in android using code.
All of them saying that it is not possible. But they are all using android SDK to talk about the topic. Since it is not possible with SDK, can it be done using NDK?
I am asking this question, even after reading all those, because the android settings application can enable/disable it, which means it is not impossible.
In my app, it needs to turn GPS for a while and turn it off (for power reduction) and send the GPS data to a server.
Ultimately android is Linux based. So there must a way to turn it on/off using the Linux commands/APIs right? (or using the NDK) can anyone share the ideas about that?
If it is not at all possible (last option), is there any alternate way to get location information using any other functionalists.
In general, the Android NDK interface is much more limited than the SDK interface. To supplement it, you are able to access Java functions through the JNI interface. There is no extra functionality in the NDK that does not already exist in the Java SDK.

Network traffic monitoring per android application

I was wondering if I can do network traffic monitoring per android application?
Basically see which app is receiving/sending how much data?
I know there are many apps which already do that but I want to know how to do it.
Well you surely can. A very simple way is to use the TrafficStats class. It can manage per app (more formally, per UID) But the problem is, it can return UNSUPPORTED. When this happens, I don't think you will have some easy (i.e. using the high level Java language) method to get the network traffic data you need.
Not sure if it can be implemented in Java as I imagine you need to access some low-level Operating system functions which are just not available in Java (but don't quote me on this!).
The obvious way I see of doing this is through C language and the Android Native Development Kit.

Android: invoking a regular Java-style program with i/o redirection

I have an apk (or .class, whatever) with a 'public static void main'-method (java style) which does some things. Compiling and installing the apk this gives works fine (from eclipse).
Now from a regular Android app I would like to invoke that code while redirecting its stdin/stdout to Input-/Output- Stream objects. Is this possible? And if so: how? And if not: is there some other way in which I can run an activity in the background with some kind of pipe/io-redirection construction?
Thanks.
Android absolutely supports pipes as well as unix domain sockets. Use of exec is somewhat discouraged, but works at the moment.
See the source of any android terminal emulator with a local shell option for an example of how to do it. Essentially your gui just replaces the terminal emulator, and your engine replaces the shell.
If using exec becomes a problem in the future, you will need to compile your engine as a jni library rather than a stand alone executable. That's not necessarily too hard - just graft it onto the ndk hello-jni example, and have a single jni function that calls main(). Call this from a java thread. Communicate with pipes as before, or rig up some other message passing scheme using jni.
Note that the "use a service" answers are going to require a java wrapper in the service too. As of the moment, you can't make a pure-native service using any supported/endorsed mechanism unless you are the platform vendor making and registering a system service.
Also be aware that your engine will need to be able to save any state, and do so at the same time the java side would need to under the android activity lifecycle (essentially, once you get paused you become killable without further notice)
You'll probably be looking at a Service and Intent based design for your app. The Android design/architecture/philosophy doesn't support streams/pipes between processes. In the case of a chess-like app the UI would be an activity and the engine might be a service. For Android the UI is the most important thing and your design will have to take that into account. When a move is made an intent could be fired off from the UI to the background service which would wrap the engine code (Java or perhaps in C via NDK). Once the reply move is ready, it would be returned back to the UI Activity and displayed. If you can't fit all of the data you want in the Intent, you may need to create a Content Provider which would allow the UI to get the missing elements. Again I'm thinking about a well made app. If the game logic doesn't take too long (or you don't need a completely 'nice' UI experience, i.e. game restart if you leave the game) you may be able to stick everything in one Activity and use an AsyncTask/Thread for game logic.
you can do it with intents :)
check android-developers

Categories

Resources