Android Question Inline Java

mlc

Active Member
Licensed User
Hello,
Due to problems with RingtoneManagerWrapper.class and amazon, and to not have 2 Phone libraries (Phone2 without this class).
I'm trying to replace PhoneWakeState.class, from Phone library, with this code:

B4X:
Sub Process_Globals
    Private nm As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)

    If FirstTime Then
        nm.InitializeContext
    End If
    nm.RunMethod("wake",Null)
End Sub

#If JAVA
import android.view.WindowManager;
import android.view.View;
import android.view.Display;

public void wake() {

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    Display display = getWindowManager().getDefaultDisplay();
    View view = getWindow().getDecorView().findViewById(android.R.id.content);
    if (view != null){      
       view.setKeepScreenOn(true);
    }
}

#End If
It works well, and the screen stays on.

If I put the app in the background, the screen turns off and when I return to bring to the fore, the screen stays on. Perfect.

But if I rotate the device then no longer useful, because when rotate, the screen turns off.

This is the solution I found:
I declare javaObject in Globals and initialize in activity_create

B4X:
Sub Globals
    Dim nm As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)

    If FirstTime Then

    End If

    nm.InitializeContext
    nm.RunMethod("wake",Null)

End Sub
Works perfectly, even when i rotate the device, but is correct?

Thanks
 

thedesolatesoul

Expert
Licensed User
The documentation here is out of date: http://www.b4x.com/android/help/javaobject.html
EDIT: Search for InitializeContext doesnt return any useful results: http://www.b4x.com/android/forum/pages/results/?query=InitializeContext&page=1&prefix=0
EDIT2: Neither does a search for JavaObject return any useful results mentioning this method.

If InitializeContext is taking the activity context then it is correct, you dont need to ensure its the first time. Also its not going to hurt doing it everytime the activity is created. I cant see why it needs to be wrapped in FirstTime.
 
  • Like
Reactions: mlc
Top