Android :: Testing Activity With Async Tasks
Jun 4, 2010How do you create unit tests for an Android activity that starts async tasks in onCreate? I would like to test the result of these tasks.
View 1 RepliesHow do you create unit tests for an Android activity that starts async tasks in onCreate? I would like to test the result of these tasks.
View 1 RepliesIn the documentation, I read that it is not possible for executing 2 Async Tasks. If execute is called again, it should throw an exception.
The following is given under the heading of "Threading Rules", and i quote:
"The task can be executed only once (an exception will be thrown if a second execution is attempted.)"
I wish to understand the meaning of this statement.
Does this mean, that if i try and execute the same task twice (concurrently) when the first execution of the task was still going on...an exception will be thrown ? Will i need to wait for the completion of the first task, before i spawn a second task ?
A possible use case:
The user wishes to download a set of files. He clicks on the download button by providing the URL. When the first download is still going on, he wishes to download another file and provides another URL. Both these user actions are serviced by a DownloadTask, that extends the AysncTask.
If i wish to change this default behavior, is there a configuration option?
I wrote a simplest test based on android.test.ActivityInstrumentationTestCase2.
CODE:..............
I am trying to have the browser part of my task. The reason being is that I am awaiting a callback from the browser before continuing.
View 3 Replies View RelatedHas anyone any idea on how to solve the generic problems related to starting a task in background from an activity and when the task is finished posting the result to the activity that created ? (the activity might get destroyed in the meantime due to a orientation change ,or receiving a call , or might be in the process of destroying and recreation)
View 2 Replies View RelatedI'm using a web service disguised as a ContentProvider and AsyncQueryHandler to isolate my activity from the delays of transport. My question is: during testing, how can I determine when my activity has retrieved the data from the web service? That would be after onQueryComplete() in the AsyncQueryHandler has come back with the "real" data. During manual testing, I just sit there and wait until the view updates. How can I do that using ActivityUnitTestCase and ActivityInstrumentationTestCase2?
View 2 Replies View RelatedI want to create a stub of one of my Activities, but I want to keep it in the test project so not to have it in the actual apk. I don't seem to be able to find a way to do it though. I get this:
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.example.test.StubActivity }
I did find if I put it in the main project and put it in the manifest accordingly (IntentFilter:(Action:Main, Category:Default)) it will work, well it actually crashed, but I am not bothered by that. Something to fix later on.I did think about putting everything in the same main project and just have a test package, but that would then involve messing the manifest, I think. It seems better to keep them separate. It's just cleaner.
while testing a activity can we move from that activity to the another activity . i think it is possible as i found that we can deploy a complete test application a android app but i am not gettig how i can move from one acitivity to another activity ....
View 6 Replies View RelatedI've been exploring the Android test package and I'm very confused on how to implement functional testing for endpoint-to-endpoint.
Consider the example below, I have 4 activities: 1. A Contact List Activity 2. A View Contact Activity 3. An Add/Edit Contact Activity 4. A Sync Contact List activity
I want to automate a few tests that will go through multiple test cases simulating a user experience: From launching the application, to selecting 'Add contact;' opening the add contact activity, entering data, saving it, going back to the list activity, and verifying the new contact was saved properly.
As I understand it, ActivityInstrumentationTestCase<T extends Activity> and ActivityUnitTestCase<T extends Activity>, are only useful for testing single activity functionality.
But what if I need to switch between 2 or more activities inside a single functional test case?
Im using a web service, so I want to use an async thread for the HTTP authentication request and another thread later to make additional service requests while my main thread runs.Would like to see a good example of how to do this and how to show busy messages somehow in main app. How does the main app know when the thread finished? And what if my thread encounters exceptions, how do I deal with that?HTTP requests are sent later, use the same cookies setup up by the first auth request, so will the later requests pick up the same cookies and just work?
View 2 Replies View RelatedI just have a concern about AsycnTask. If i have one activity which starts a async task. Now that activity goes in background and new activity gets opened & visible to us.
So the async task opened by previous activity stops or it still keeps going in background or it pauses or stops ?
I am developing a program that needs async socket. I am new to Android and Java. As I can see in the document, to write network program, I got Socket and SocketChannel, but it seems they are all sync socket, it means, I need an extra thread to call blocking read, write... and etc. Here comes the problem, my program is a chat room, the communicate is not linear, I have no idea when the server will send a message to me, if I read in that thread to wait message from server, but the user want to send message to server, the reading function blocked forever, if there is no incoming data, then the pending outgoing message will not be sent. That's why I need async socket. So my question is: How to write async socket under Android environment?
View 8 Replies View RelatedI've got the URL of a .png image, that needs to be downloaded and set as a source of an ImageView. I'm a beginner so far, so there are a few things I don't understand:
1) Where do I store the file?
2) How do I set it to the ImageView in java code?
3) How to correctly override the AsyncTask methods?
What is the difference between Service, Async Task & Thread. If i am not wrong all of them are used to do some stuff in background. So, how to decide which to use and when?
View 2 Replies View RelatedI read somewhere (and have observed) that starting threads is slow. I always assumed that AsyncTask created and reused a single thread because it required being started inside the UI thread.The following (anonymized) code is called from a ListAdapter's getView method to load images asynchronously. It works well until the user moves the list quickly, and then it becomes "janky".final File imageFile = new File(getCacheDir().getPath() + "/img/" + p.image);image.setVisibility(View.GONE);view.findViewById(R.id.imageLoading).setVisibility(View.VISIBLE);
(new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
try {Bitmap image;
if (!imageFile.exists() || imageFile.length() == 0) {
image = BitmapFactory.decodeStream(new URL(
"http://example.com/images/"
+ p.image).openStream());
image.compress(Bitmap.CompressFormat.JPEG, 85,
new FileOutputStream(imageFile));
image.recycle();
}image = BitmapFactory.decodeFile(imageFile.getPath(),
bitmapOptions);
return image;
} catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
@Override
protected void onPostExecute(Bitmap image) {
if (view.getTag() != p) // The view was recycled.
return;
view.findViewById(R.id.imageLoading).setVisibility(
View.GONE);
view.findViewById(R.id.image)
.setVisibility(View.VISIBLE);
((ImageView) view.findViewById(R.id.image))
.setImageBitmap(image);
}}).execute();I'm thinking that a queue-based method would work better, but I'm wondering if there is one or if I should attempt to create my own implementation.
I have the following code...
CODE:.................
My mClient get method is a simple HttpClient execute with an output buffer to read the input stream, so it's very trivial. Everytime I execute this task UnknownHostException will be triggered which will in turn call onTaskError on my currect Activity, but I still get a RuntimeException, but I don't see why. Maybe it's because it's late and i've done about 12 hours of writing code so it's something simple I just can't see?
In my android application i am using a loading screen,processed by Async task.In do in background of this task i would like to display a alert whenever an upgraded version exist in the server.
Could you please let me know if there is any way that i can do it in android.
The code is.
CODE:.................
I have been searching for an answer for this for some time now. I have an async task that downloads the database needed for my app, while this is downloading my app cant do anything as all the data it references is in this file, i have the app waiting for the file to be downloaded but i am attempting to show a progress dialog so the user knows something is happening while they wait for this to happen.however nothing shows up i have also tried directly calling ProgressDialog.show in the pre execute and moving this to the calling activity with no luck.
View 2 Replies View RelatedI have a simple app and I'm using the AsyncTask to test out a background process, for clearness purposes I've opted to put my AsyncTask in a separate class rather than in an inner class, which where my problems begin, this is my AsyncTask. Code...
View 8 Replies View RelatedI've got an Android activity which grabs an RSS feed from a URL, and uses the SAX parser to stick each item from the XML into an array. This all works fine but, as expected, takes a bit of time, so I want to use AsyncActivity to do it in the background. The line items = parser.getItems() works fine - items being the arraylist containing each item from the XML. The problem I'm facing is that on starting the activity, the ProgressDialog which i create in onPreExecute() isn't displayed until after the doInBackground() method has finished. i.e. I get a black screen, a long pause, then a completely populated list with the items in. Why is this happening? Why isn't the UI drawing, the ProgressDialog showing, the parser getting the items and incrementally adding them to the list, then the ProgressDialog dismissing?
View 3 Replies View RelatedI have read quite a good books & blogs about the Background work process in Android and updating the UI.
I have learned few way like using a Service with Intent, Using AsynTask, Using a Bind Service with a Intent and so..
What is the rule of thumb using this, i mean when to use what?
I am just learning to code in JAVA & Android.
What is the best performing app to set a reminder to do something? Not an appt, but just to remember to do. a task list or todo app that shows up on the home screen. For instance, I need to remember to bring something to work for tomorrow. Or, like, remember to buy eggs on the way home. On previous non android phones, it had the task list showing on the home screen.
View 6 Replies View RelatedI am currently using Gtasks and I like being able to add a task that will allow an audible alert but lately for some reason the program has been buggy and its starting to frustrate me. I was wondering if anyone knows of an app that will do something similar All I want something that has a widget so I can see upcoming events or tasks and it must also be able to alert me audibly - the built in calendar on my Captivate is terrible. A calendar type app would be fine as well. I saw this in the market Pure Calendar Widget - anyone out there that can recommend this app? I would appreciate any info on an app I should try.
View 2 Replies View RelatedI'm not talking about a startup app like startup auditor, or a task killer, but something that will actually prevent the tasks I don't want, from opening at all. I can't STAND moxier mail. It always opens in the background. I don't want it opening EVER, so I'd love an app like this.
View 4 Replies View RelatedI had a question regarding this concept. Suppose i have an application that defines a task. This task has some activities that belong to another Android application. The Dev guide says, that to maintain a uniform user experience, Android will maintain such 'distributed' activities as part of the same 'task' even if they belong to different applications.
I refer to it from here: http://developer.android.com/guide/topics/fundamentals.html
Some lines later, the Dev guide says and i quote: * * *"**Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward. When the user presses the BACK key, the screen does not display the activity the user just left (the root activity of the previous task). Rather, the activity on the top of the stack is removed and the previous activity in the same task is displayed."*
I think "three under the current activity" should be "four under the same task".
I think this paragraph i quote above has nothing to do with activities distributed across applications..and belonging to the same task. This para is maybe talking about two applications, that share a common application launcher and each application has defined its own tasks (stack of activities).
The user is simply toggling from one application to another, thus demonstrating that all activities (of a task) move together. However, the "three under the current activity" phrase is confusing me.
New to Motorola droid. I was wondering if there is a shortcut to show you what apps/tasks you have running, and allows you to easily switch between them? Currently, I just hit the home button to get out of one app/task and then switch to the screen with the app I want, and click on the app/task. I imagine there is an easier way via a key or shortcut that will pop up my open apps and allow me to choose which one to go to. I figure with all the talk of the droid being a multitasker, I figure there would be an easy way to switch between tasks.
View 3 Replies View RelatedIs there a PC desktop application that can sync with Android calendar, tasks, and reminders?
I know on Windows Mobile I can use Outlook for this.
But I don't know how is this on Android.
I am a WinMo user and seriously thinking to jump move to Android because HTC Desire will be released soon
WARNING: Back up your Astrid 2 before doing the Astrid 3 upgrade!I just updated to Astrid 3.0 ("Update Available"). Silly me, I didn't read the notes other than to see that Astrid 3 was "all new". My tasks vaporized! The I tried to "restore" and it said I had to buy an add-on pack to do that. No indication of whether the restore in V3 is compatible with saved tasklists from V2. The experience left me cold. I uninstalled Astrid 3 and reverted to V2 via the link http://bit.ly/oldastrid. Then I restored from the saved list on my SD card from the scheduled backup that Astrid did last night. I still lost some tasks I entered today.
View 22 Replies View RelatedI have now had my Desire for over a month and very happy with it so far except for one small annoyance. I'm a heavy user of my Outlook Tasks and am desperatly trying to find an app that allows me to sync my Outlook Tasks with my phone and add/amend for either side. Anyone know of a suitable app?
View 12 Replies View RelatedI don't know how to elegantly solve the following task:
I have several blocks of code (operation) to execute. Each block can return true of false to indicate that further execution is possible. Inside of each block I have to use asyncronous methods calls (Because Android is completeley asynchronous).
Example of processing operations (not working now as expected):
CODE:.......
The problem is that inside of the operation I need, for example, display AlertDialog and wait for the input. But after I call dialog.show() my method execute finishes and it returns incorrect result.
Example of the button listener, registerd with AlertDialog is below:
CODE:..........
How should I modify processing of operations to support asynchronous model of Android?