This thread was migrated from an old forum. It may contain information that are no longer valid. For further assistance, please post a new question or open a support ticket from the Customer Support portal. |
This thread was migrated from an old forum. It may contain information that are no longer valid. For further assistance, please post a new question or open a support ticket from the Customer Support portal. |
This is an informative post from Android framework team
1. Introduction
Whenever an Android application boots up, a process is created specific to application.
a) UI/Main thread
The application process is created and executes on Main/UI thread whenever any of the components like Activities (UI), broadcast receiver, content providers & services are created and executed.
Whenever a UI/Activity is shown to user, UI/Main thread is used for displaying UI and processing user input events.
b) VM (Virtual Machine) thread
When a Kony Android app is launched, additionally a VM thread is created for loading and execution of all the Java Script code/business logic which is different from UI thread, to leave UI responsive to user.
c) Android Restriction:
In Android, all UI operations are to be performed only on UI (Main) thread.
Android framework throws exception when we try to access/update any view or do UI operations from non UI thread.
Example use case:
We wanted to query the UserAgent String of current Android device’s Web View, in onClick of a JS Button.
Problem:
If an android native webview instance is already created in application in UI thread prior to button click and as onClick callback JS function executes in a separate VM thread, we cannot directly access and use webview methods from onClick JS callback function.
Asynchronous Solution:
· Post a runnable to execute UI operations on UI thread when FFI is invoked from a JS function
Ex: Button click callback in JS
· You can register a Java Script callback Function of type “com.konylabs.vm.Function” with FFI function.
Refer: http://docs.kony.com/konylibrary/visualizer/visualizer_user_guide/Content/Android_Custom_Widget.htm
http://docs.kony.com/6_5/konylibrary/studio/studio_user_guide/Content/Foreign_Function_Interface_JS.htm#function
· From FFI , Invoke the registered Java Script callback asynchronously using
callbackJS.executeAsync(new Object[] {infotable})
to notify JS layer of completion of the desired action or to send return values.
Synchronous Solution:
· Post a runnable to execute UI operations on UI thread when FFI is invoked from a JS function.
Ex: Button click callback in JS
· As button click callback executes on Non UI/VM thread, Make the Non UI thread wait until the scheduled runnable is complete using Java thread’s wait notify mechanism.
· Return values if any synchronously
Please find the below snippet to access web view setting from non UI thread ex: from a JS button click.
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.webkit.WebView;
import com.konylabs.android.KonyMain;
public class UserAgent
{
private static Context mContext;
static Runnable mEvalRunnable = null;
public static String getUserAgent()
{
mContext = KonyMain.getAppContext();
final String[] userAgent = new String[1];
mEvalRunnable = new Runnable() {
public void run() {
WebView view = new WebView(mContext);
userAgent[0] = view.getSettings().getUserAgentString();
System.out.println("userAgent=======>>>>" + userAgent[0]);
synchronized (mEvalRunnable) {
mEvalRunnable.notify();
}
}
};
Handler uiThreadHandler = new Handler(Looper.getMainLooper());
uiThreadHandler.post(mEvalRunnable);
synchronized (mEvalRunnable) {
try {
mEvalRunnable.wait();
} catch (InterruptedException e) {
}
}
return userAgent[0];
}
}