In the onCreate() method, is there a way to reload URL? I am trying to reload URL every minute
public class AAPActivity extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
loadUrl("file:///android_asset/www/index.html");
task = new TimerTask(){
public void run() {
handler.post(new Runnable() {
public void run() {
// Here instead of loadURL, is there a way to reload?
loadUrl("file:///android_asset/www/index.html");
}
});
}};
t.schedule(task, 0, 10000);
}
}
Thank you
As it is a Phonegap App you will probably use javascript. I found this:
<script type = "text/Javascript">
function refresh() {
window.location.reload(true);
tim = setTimeout("refresh()",60000); // refresh every minute
}
</script>
on http://www.codingforums.com/archive/index.php/t-157966.html . Maybe that helps ;)
Related
I have created a method and i want to call it after every 10 seconds with the help of Thread in Handler. My code is..
public void saveDataToServer(){
//do logic here
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(
new Runnable() {
#Override
public void run() {
saveDataToServer();
}
},
10000);
}
There is no error in code, But unfortunately not running. Can anybody please tell what i am doing wrong.. Thanks in advance.
You should call
new Handler().postDelayed(this,10000);
in Run like
Runnable r=new Runnable() {
#Override
public void run() {
saveDataToServer();
new Handler().postDelayed(this,10000);
}
};
new Handler().postDelayed(r,10000);
I am trying to change the URL in webView from within a timer class, and as soon as the timer fires, the application crashes:
public class MainActivity extends Activity {
public static WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
Timer xxmyTimer = new Timer();
xxmyTimer.schedule(new TimerTask() {
public void run() {
mWebView.loadUrl("http://somesite.com/");
}
},5000,5000);
}
I've been trying to solve this all day, and no matter what I try, the result is always the same - crash & burn !
Would really appreciate some help with this.
Application is crashing because you are trying access or update UI element(WebView) from the TimerTask which run on separate non UI Thread.
So in current code just add runOnUiThread for accessing WeView from TimerTask Thread as:
xxmyTimer.schedule(new TimerTask() {
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mWebView.loadUrl("http://somesite.com/");
}
});
}
},5000,5000);
and other option is use Handler.postDelayed instead of TimerTask.
I get this error. not on any device, just on some.
This is all the report:
java.lang.NullPointerException
at eu.innovaapps.tpark.ro.MainSms$4$1$1.run(MainSms.java:444)
at java.util.Timer$TimerImpl.run(Timer.java:284)
and it comes from:
buton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
buton.setEnabled(true);
}
});
}
}, 5000);
When i press the button it gets disabled and after 5 seconds it gets enabled again.
Line 444 is getActivity().runOnUiThread(new Runnable() {.
I'd appreciate alot if you could help me solve this error.
EDIT:
I got the same problem as here:
Scheduled Task in Fragment returns getActivity as null
If the button in disabled, and i press the back button of the phone, i get this error which sends me at this part of the code.
Is there a way i could stop the timer if the back button is pressed?
Can you please tell what is the line number 444 in your code ?
If line 444 is buton.setEnabled(true); then can you modify the code to add Button buton = (Button) findViewById(R.id. just before buton.setEnabled(true); and then try out the same code.
EDIT
Dear Victor,
I have read your EDIT... I used to get such exception... To go around this, what I have done is that I declared and static variable in my Activity class...
public static sActivity = null;
and then in onCreate method of Activity I assigned the current activity to it...
sActivity = this;
and then used it in the code instead of getActivity()...
So for e.g. my Activity name is myActivity then your code in that case could changed to...
#Override
public void run() {
getActivity().runOnUiThread(new Runnable()
#Override
public void run() {
if (myActivity.sActivity != null) {
myActivity.sActivity.runOnUiThread(new Runnable()
.
.
.
}
Let me know if you find any other better solution...
Timer notificationtask = new Timer();
notificationtask.schedule(new TimerTask()
{
#Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
buton.setEnabled(true);
} catch (Exception e) {
}
}
});
}
}, 1000, 1000);
This question already has answers here:
Updating Android UI using threads
(4 answers)
Android toast message from a separate thread class
(1 answer)
Closed 9 years ago.
I'm having some trouble trying to update automaticaly a view in my android activity.
The application display some message like a chat. Im using a ListView to put the message with a ArrayAdapter.
I use this metod to update the ListView
public void loadMessages() {
ArrayList<String> messages = this.dbHelper.getMessages();
conversationArrayAdapter.clear();
conversationArrayAdapter.addAll(messages);
conversationArrayAdapter.notifyDataSetChanged();
}
My idea is to put a thread that call that metod, but when i try to do this i have the following error.
Only the original thread that created a view hierarchy can touch its view.
because you are trying to access or update UI elements from Thread . to avoid this error you will meed to use runOnUiThread for updating UI from Thread as :
Your_Current_Activity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// Update UI here
loadMessages();
}
});
and second solution is use AsyncTask instead of thread
Use this code.
public class MainActivity extends Activity
{
private Timer autoUpdate;
#Override
public void onResume()
{
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
public void run()
{
updateScore();
}
});
}
}, 0, 5000); // updates each 5 seconds
}
#Override
public void onPause()
{
autoUpdate.cancel();
super.onPause();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
// initialize view layout
super.onCreate(savedInstanceState);
setContentView(R.layout.cleanermain);
super.onResume();
}
private void updateScore()
{
// decide output
// update cricket score
}
}
UI should be updated only from the UI (Main) thread.
Here is a solution using AsyncTask.
public void asyncCallWithSchedule() {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
new SearchAsync().execute(txtSearch.getText().toString());
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 2000);
}
}
AsyncTask class:
private class SearchAsync extends
AsyncTask < String, Object, List < Users >> {
#Override
protected List < Users > doInBackground(String...params) {
// Call DB here
return null;
}
#Override
protected void onPostExecute(List < Users > result) {
super.onPostExecute(result);
// Update UI here
}
}
Simple:
public void loadMessages() {
ArrayList<String> messages = this.dbHelper.getMessages();
conversationArrayAdapter.clear();
conversationArrayAdapter.addAll(messages);
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
conversationArrayAdapter.notifyDataSetChanged();
}
});
}
I have one activity. OnCreate the activity gets the source (html) of a web page to a string and presents the result (after parsing it a bit) in a textview.
I would like the activity to reload/refresh periodically to always present the latest information.
What is the best solution for this?
First of all... separate the updating logic from your onCreate method. So, for instance, you can create an updateHTML().
Then, you can use a Timer in order to update the page periodically:
public class YourActivity extends Activity {
private Timer autoUpdate;
public void onCreate(Bundle b){
super.onCreate(b);
// whatever you have here
}
#Override
public void onResume() {
super.onResume();
autoUpdate = new Timer();
autoUpdate.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
updateHTML();
}
});
}
}, 0, 40000); // updates each 40 secs
}
private void updateHTML(){
// your logic here
}
#Override
public void onPause() {
autoUpdate.cancel();
super.onPause();
}
}
Notice that I'm canceling the updating task on onPause, and that in this case the updateHTML method is executed each 40 secs (40000 milliseconds). Also, make sure you import these two classes: java.util.Timer and java.util.TimerTask.