Android Question [RESOLVED] Javaobject and Freeze

MarcoRome

Expert
Licensed User
Longtime User
Hi all.
I have a problem, when i call:
B4X:
Ritorno = Test.RunMethod("doFwUpdate", Array As Object(FormatoFile))

Freeze the App.
I know the DoEvents method is unfortunately not the case.
I also tried the library Thread and CallSubExt same result.
When i call the RunMethod the app Freeze until it ends (it takes 2-3 minutes).
I should have simply use a ProgressDialogShow2, but when i launch the RunMethod command the same freezes.

Any solution ?
Thank you
Marco
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Which method are you calling?
Hi Erel. This is a method of a library that interfaces with a hardware device. The libraries and methods work without problem. In this case (doFwUpdate ) update the hardware firmware, everything work without problem (but request between 1-3 minutes). When i run the method i haven't way to activate the progressdialog that alerts the user to wait.
What would be the right way to do this ??
Thank you
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Erel i tried with both ( Threading library by @agraham and CallsubExt by @Informatix ) unfortunately i have anyway the freeze.

As you see in this movie the progress freeze:



This is code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim thread1 As Thread
    Private Button1 As Button
    Dim Ritorno As Int
    Dim FormatoFile As Object
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    thread1.Initialise("thread1")
   
    '###MODIFICA Marco Rel. 1.21####
    Dim jofile As JavaObject
    jofile.InitializeContext
    FormatoFile = jofile.RunMethod("FW", Null)
    Log("File: " & FormatoFile)
    Cooffee.RunMethod("setAudioFilter", Array As Object(1))
       
   
End Sub

Sub Button1_Click

    ProgressDialogShow2("Wait...", False)
   
    timer1.Initialize("timer1", 1000)
    timer1.Enabled = True

End Sub


Sub ThreadSub1
    thread1.RunOnGuiThread("test", Null)
End Sub

Sub test
    Ritorno = Cooffee.RunMethod("doFwUpdate", Array As Object(FormatoFile))
    Log("Ritorno: " & Ritorno )   
End Sub



Sub Thread1_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    Msgbox(error, "Thread1 Ended")  
End Sub


Sub timer1_Tick
    thread1.Start(Me, "ThreadSub1", Null)
    timer1.Enabled = False
End Sub
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Here a example that you can try ( is a example dirty but just for try )

The effect that i have is the same ( freeze progressdialog )



B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim thread1 As Thread
    Dim lock1 As Lock
    Private Button1 As Button
    Private Label1 As Label
    Private Label2 As Label
   
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    thread1.Initialise("thread1")
       
End Sub

Sub Button1_Click

    ProgressDialogShow2("Wait...", False)
   
    timer1.Initialize("timer1", 1000)
    timer1.Enabled = True

End Sub


Sub ThreadSub1
    thread1.RunOnGuiThread("test1", Null)
End Sub

Sub test1
    Dim jofile As JavaObject
    jofile.InitializeContext
    jofile.RunMethod("test", Null)
End Sub



Sub Thread1_Ended(fail As Boolean, error As String) 'An error or Exception has occurred in the Thread
    Msgbox(error, "Thread1 Ended")
   
End Sub


Sub timer1_Tick
    thread1.Start(Me, "ThreadSub1", Null)
    timer1.Enabled = False
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub




#If JAVA
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;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;


public void test(){
StringBuilder t = new StringBuilder();
for(int l=0; l<=2000000; l++){
    t.append("Num:");
    t.append(l+"\n");
}
}

#End If
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
The whole point of using the Threading library is to run the code on a different thread, not the GUI thread.
Hi Erel. Can you write a little example ? ( I don't know in library i see only RunOnGuiThread )
Thank you very much
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi Erel, right this code work ( as usual ;) ) with example that i send in attachment #6 and THANK YOU FOR THIS, but i have this situation

I have this code:

B4X:
Sub aggiornafw_Click
.....
aggiorna_fw_thread.Start(Me, "FWTHREAD", Null )
.....
End Sub

Sub FWTHREAD
    'Verificare che esiste il file IR.FW nella cartella downaload se si lanciare la procedura 6
    Try 
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim handler As JavaObject
        handler.InitializeNewInstance("android.os.Handler", Null) ' <---- HERE I HAVE ERROR
        Dim ir As JavaObject
        ir.InitializeNewInstance("com.rft.irlib.IrHandler", Array(ctxt, handler))
       
        Ritorno = ir.RunMethod("doFwUpdate", Null)
        Log("VALORE DOPO: " & Ritorno)
       
    Catch
        Log("ECCEZIONE THREAD: " & LastException)
    End Try
   
End Sub

I have this Error:
(RuntimeException) java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

There are another solution or can i fix this code ?
Thank you again
Marco
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Maybe this is the way but anyway the method does not run

B4X:
Sub test_Click
.....
            Dim jofile As JavaObject
            jofile.InitializeContext
            FormatoFile = jofile.RunMethod("FW", Null)
            Log("File: " & FormatoFile)
         
            Cooffee.RunMethod("setAudioFilter", Array As Object(setta_modello))
         
         
            Dim ctxt As JavaObject
            ctxt.InitializeContext
            Dim handler As JavaObject
            handler.InitializeNewInstance("android.os.Handler", Null)
            Dim ir As JavaObject
            ir.InitializeNewInstance("com.rft.irlib.IrHandler", Array(ctxt, handler))
         

            aggiorna_fw_thread.Start(Me, "FWTHREAD", Array As Object(ir) )
.....
End Sub

Sub aggiorna_fw_thread_Ended(endedOK As Boolean, error As String) 'The thread has terminated. If endedOK is False error holds the reason for failure
    Log("Ok / Errore: " & endedOK & " - " & error)
    Log("FINISH VALUE RETURN = " & Ritorno)
End Sub

'Aggiorna il Firmware
Sub FWTHREAD( passa As JavaObject )

    Try
        Log("Entro dentro FW")
 
        Dim esegui As JavaObject
        esegui = passa     
        Ritorno = esegui.RunMethod("doFwUpdate", Array As Object(FormatoFile))
        Log("******VALORE DOPO: " & Ritorno)
    Catch
        Log("ECCEZIONE THREAD: " & LastException)
    End Try
End Sub

as i tap about test_Click return aggiorna_fw_thread_Ended:
Ok / Errore: false - Exception : argument 1 should have type anywheresoftware.b4j.object.JavaObject, got com.rft.irlib.IrHandler
Of course if i run without thread work but freeze for 1-2 minute the app
Any idea ?
Thank you
Marco
 
Last edited:
Upvote 0
Top