Crosswalk onJsBeforeUnload counterpart - android

Is there a method wherein I can override the onJsBeforeUnload in XWalkUIClient like this in WebChromeClient?
#Override
public boolean onJsBeforeUnload(WebView view, String url, String message, final JsResult result) {
new Thread(new Runnable() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
result.confirm();
}
});
}
}).start();
return true;
}
I'm trying to use Crosswalk's onJsPrompt and onJsAlert but I have no success in getting the result of onJsBeforeUnload.
Thanks!

Apparently I haven't tried onJavascriptModalDialog. I was able to imitate onJsbeforeUnload's behavior using
#Override
public boolean onJavascriptModalDialog(XWalkView view, JavascriptMessageType type, String url, String message, String defaultValue, final XWalkJavascriptResult result) {
new Thread(new Runnable() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
result.confirm();
}
});
}
}).start();
return true;
}

Related

Activity.runOnUiThread(Runnable action) only updates view once

In my fragment, I have an AlertDialog and a Bluetooth connection manager. I want to update the AlertDialog with the new states of the Bluetooth connection process, so I used the runOnUiThread(...) method:
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
interactor = new BluetoothInteractor(getActivity(), new OnBluetoothStatusChangedListener() {
#Override
public void OnConnectionStopped() {
alertDialog.setMessage("Disconnected.");
}
#Override
public void OnConnectionStopping() {
alertDialog.setMessage("Stopping connection...");
}
#Override
public void OnConnectionStarting() {
alertDialog.setMessage("Connecting to device...");
}
#Override
public void OnConnectionStarted() {
alertDialog.setMessage("Streaming data...");
}
});
}
});
The first time I update the AlertDialog message (OnConnectionStarting event) everything works fine, but the second time I got android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
What could be happening here?
Replace with
interactor = new BluetoothInteractor(getActivity(), new OnBluetoothStatusChangedListener() {
#Override
public void OnConnectionStopped() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Disconnected.");
}
});
}
#Override
public void OnConnectionStopping() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Stopping connection...");
}
});
}
#Override
public void OnConnectionStarting() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Connecting to device...");
}
});
}
#Override
public void OnConnectionStarted() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Streaming data...");
}
});
}
});

CountDownTimer incorrectly sets text to more view than asked for

On the CountDownTimer onFinish method, I want to set 2 string to 2 different views like this
public void onFinish() {
setText(timer, getString(R.string.times_up));
setText(speedTextView, speedValue);
setColorTimer(timer, Color.RED);
runOnUiThread(new Runnable() {
public void run() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
startActivity(intent);
}
}, 3000);
}
});
}
However, when the timer finished, the R.string.times_up value is casted to both timer and speedTextView view. I changed the order of the 2 lines and in that case, the speedValue is casted on both view, although they should be 2 different method calls.
setText method is a modified one to run them on the main thread instead of the background thread like this
private void setText(final TextView text, String value) {
speedValueToSetText = value;
runOnUiThread(new Runnable() {
#Override
public void run() {
text.setText(speedValueToSetText);
}
});
}
How can I resolve this? Shouldn't the setText methods be independent to each other?
The global variable speedValueToSetText cause the error.Remove this line speedValueToSetText = value;.The setText should be
private void setText(final TextView text, String value) {
runOnUiThread(new Runnable() {
#Override
public void run() {
text.setText(value);
}
});
}
or
private void setText(final TextView text, String value) {
speedValueToSetText = value;
final String resultValue = value;
runOnUiThread(new Runnable() {
#Override
public void run() {
text.setText(resultValue);
}
});
}

How to restart listening again after getting error 8 in speechrecognizer android?

I have tried this code,but its not working,after getting error 8,its not listening again,what should i do?
#Override
public void listen() {
myHandler.post(new Runnable() {
#Override
public void run() {
sr.startListening(i); // here i is Intent,SpeechRecognizer sr;
Log.d("message","start listening");
}
});
}
#Override
public void onError(int error) {
Log.d("message","error occurred! "+error);
if(error==8) {
sr.destroy();
myHandler.post(new Runnable() {
#Override
public void run() {
sr.startListening(i);
Log.d("message","again start listening");
}
});
}
}

How to stop the thread in Android?

I have a thread in my callback function as follows:
#Override
public void onConnectError(final BluetoothDevice device, String message) {
Log.d("TAG","Trying again in 3 sec.");
runOnUiThread(new Runnable() {
#Override
public void run() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something
}
}, 2000);
}
});
}
I will to close the the above thread when I press the back button or onDestroy. How can I do it. Thank you
#Override
public void onBackPressed() {
// Close or distroy the thread
}
#Override
public void onDestroy() {
// Close or distroy the thread
}
Please do this like
private Handler handler;
private Runnable runnable;
#Override
public void onConnectError(final BluetoothDevice device, String message) {
Log.d("TAG","Trying again in 3 sec.");
runOnUiThread(new Runnable() {
#Override
public void run() {
handler = new Handler();
runnable = new Runnable() {
#Override
public void run() {
//Do something
}
};
handler.postDelayed(runnable, 2000);
}
});
}
and
#Override
public void onBackPressed() {
if (handler != null && runnable != null) {
handler.removeCallbacks(runnable);
}
}
and same in onDestroy();
I'm mostly use thread in this way.See its independent in activity
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
holdConnectionHandler.sendEmptyMessage(0);
}
Handler holdConnectionHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
// do some work
holdConnectionHandler.sendEmptyMessageDelayed(0, 10 * 1000);
}
};
#Override
public void onDestroy() {
super.onDestroy();
holdConnectionHandler.removeCallbacksAndMessages(null);
// or
holdConnectionHandler.removeMessages(0);
}
}
Thanks hope this will help you

Starting and stopping progressbar on UI thread from class

Is there a slicker/simpler way of doing the following? I have a method in a class that shows a progressbar while a thread runs. This code works but it just seems a little overly clunky having 3 steps.
private void pause() {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
//do stuff
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
}
This does not show a progress bar while a thread runs.
Your thread can run 10 seconds but the visibility of the ProgressBar will only blink if you can see it at all.
Instead, you should hide only once the thread has completed, so this would be correct:
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
//do stuff
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
For a "slicker" way, you could use an AsyncTask which was created for this very task. Example:
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Guaranteed to run on the UI thread
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params) {
// Do stuff
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// Guaranteed to run on the UI thread
mProgressBar.setVisibility(View.GONE);
}
}.execute();
Here you can use the handlers concept to communicate with UI Thread.
I.e,
Handler handler=new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 1:
mProgressBar.setVisibility(View.VISIBLE);
break;
case 2:
mProgressBar.setVisibility(View.GONE);
break;
}
}
}
new Thread(new Runnable() {
#Override
public void run() {
Message processStart = handler.obtainMessage(1);
processStart.sendToTarget();
try {
//do stuff
} catch (InterruptedException e) {
e.printStackTrace();
}
Message processStart = handler.obtainMessage(2);
processStart.sendToTarget();
}
}).start();
I hope this one will helps you :)

Categories

Resources