HelloWorld HelloWorld - 2 months ago 28
Android Question

How to test an Android native code snippet with Codename One?

First off I am used to programming with Java that's why I am using Codename One to develop mobile applications.

However sometimes I see some code in Android "format" that I am interested on testing. I know how to set up basic native interface thanks to Codename one tutorial.

For example, I would like to test this snippet about real time sound processing. However it involves initializing some variable in the Android

method with data that is available in this method such as
am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
with the use of
which has not the same reference in the Codename One Native Interface. Maybe I don't have to use the
method (which could be reached from Codename One) but I am not an Android guru (nor a CN1 one either!), so I don't know.

Consequently what changes do I have to make to test native Android code in Codename One native interface ? Maybe there is a methodology that I would be glad to hear of.

EDIT SOLVED : Code used in the native interface implementation that works

Here is the Codename One native interface implementation of the original Android code. Indeed Android
method has not been used but the things that were initialized in it have been transfered in the
method which is called when suited in the Codename One's form. It uses the same method as described below by @akash thus replacing

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import android.content.Context;

public class KestudisNativeInterfaceImpl {

boolean isRecording = false;
AudioManager am = null;
AudioRecord record = null;
AudioTrack track = null;

public void initRecordAndTrack() {
android.app.Activity ctx = com.codename1.impl.android.AndroidNativeUtil.getActivity();

am = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
int min = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,
// if (AcousticEchoCanceler.isAvailable())
// {
// AcousticEchoCanceler echoCancler = AcousticEchoCanceler.create(record.getAudioSessionId());
// echoCancler.setEnabled(true);
// }
int maxJitter = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, 8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, maxJitter,

(new Thread() {
public void run() {

public void startRecordAndPlay() {
isRecording = true;

public void stopRecordAndPlay() {
isRecording = false;

private void recordAndPlay() {
short[] lin = new short[1024];
int num = 0;
while (true) {
if (isRecording) {
num = record.read(lin, 0, 1024);
track.write(lin, 0, num);

public boolean isSupported() {
return true;


Thanks a lot,



Have a look at this link https://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html

In android this normally refer to context and to access context in CN1 you can replace this with com.codename1.impl.android.AndroidNativeUtil.getActivity()

hope that helps