3

I have an application, mostly written in native code, based around NativeActivity. Mostly this runs as a full-screen activity, and it works fine like this. But for one specific use case I need to be able to run it in the background, headless, communicating with the user via notifications and toasts.

The idiomatic way to do this on Android is to use a Service; but I can't do this here, because the code absolutely has to be a NativeActivity (because the native code relies on the NativeActivity event model, and before you ask, no, I can't change this).

So I need some way to force the Activity to behave (to the user, at least) like a Service.

The least bad way I can think of is to somehow make the Activity hidden, so that it's running, but is not shown to the user. There's even a method, Activity.setVisible(), which is supposed to do this; but when I try it, it doesn't seem to work properly. (The Activity appears, but blank, and then vanishes when the application displays its first Dialog. This is on a Honeycomb tablet if that makes a difference.)

Ideally, of course, I would like a NativeService to go with NativeActivity, but unfortunately such a thing does not seem to exist, and from looking at the source code to NativeActivity, writing a NativeService looks highly non-trivial.

Suggestions?

David Given
  • 13,277
  • 9
  • 76
  • 123
  • Hi David, I know this is 2 years ago, but do you happen to still remember what is your solution for this? I too, want to run native code on Android in the background, keeping my main activity to be active – bonchenko Jan 28 '14 at 12:52

0 Answers0