How to implement NativeActivity while using Android SDK's thread

435 views
Skip to first unread message

Sky Hsiao

unread,
Mar 1, 2011, 3:22:36 PM3/1/11
to android-ndk
Hi, I'm new to NDK code.
I've already had Android Java code, and I just wanna add some NDK code
to get input event (Because I need to implement hardware part).
In Java code, I already have an Activity class to do game thread and
other graphic display. if I want to get the control event, I should
implement NativeActivity, but it won't work because there's only one
activity in application, and all my code is in Activity class, which
has a view with thread. And I don't wanna re-write all java code to c/c
++ code.
I look around the demo code but still can't find a better way to do
that.
Can anyone give me some suggestion how to do this?
Thanks

David Turner

unread,
Mar 2, 2011, 12:02:07 AM3/2/11
to andro...@googlegroups.com, Sky Hsiao
On Tue, Mar 1, 2011 at 9:22 PM, Sky Hsiao <little...@gmail.com> wrote:
Hi, I'm new to NDK code.
I've already had Android Java code, and I just wanna add some NDK code
to get input event (Because I need to implement hardware part).

I don't get the "I need to implement hardware part" here. What prevents you from getting the input events from Java?
It will probably much easier than using JNI.
 
In Java code, I already have an Activity class to do game thread and
other graphic display. if I want to get the control event, I should
implement NativeActivity, but it won't work because there's only one
activity in application, and all my code is in Activity class, which
has a view with thread. And I don't wanna re-write all java code to c/c
++ code.
I look around the demo code but still can't find a better way to do
that.
Can anyone give me some suggestion how to do this?
Thanks

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


lorydi

unread,
Mar 9, 2011, 5:50:41 AM3/9/11
to android-ndk
I have the same questions as you have. The problem is for example with
the touchpad from xperia play that only has events in native code. Did
you find any solution?

Thank you.

Dianne Hackborn

unread,
Mar 9, 2011, 12:27:25 PM3/9/11
to andro...@googlegroups.com, Sky Hsiao
I'm not sure exactly what you are asking, but:

(1) You can inherit from NativeActivity if you also want to use Java code with it.
(2) You don't need to use the standard glue code (that puts your native loop in a separate thread), you can directly use the public NDK APIs to hook up your native code however you want.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Dia

unread,
Mar 9, 2011, 1:58:25 PM3/9/11
to andro...@googlegroups.com
I understand. But am I able to get the Motion event in native code? Somehow I have to listen to this events in native code. So I don't see any other option than to have a native thread.

The fact is I have a normal Java application made with SDK. I have to adapt it to work on the Xperia Play. As you know the additional touchpads only give events in native code. They can not be tracked from Java. I only want to get those events without altering my existing application or rewriting it into C.

So please. If you have an example for this: read touch events from native code but make paint in normal SurfaceView in Java share it with us.
Thank you very much.
 Diana Loredana Radu
 Email: diana.lor...@gmail.com
 Phone: 0747.11.30.27

" Fii schimbarea pe care vrei să o vezi în lume! " Gandhi :)


Dianne Hackborn

unread,
Mar 9, 2011, 3:19:46 PM3/9/11
to andro...@googlegroups.com, Dia
You can only receive the event stream one place or the other.

2011/3/9 Dia <loryd...@gmail.com>

Sky Hsiao

unread,
Mar 10, 2011, 1:10:55 PM3/10/11
to android-ndk
I think I got some progress.
The way I did is to use NativeActivity with Java code, just like the
Xperia Play's example code.
(I change my Java's Activity to NativeActivity, and load C code
library.)
In my previous code, I have a SurfaceView with SurfaceHolder.Callback
implemented and have a thread.
I called the thread to start in the surfaceCreated() method.
So right now, in NativeActivity class, I add surfaceCreated() method
and in this mothed I call SurfaceView's surfaceCreated, then I can
make the thread start.
And I also put NativeActivity's SurfaceHolder to SurfaceView, so that
I can get the Canvas and do the drawing.
(
Canvas c = null;
c = mSurfaceHolder.lockCanvas(null);
mGraphics = new Graphics(c);
)

So after that, it seems the thread work and there's a drawing and I
also can get touch event works!
But the problem is, the NativeActivity has black screen block on it.
I don't know if it's C code's issue.
So if someone can tell me how to get away that black screen?



On Mar 9, 10:58 am, Dia <lorydi3...@gmail.com> wrote:
> I understand. But am I able to get the Motion event in native code? Somehow
> I have to listen to this events in native code. So I don't see any other
> option than to have a native thread.
>
> The fact is I have a normal Java application made with SDK. I have to adapt
> it to work on the Xperia Play. As you know the additional touchpads only
> give events in native code. They can not be tracked from Java. I only want
> to get those events without altering my existing application or rewriting it
> into C.
>
> So please. If you have an example for this: read touch events from native
> code but make paint in normal SurfaceView in Java share it with us.
> Thank you very much.
>
>
>
> On Wed, Mar 9, 2011 at 7:27 PM, Dianne Hackborn <hack...@android.com> wrote:
> > I'm not sure exactly what you are asking, but:
>
> > (1) You can inherit from NativeActivity if you also want to use Java code
> > with it.
> > (2) You don't need to use the standard glue code (that puts your native
> > loop in a separate thread), you can directly use the public NDK APIs to hook
> > up your native code however you want.
>
> > hack...@android.com
>
> > Note: please don't send private questions to me, as I don't have time to
> > provide private support, and so won't reply to such e-mails.  All such
> > questions should be posted on public forums, where I and others can see and
> > answer them.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
>  Diana Loredana Radu
>  Email: diana.loredana.r...@gmail.com

Dianne Hackborn

unread,
Mar 10, 2011, 2:36:37 PM3/10/11
to andro...@googlegroups.com, Sky Hsiao
Gah.  PLEASE DON'T DO THAT.

This is so broken, I don't even know where to start.

Do like I have already said two times -- use getWindow().takeSurface(null).

You can even use this to optimize your app -- this is a new feature of GB that allows an application to take complete ownership of drawing directly to its window's surface.  Basically if all you want is a big SurfaceView covering your entire window, you can replace the SurfaceView completely with this call to getWindow().takeSurface() and pass in your own SurfaceHolder.

Dia

unread,
Mar 10, 2011, 3:03:42 PM3/10/11
to andro...@googlegroups.com, Sky Hsiao
Yes:) Just got that one solved with Dianne's help. You should call  getWindow().takeSurface(null); in NativeActivity onCreate(). It really solves the problem.

Good luck.
Reply all
Reply to author
Forward
0 new messages