I am working on an jQuery Mobile application which uses HTML pages for UI
There are some JS function through which i call some native functions. There are some popups and dialogs with input fields, but when i open some dialog or popup in html the logCat gives me this warning
W/webview_proxy(8104): java.lang.Throwable: Warning: A WebView method was called on thread
'WebViewCoreThread'. All WebView methods must be called on the UI thread. Future versions of
WebView may not support use on other threads.
and while entering values in these input fields page and keyboard starts blinking.
that's how i am calling theses native functions
JS Function
function SettingMethod()
{
activity.setSettings();
}
Android Method
#JavascriptInterface
public void setSettings() {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
final String webServiceURL = settings.getString("URL", "");
final String InputFolder = settings.getString("INPUTFOLDER", "");
final String OutPutFolder = settings.getString("OUTPUTFOLDER", "");
index.loadUrl("javascript:setSettings('" + webServiceURL + "','"
+ InputFolder + "','" + OutPutFolder + "')");
}
how can i remove this lag ?
Problem solved by using
runOnUiThread(new Runnable() {
public void run() {
index.loadUrl("javascript:setSettings('" + webServiceURL + "','"
+ InputFolder + "','" + OutPutFolder + "')");
}
});
Related
I'm trying to finish the 'backbone' of my app in the next 3 weeks, however, one of the few obstacles I stutter at is saving data. I've had a look at saving data internally, but there is limited tutorials from what I can find of reading and writing multiple lines to files in the apps cache directory.
Basically what I'm trying to do is save the values stored inside a fragment. This fragment resets all its values when the user clicks a button and changes text to match a page number. (A number of duplicates that contain various values.) I would do multiple fragments, however, thought it would be beneficial to use just one fragment to minimize storage space needed.
I've only got round to writing to the files, and created two methods to manage this which are then called on the click of a button. One creates these files and the other writes to them. Unfortunately I'm inexperienced using adb and could only find that the files are created, but don't know if they are being correctly written to. Is there any chance someone could review this and possibly assist with re-reading the files? Help is much appreciated.
The two methods (Warning: A great number of lines ahead):
public void createEmptyFiles() {
try {
outputTempExerciseFileE1 = File.createTempFile("temp_exercise_1",
".txt", outputTempExerciseDir);
outputTempExerciseFileE2 = File.createTempFile("temp_exercise_2",
".txt", outputTempExerciseDir);
outputTempExerciseFileE3 = File.createTempFile("temp_exercise_3",
".txt", outputTempExerciseDir);
} catch (IOException e) {
e.printStackTrace();
Log.w("rscReporter", "Encountered an error when creating empty files!");
}
}
public void writeTemporaryFiles() {
try {
if (counterAnotherExercise == 1) {
writerTemp = new FileWriter(outputTempExerciseFileE1);
writerTemp
.write(editTextExerciseName.getText().toString() + "\n"
+ counterNoSets + "\n" + counterRepsPerSet
+ "\n" + counterMeanRepTime + "\n"
+ counterMeanRepTimeRefined + "\n"
+ counterSetInterval);
writerTemp.close();
} else if (counterAnotherExercise == 2) {
writerTemp = new FileWriter(outputTempExerciseFileE2);
writerTemp
.write(editTextExerciseName.getText().toString() + "\n"
+ counterNoSets + "\n" + counterRepsPerSet
+ "\n" + counterMeanRepTime + "\n"
+ counterMeanRepTimeRefined + "\n"
+ counterSetInterval);
writerTemp.close();
} else if (counterAnotherExercise == 3) {
writerTemp = new FileWriter(outputTempExerciseFileE3);
writerTemp
.write(editTextExerciseName.getText().toString() + "\n"
+ counterNoSets + "\n" + counterRepsPerSet
+ "\n" + counterMeanRepTime + "\n"
+ counterMeanRepTimeRefined + "\n"
+ counterSetInterval);
writerTemp.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Any of the text files should look like:
editTextExerciseName
counterNoSets
counterRepsPerSet
counterMeanRepTime
counterMeanRepTimeRefined
counterSetInterval
Where the two methods are called:
// In a switch statement as there are around 15 buttons
case R.id.button_another_exercise_foreground:
// Increases page number in fragment
counterAnotherExercise++;
// This then checks the page number and changes text
checkPageNo();
// Writing to files is called, files were created in onCreateView()
writeTemporaryFiles();
// Resets all the counters, giving the imitation it is a completely new fragment
counterReset();
// default array exercise is then set to the page number which is then displayed as title
// For example: Exercise 1, Exercise 2, Exercise 3...
textViewExerciseTitle.setText(defaultArrayExercise);
break;
I only know the basics of Java and Android, for myself this is ambitious, however, you gotta learn somewhere! Additional suggestion for saving values are welcomed.
You don't really need files as you are only writing and then reading a handful of fixed data. Use SharedPreferences like this:
to write:
PreferenceManager.getDefaultSharedPreferences(YourActivity.this).edit().putString("editTextExerciseName", "my exercise").commit();
to read:|
PreferenceManager.getDefaultSharedPreferences(YourActivity.this).getString("editTextExerciseName");
I'm developing a game with Unity3D for Android. I'm using Facebook App for sharing game score in Facebook. But I receive a error message ;
My codes are here ;
//facebook share start
public static void share(string link, string pictureLink, string name,string caption, string description, string redirectUri){
Application.OpenURL(ShareUrl +
"?app_id=" + AppId +
"&link=" + WWW.EscapeURL( link )+
"&picture=" + WWW.EscapeURL(pictureLink) +
"&name=" + WWW.EscapeURL(name) +
"&caption=" + WWW.EscapeURL(caption) +
"&description=" + WWW.EscapeURL(description) +
"&redirect_uri=" + WWW.EscapeURL(redirectUri));
}//facebook share end
if(GUI.Button(new Rect(Screen.width/2,(Screen.height/2-30),80,20), "Share")){
print("Share");
share("http://www.halilcosgun.com","https://24.media.tumblr.com/avatar_ce3a5b939737_64.png","Facebook skor paylaşma denemesi " + score,"Skor da mı paylaşmıyah?","oyun çok yakında!","http://facebook.com");
}
I tried to write many adresses (many many configurations) intead of "http://facebook.com", but I can't find the true one.
If you know the solution, can you halp me please?
I would like to thank you for your interest.
there must me an invalid parameter that you have provided . try to find it out and make the change it will solve your issue
try the share with first s in caps example
Share("http://www.halilcosgun.com","https://24.media.tumblr.com/avatar_ce3a5b939737_64.png","Facebook skor paylaşma denemesi " + score,"Skor da mı paylaşmıyah?","oyun çok yakında!","http://facebook.com");
}
I am working on an Android application where I am using webview to display GIF files downloaded from internet, it is a picture viewer do user can click next and previous to view the next and previous gif files, the issue I am facing is that when user clicks next, for one split second it displays the older html and then updates the webview, I have tried disabling the webview cache but it is of no use, relevant part of the code is.
String base = Common.CACHE_IMAGES_PATH.getPath();
String imagePath = "file://" + base + "/" + forImage;
String html = "<html style=\"height:100%\"><head><META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\"><META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\"> </head><body style=\"padding:0x;margin:0px; background-color:#999;height:100%\"><img id=\""
+ System.currentTimeMillis()
+ "\" align=\"middle\" style=\"width:100%;height:auto;\" src=\""
+ imagePath + "\"></body></html>";
webView.loadDataWithBaseURL(Common.FULL_IMAGE_BASE_URL + forImage
+ "?fix=" + System.currentTimeMillis(), html, "text/html",
"utf-8", Common.FULL_IMAGE_BASE_URL + forImage + "?fix="
+ System.currentTimeMillis());
Here I am reading the image from sdcard and displaying it in webview, this code is executed everytime user click next or previous.
I want to know:-
In my project i am using html content and displaying them in android webview. and i am using eclipse ide. this is tiny code.
"<form name =\"frm\">"+
"<input type=\"checkbox\" name =\"First\" value =\"xyz\">xyz<br>"+
"<input type=\"checkbox\" name =\"First\" value =\"abc\">abc<br>"+
"</form>"
my question is how can i get check box state .its checked or unchecked.
or how can i catch the state in my java code.
UPD:-
public String html = "<form name =\"frm\">"+
"<input type=\"checkbox\" name =\"First\" value =\"xyz\">as<br>"+
"<input type=\"checkbox\" name =\"Second\" value =\"zyx\">as<br>"+
"<input type =\"button\" onclick =\"callDoSomething()\"><br>"+
"</form>" +
"<script type=\"text/javascript\">"+
"function callDoSomething() {"+
" var theName = document.frm.First.value;"+
"alert('theName ')"+
"}"+
"</script>";
First, both of your checkboxes are named "First", you should probably name second one "Second". If you want to search checkboxes by value - just add a simple js for loop.
Assuming you want to get the results from your Android code (as opposed to JS event like clicking a button), here's how you get Java boolean value for you checkbox by name:
// assuming your activity is MyActivity, target checkbox name
// is in the targetCheckboxName var and webView has the document
// loaded already
Object jsi = new Object() {
#JavascriptInterface
public String reportCheckboxState(String name, boolean isChecked) {
new AlertDialog.Builder(MyActivity.this).setMessage(name + " is " +
isChecked).create().show();
}
};
webView.addJavascriptInterface(jsi, "injection");
webView.loadUrl(
"javascript:injection.reportCheckboxState(frm." + targetCheckboxName +
".name, frm." + targetCheckboxName + ".checked);"
);
But really, it's a very simple trick. Judging by comments to the question, you should probably read up on JavaScript and WebView.addJavaScriptInterface()
I have code like this:
MyLog.d("TAG", "debug string " + aVariable + " more debug string =" + anotherVariable);
And MyLog class is like
public void d(String tag, String message) {
private static final boolean DEBUG = true;
if (DEBUG) {
Log.d(tag, message);
}
}
My question is if I set DEBUG to false, will android java compiler smartly detect that this line of code
MyLog.d("TAG", "debug string " + aVariable + " more debug string =" + anotherVariable);
does nothing
and it won't create temporary string objects for "debug string " + aVariable + " more debug string =" + anotherVariable
You're doing the string concatenation before anything related to the DEBUG matters: I doubt that would be optimized out by ProGuard, although the call to Log.d inside MyLog.d would disappear.
If you check the bytecode, it'd be worth reporting back; I'm curious how far ProGuard will follow a call chain to detect dead code. I'd be surprised if the string concatenation went away.
You also can't declare a variable private like that inside a method.