I'm a complete newb. I'm trying to port this simple program I made in class to an Android app. It's just suppose to print out " I love Computer Science!!" a certain amount. But I'm suspecting the while loop is causing my program to automatically force close. I've searched and found that I need to make a thread, but I can't find a similar situation.
Here's my onCreate method:
public void onClick(View v)
{
int number = 1;
int printed = 0;
int limit = 0;
int count = 0;
String countString;
second.setText("How much do you love Computer Science?");
countString = howMuchEditText.getText().toString();
count = Integer.valueOf(countString);
printed = count;
while ((count -1) >= limit)
{
third.setText(+number + " I love Computer Science!!");
count -= 1;
number ++;
}
fourth.setText("This printed " +printed + " times.");
}
});
}
}
Could anyone help me fix my force close?
Android only allows your application's handlers five seconds to execute before you'll get an "App not responding" message. Depending on the loop count, your onClick method could easily exceed that limit.
If you want to display an automatically updating counter, you will need to launch an asynchronous task that executes in the background.
Here is a quick tutorial on AsyncTask, but you will need to learn more about the fundamentals of the Android platform before it will make sense. Good luck!
To add to the previous answer, AsyncTask is not quite the answer either. This is because your modifications of the TextView cannot be done on a background thread—UI calls are only allowed on the main thread. To schedule periodic tasks on the UI thread, create yourself a Handler object, which has various post methods you can call to run tasks after a specified interval, or as soon as possible.
Related
I am updating a database (SQLite) with a lot of data, which takes several minutes. I am doing this on a Thread. At the same time I want to show the progression of the updates (25%, 50%, etc.) through a Toast. Since I am within a thread, I need to use the runOnUiThread() function to run the Toast. Like this:
getActivity().runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getActivity(),"Updating the database...", Toast.LENGTH_SHORT).show();
}
});
It was working fine when the workload was not to big. Now that it is, no toast is being displayed at all.
I have been looking for ways to set a very high priority to the toast's thread, without success. Maybe I can bypass using a thread for the toast in the first place?
Thank you for the insights!
EDIT: In fact, the toast is working, but is displayed after all the work has been done. I want to notify the percentage of the complete update to the user, so I need to have the toast displayed during the update and not after.
I tried removing the workload (no database update, only a Log.d inside the for loop of things to add to the database). And the toast are displayed after the loop is finished although the runOnUiThread() method is called inside the loop.
EDIT 2: I managed to do what I wanted after cleaning up the code and starting fresh. I posted the code I used as the answer below.
After having cleaned up the initial thread, I managed to have something working. I post the code here since it can be used as a template to do a specific task:
Run a initial thread that does tasks periodically for a given number of times (here collect some data). After this given number of times, the collected data is pushed to a database and the user is notified of the advancement of the process (which takes a long time).
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
long t0 = System.currentTimeMillis();
#Override
public void run() {
if(System.currentTimeMillis() - t0 > EXPERIMENT_DURATION ){
processData(dataArrayList); // custom tasks - see below
cancel();
}else {
dataArrayList = collectData(); // custom tasks
}
}
}, 0, INTERVAL);
With:
void processData(Arraylist<Data> dataArrayList){
for(Data data : dataArrayList){
// show progression to the user
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
int percent = Math.round(((float)dataArrayList.indexOf(data))/((float)dataArrayList.size())*100)
Toast.makeText(getActivity(),"Update: " + Integer.toString(percent) + "%", Toast.LENGTH_LONG).show();
}
});
updateDataBase(data);
}
}
I highly recommend you use an AsyncTask.
AsyncTask performs in another working Thread, but it offers a method that is executed in the UI Thread, publishProgress(String ...). that you then customize by overwriting onProgressUpdate(String ...).
EDIT: As read in the comments, AsyncTask is not that good of an option because:
All AsyncTask share a Thread (they don't run on workers as I expected)
For long running Tasks, all other AsyncTasks (system's and your's) will be on hold
This includes AsyncTasks from libraries
Trying to figure out how to do this, basically my App requires a button to be hit and hit multiple times, it counts how many times you hit it and if you don't hit one within a certain space of time it will display a message.
I just can't figure out how to get the 'If button isn't pressed within 'x' seconds then...' part, I've tried if(imagebutton1.isPressed()) statement but it checks it instantly when the actvity starts, I just want it to check 'X' amount of seconds after the button was last pressed.
ANy help is appreciated thanks.
In your case you would need to record the last time the button was pressed
Then add a updated while statement
In c++
Int presses;
Int timelimit; //the seconds between each press (you can use a chronometer but this is simpler but less accurate (and no decimals)
Int lastpressed; //need chronometer for more accuracy or decimals)
Int ammountpassed; //time since you pressed it
If(imagebutton1.isPressed())
{
Bool Started = Yes;
Presses++;
While(!imagebutton1.isPressed()&&ammountpassed<TimeLimit)
{
Ammountpassed++;
};
};
If (ammountpassed>=timelimit)
{
If (presses>=highscore)
{
DisplayMsg " Number of presses" presses; "! New highscore!";
};
Else(presses<highscore)
{
DisplayMsg "not fast enough! Number of presses" presses; "!" };
};
};
You will have to tweak it a bit to fit your needs ("displaymsg" I for think is the actual function so you might have to change that but there's the logic :)
I recommend hand typing this as I belive I may have made a few error but nothing adding a semi colon or 2 won't fix ;)
Hope it helps :) Good Luck :)
Every time the user hits the button you can post a message on the handler's queue with your text message to be displayed and the appropriate delay time (and remove previous messages). Therefore if the delayed time exceeds without any press the thread will execute the handler's message. Lets say you want to post to the main handler a message to be executed in delay number of milliseconds, then in your activity you would need to hold the reference to the handler and create a Runnable where the necessary text message will be displayed:
Handler mainHandler = new Handler(getMainLooper());
Runnable runnable = new Runnable(...);
In your OnClickListener of the button you would need to execute only:
#Override
public void onClick(View v) {
mainHandler.removeCallbacksAndMessages(null);
mainHandler.postDelayed(runnable, delay);
}
Since Webworkers is only implemented from Android 4.4 onwards, is it possible to have a wrapper in the application code that provides this functionality to the contained WebView?
An example on how to solve this would really help.
Thanks,
Rajath
I guess you are talking about running a javascript code block in the background, i.e. different thread. Had tried doing that using RhinoJS on Android. Tested on Android 2.2 and above
https://github.com/devthon/SilentJSAndroid
The main features are
Execute Javascript code without browser context
Execute Javascript code from a script file
Load other JS files in the same context
Execute a method in the background thread and return a result
Execute a Object.Method() call
Execute a prototype method call
Run long running script in the background after app is closed.
May not be a full fledged Web worker as such, since it doesn't have API to check the status in between. But that can be still added to the interface I believe.
If this is the direction you are looking for, I can explain more on how it is done.
How much of the Worker spec do you need to implement, and how flexible does the implementation need to be? You could probably get basic functionality up and running using a JavaScript interface[1] and spawning threads natively from Java. However this will get complex quite quickly.
Perhaps if you can describe what you are using workers for I might be able to offer a different/better suggestion.
[1] http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
--
Adding some pseudo code
In JavaScript spawn a Java worker thread:
var worker_id = window.Android.spawnWorker();
In JavaScript, run a task on that worker:
var task_id = window.Android.doAdditionOnWorker(2,2, worker_id);
Handle the result in JavaScript
function onReceiveResultForWorkerTask(task_id, result) {
alert("the answer was " + result);
}
Java side:
public int spawnWorker() {
HandlerThread worker = new HandlerThread();
worker.start();
Handler h = new Handler(worker.getLooper()) {
#Override
handleMessage(Message msg) {
switch(msg.what)
case ADD:
// calculate the answer and send back to JS via UI thread
// Unpack parameters and task id from Message
mWebView.post(new Runnable(
public void run() {
mWebView.loadUrl("javascript:onReceiveResultForWorkerTask(task_id, " + (a+b) +");");
}
)
}
};
mWorkerMap.put(mWorkerId++, h);
return mWorkerId;
}
public int doAdditionOnWorker(int a, int b, int worker_id) {
Handler h = mWorkerMap.get(worker_id);
Bundle b = new Bundle();
int task_id = mTaskId++;
// pack arguments and task_id into the bundle
h.postMessage(Message.obtain(h, ADD, b);
return task_id;
}
Don't forget to go through and tear down all the worker threads that you spawn when the app doesn't need them anymore. Depending on how many workers you need you might also prefer to use a thread pool rather than creating new threads every time.
So I'm porting my Android app over to Kindle, and expected some tweaks for compatibility, but this is getting ridiculous.
The first thing I found out was that apparently running an AsyncTask from onCreate() can cause some problems if you are also initializing your layout from onCreate() as well. That's definitely not the case in regular Android, but the end result I was getting on Kindle was parts of my layout not being displayed at all. Moving the AsyncTask to onStart solved the problem, but this was a bit surprising. I still have a problem invovling this AsyncTask though.
The purpose of this AsyncTask is to load data from online and display the result in the activity's layout. Pretty straightforward, at least on regular Android devices. Just load the data in doInBackground, then in onPostExecute apply the data to the UI to build the page.
However it seems that in some cases (not all) the data from the AsyncTask won't display on screen. for instance, some data is displayed in a GridView. I update the GridView adapter, use the adapter's notifyDataSetChanged() method, and what should happen is that the view is changed to display the data from the adapter. This process on a normal Android device works fine.
On this Kindle however the data doesn't load (again in some cases, not all, which makes this even more frustrating). I've found that if you press the Back button on the Kindle, then drag your finger off of it (so finish() isn't called), the data will sometimes show up! I get the feeling the normal behavior of notifyDataSetChanged() has been altered for Kindle's version of Android, but I'm not sure what else I'm expected to do to show data loaded into an adapter.
Has anyone run into this issue?
Also, I should note in the case of it only happening sometimes, the actual data structure remains constant, but for certain pages it works and others it does not (the app pertains to music, so for example one artist's page loads properly while the other's does not).
I've included the AsyncTask in question, it works perfectly on any Android device so I don't know what use it will be, but here it goes anyway:
private class GetAttendees extends AsyncTask<Void, Void, AttendeesSectionData> {
#Override
protected AttendeesSectionData doInBackground(Void... params) {
JsonHandler jsonHandler = new JsonHandler(getApplicationContext());
return jsonHandler.getEventAttendeesFromFb(mEvent.getFacebookEventId(), JsonHandler.FACEBOOK_FRIENDS_NO_LIKES);
}
#Override
protected void onPostExecute(AttendeesSectionData data) {
if(data != null && !data.isEmpty()) {
if(data.size() > 18) {
ArrayList<String> toShow = new ArrayList<String>();
int friendLimit = data.getFriendsAttendingIds().size();
if(friendLimit > 18)
friendLimit = 18;
for(int i = 0; i < friendLimit; i++)
toShow.add(data.getFriendsAttendingIds().get(i).getImageURL());
int toShowSizeAfterFriends = toShow.size();
if(toShowSizeAfterFriends < 18) {
int othersLimit = data.getNonFriendsAttendingIds().size();
if(othersLimit > 18 - toShowSizeAfterFriends)
othersLimit = 18 - toShowSizeAfterFriends;
for (int i = 0; i < othersLimit; i++)
toShow.add(data.getNonFriendsAttendingIds().get(i).getImageURL());
}
mFbImagesAdapter.setItems(toShow);
}
else
mFbImagesAdapter.setItems(data.unloadIntoOneArrayList());
String caption = String.format(AppSettings.ATTENDEE_HEADER_CAPTION,
//fill in the %s with:
data.getFriendsAttendingIds().size(),
data.getNonFriendsAttendingIds().size());
caption = caption.replace("*", "<b>");
caption = caption.replace("?", "</b>");
mAttendeesCaption.setText(Html.fromHtml(caption));
calculateGridSize(data.size());
}
else {
Print.log("no attendees for event " + mEvent.getVenue_FormattedLocation());
mAttendeesGrid.setVisibility(View.GONE);
mAttendeesHeaderContainer.setVisibility(View.GONE);
}
loadFacebookImages();
}
}
Thanks!
I found the solution. Oh man that took a LONG time, but I finally got it.
It's really dumb. Basically, the xml attribute android:animateLayoutChanges will totally break your layouts. I guess Kindle will attempt to animate, but fail, so the end result is nothing happening. Removing all instances of android:animateLayoutChanges from my app fixed the problem.
I hope someone else who has this issue in the future can save a ton of time by stumbling across this answer.
My problem is make a function to set up themes in my application every 00:00 AM if there are new themes. As I know, to do this problem we must use a loop.
Here is my code:
private void updateThemes() {
Thread time = new Thread() {
public void run() {
int time = 0;
while(time > 86400000) {
//invoke method or start new activity
}
}
};
}
Please help me - Thanks.
Running a thread and waiting for a full day is not going to work. What if the phone is shutdown? What if the user switches to another app and your app is closed by Android because it needed the resources? Besides, it's not very battery friendly either.
You'd better use the Android AlarmManager to set the times at which you would like to check for updates. Also specify a BroadcastReceiver in your app that will receive and process the alarms. There's an example application that does this here or check this post for more info.