Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

c++ - How to Create a android native service and use binder to communicate with it?

My basic task is to create a native service in android and then write a simple native program to test it. lets say I want to write a simple service which return me sum of two integers. I have to use binders to talk to it from the program, I have tried to google around but I can't find a precise example. I need to know how to create a native service and find it in the program and if needed in Java also.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're creating a normal Android application using the NDK, you can't use Binder because it's not part of the NDK APIs.

Look in the NDK docs/STABLE-APIS.html for the full list of stable APIs, and this thread for an explicit comment by David Turner (the NDK maintainer) that Binder is not a supported API.

https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/1QmVRrNckfM

Your options are:

  • Use some other form of IPC in native code - for example a UNIX domain socket
  • Do it in Java, using the normal Service and AIDL facilities of the Android SDK. If you wish to combine this with native code you may be able to call up to Java from native code using JNI.
  • (Not recommended) Copy the relevant libraries and headers from an Android Open-Source Project; build into your NDK project; and use the APIs. However this is not officially supported and is extremely likely to break your application in future releases because Google are under no obligation to maintain compatibility in such libraries (and frequently do not). It's also very difficult, since you need to find some way to register the service such that the client can find it.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...