Android Question JavaObject Release

MarcoRome

Expert
Licensed User
Longtime User
Hi all,
JavaObjects remain in memory once there are created, is it possibile release Javaobject ?
Thank you very much
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
Hi Erel, Than kyou for your answer.
I give you an example, I have a button that invokes the java functions. Everything works, but if I try to re-run the command (tap again the button), the code runs with the previous results :confused:.
if i go out from the app and return everything works.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Can depend this code:
B4X:
public void _onCreate() {
   //  RFT -->
          BA.Log("Entro in Create");
        handler = new MyHandler();
   //  <-- RFT
}

maybe is necessary that i call _onDestroy ?
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi Erel i have this code:

B4X:
.....
Sub Button1_Click
jo.RunMethod("Coffe", Array(1))
tempo1.Initialize("tempo1", 500)
tempo1.Enabled = True
 End Sub

Sub tempo1_Tick

Dim ritornodo As Int = jo.RunMethod("RitornoDo", Null)
'Qui controllo che abbia caricato il valore e che non sia 100
'nel caso in cui lo sia ripete fino a quando cambia
If ritornodo = 100 Then Return
If ritornodo = 0 Then
ProgressDialogHide
tempo1.Enabled = False
lbl_fw.Text = " FW: " & jo.RunMethod("GetVersion", Null)
lbl_appversion.Text = " AppVersion:" & versionepackage
lbl_battery.Text = " Battery: " & jo.RunMethod("GetMemTot", Null) & "/" & jo.RunMethod("GetMemUsed", Null)
lbl_libversion.Text = " LibVersion: 1.12"
lbl_sn.Text = " SN: " & jo.RunMethod("GetSN", Null)
lbl_status.Text = " Status: " & jo.RunMethod("Ritorno", Null)
Log( jo.RunMethod("NomeFile", Null))
NomeFile = jo.RunMethod("NomeFile", Null)
Else
ProgressDialogHide
tempo1.Enabled = False
Dim errore As Int = Msgbox2("Errore: " & jo.RunMethod("Ritorno", Null), "ATTENZIONE", "OK","","",Null)
If errore = DialogResponse.POSITIVE Then
   .....
End If
End If
End Sub

#If JAVA
// RFT -->
import com.rft.irlib.IrHandler;
// <-- RFT
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import java.io.File;
import java.util.Calendar;
private int flgchiudi;
// RFT -->
private boolean taskRunning = false;
private Handler handler;
private boolean saveImmediately = true;
// <-- RFT
// VARIABLI DA RIPORTARE
String name;
String versione = "";
int GetMemoriaTotale = 0;
int GetBatteria = 0;
int GetMemUtente = 0;
String GetSerialNumber = "";
String Ritorno = "";
int RitornoDo = 100;
public void _onCreate() {
// RFT -->
BA.Log("Entro in Create");
handler = new MyHandler();
// <-- RFT
}
public void Coffe(int c) {
if (!taskRunning) {
BA.Log("Entro TaskRunning");
IrLibStart();
irLibThread irThr = new irLibThread(this, handler);
irThr.cmd(c);
irThr.start();
}
}
class irLibThread extends Thread {
private Handler handler;
private Context context;
private int irCmdReq = 0;
public irLibThread(Context context, Handler handler) {
this.handler = handler;
this.context = context;
}
public void cmd(int c) {
irCmdReq = c;
BA.Log("irThr.cmd(" + String.format("%X", irCmdReq) + ")");
}
public void run() {
BA.Log("Entro in run()");
Calendar c = Calendar.getInstance();
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (irCmdReq == 0)
return;
IrHandler irh = new IrHandler(context, handler);
irh.setAudioFilter(IrHandler.FILTER_TYPE_1);//per s4 mini
//String name;
File f = null;
int ret = 0;
boolean saveImmediately = true;
switch (irCmdReq) {
// Trasmissione IR
case 1:
BA.Log("Entro in saveimmediately");
if (saveImmediately) {
name = String.format("%d%02d%02d%02d%02d%02d.txt", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
f = new File(folder, name);
}
ret = irh.doDdcmp(0, 0, IrHandler.COM_IR | IrHandler.COM_BAUD_2400, f);
BA.Log("Valore RET doDdcmp: " + String.format("%X", ret));
RitornoDo = ret;
versione = irh.GetVersion();
BA.Log("Valore GetVersion: " + versione);
GetMemoriaTotale = irh.GetMemTot();
BA.Log("Valore GetMemTot: " + String.format("%X", GetMemoriaTotale));
GetBatteria = irh.GetBattery();
BA.Log("Valore GetBattery: " + String.format("%X", GetBatteria));
GetMemUtente = irh.GetMemUsed();
BA.Log("Valore GetMemUsed: " + String.format("%X", GetMemUtente));
GetSerialNumber = irh.GetSN();
BA.Log("Valore GetSN: " + GetSerialNumber);
break;
// Trasmissione RS232
case 2:
if (saveImmediately) {
name = String.format("%d%02d%02d%02d%02d%02d.txt", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));
f = new File(folder, name);
}
ret = irh.doDdcmp(0, 0, IrHandler.COM_RS, f);
BA.Log("Valore RET doDdcmp: " + String.format("%X", ret));
RitornoDo = ret;
versione = irh.GetVersion();
BA.Log("Valore GetVersion: " + versione);
GetMemoriaTotale = irh.GetMemTot();
BA.Log("Valore GetMemTot: " + String.format("%X", GetMemoriaTotale));
GetBatteria = irh.GetBattery();
BA.Log("Valore GetBattery: " + String.format("%X", GetBatteria));
GetMemUtente = irh.GetMemUsed();
BA.Log("Valore GetMemUsed: " + String.format("%X", GetMemUtente));
GetSerialNumber = irh.GetSN();
BA.Log("Valore GetSN: " + GetSerialNumber);
break;
}
notifyMessage(ret);
}
private void notifyMessage(int ret) {
Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putInt("EndThread", ret);
msg.setData(b);
handler.sendMessage(msg);
}
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
if (bundle.containsKey("EndThread")) {
int value = bundle.getInt("EndThread");
switch (value) {
case 0:
BA.Log("SUCCESS");
Ritorno = "SUCCESS";
break;
case IrHandler.ERR_SAMPLE_FREQ:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Audio Frequency");
Ritorno = "ERROR: " + String.format("%X", value) + " Audio Frequency";
break;
case IrHandler.ERR_NOHEADSET:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " No HeadSet");
Ritorno = "N.° " + String.format("%X", value) + " No HeadSet";
break;
case IrHandler.ERR_OUTPUT:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Audio output");
Ritorno = "N.° " + String.format("%X", value) + " Audio output";
break;
case IrHandler.ERR_NOIRMODULE:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " No Ir Module");
Ritorno = "N.° " + String.format("%X", value) + " No Ir Module";
break;
case IrHandler.ERR_RX_TIMEOUT:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Timeout");
Ritorno = "N.° " + String.format("%X", value) + " Rx Timeout";
break;
case IrHandler.ERR_RX_DATA:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Data");
Ritorno = "N.° " + String.format("%X", value) + " Rx Data";
break;
case IrHandler.ERR_RX_DATALEN:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Data Length");
Ritorno = "N.° " + String.format("%X", value) + " Rx Data Length";
break;
case IrHandler.ERR_RX_FRAME:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx Frame");
Ritorno = "N.° " + String.format("%X", value) + " Rx Frame";
break;
case IrHandler.ERR_RX_SYSTEM:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Rx system");
Ritorno = "N.° " + String.format("%X", value) + " Rx system";
break;
case IrHandler.ERR_FILE_NONE:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " No File");
Ritorno = "N.° " + String.format("%X", value) + " No File";
break;
case IrHandler.ERR_FILE_WRITE:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Write File");
Ritorno = "N.° " + String.format("%X", value) + " Write File";
break;
default:
int mask = value & 0xFF00;
switch (mask) {
case IrHandler.ERR_AUDIT_MASK:
if (value == IrHandler.ERR_AUDIT_MEMFULL)
// *** Aggiunto per visualizzare i Log
//BA.Log("ERROR: " + String.format("%X", value) + " Memory Full");
Ritorno = "N.° " + String.format("%X", value) + " Memory Full";
else
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " AUDIT FAILED");
//Ritorno = "N.° " + String.format("%X", value) + " AUDIT FAILED";
break;
case IrHandler.ERR_FILE_READ_MASK:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " Read File");
Ritorno = "N.° " + String.format("%X", value) + " Read File";
break;
case IrHandler.ERR_FILE_DEL_MASK:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " File SAVED, still in module?");
Ritorno = "N.° " + String.format("%X", value) + " File SAVED, still in module ?";
break;
case IrHandler.ERR_FW_MASK:
if (value == IrHandler.ERR_FW_UPTODATE)
// *** Aggiunto per visualizzare i Log
//BA.Log("FW UPTODATE: " + String.format("%X", value) + " ");
Ritorno = "N.° " + String.format("%X", value) + " ";
else
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR FW: " + String.format("%X", value) + " ");
Ritorno = "N.° " + String.format("%X", value) + " ";
break;
default:
// *** Aggiunto per visualizzare i Log
BA.Log("ERROR: " + String.format("%X", value) + " ");
Ritorno = "N.° " + String.format("%X", value) + " ";
break;
}
break;
}
IrLibStop();
}
}
}
private void IrLibStart() {
taskRunning = true;
}
private void IrLibStop() {
taskRunning = false;
}
public int RitornoDo(){
return RitornoDo;
}
public String GetVersion(){
return versione;
}
public int GetBattery() {
return GetBatteria;
}
public int GetMemTot() {
return GetMemoriaTotale;
}
public int GetMemUsed() {
return GetMemUtente;
}
public String GetSN() {
return GetSerialNumber;
}
public String Ritorno(){
return Ritorno;
}
public String NomeFile(){
return name;
}
#End If

Now all work if you tap first time top button. but if in same "session" i repeat tap, comeback same value that first. so i think that dont "destroy" and restart but have code in memory
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
The JavaObject instance is not related to your problem.

It is hard to say what exactly is the problem and what is the solution without debugging the Java code.
Already without debug is hard.
In a future version of B4A will be possible also debugging code of Java ?
 
Upvote 0
Top