I need to write real time log data in an Activity window from a Service to watch what is happening. Like Console.WriteLine.Log.I(), but that isn't good for real time. If I write to a TextView, I need to periodically refresh the Activity and save data in onDestroy() when the screen orientation is changed. It is complex, and I don't now how to refresh the Activity from a static method. What is a simple solution for this?
Maybe a good idea will be writing logs to a certain log file on SD card and then when your Activity is in foreground you just need to load the file contents to your TextView. Hope this helps.
Related
The problem is only when my activity already shown it begins updates and it look no good. How it works: in onStart of activity I send cmd to service to get update data, also I register brodcast listener there. I want to prepare received data from service to show before activity appears. How to do that? Thanks.
How it works now: when I back from another activity first I see old data and then it changes (very fast but you can see it) to new.
if you want to setup things before your activity is shown you need to do things in the onCreate method instead of onStart method.
further informations in android documentation
When you send the command to the service to update your data, you should change the views in your Activity to show loading indicators, or a blank view, or whatever you want the user to see until the Broadcast comes in that your new data is ready. Then you shouldn't need to worry about old data being visible.
My app represents funny sentences that my users upload, and in order to know which sentences are better I want to know if the user has taken a screenshot. The only related thing I found is from Google Maps. Anyone know how to make a "screenshot listener" or which method it invokes? Thanks.
One way to do this is by creating a class which is implementing FileObserver. Then add listener when you are loading activity ( by using onStart() activity's method) and remove listener when you are going out of your activity (by using onStop() activity's method).
Once you get FileObserver event, you have to be sure that is a picture creation.
Note that this method is not 100% safe but it's simple to make it works !
I have a method in my main class, that fetches some data from the internet. The thing is that after everything is done, if I change the screen orientation by moving the device, everything starts allover again(fetching data while displaying a loading screen). Is there somewhere I could put my method so that if my device's screen orientation changes, it won't erase everything that has been done until that moment? Thanks.
What is happening to you is that every time you rotate your activity is recreated, as per android good practices you should handle your activity being recreated because android may destroy your activity at any point if resources go low on the device. Take a look at saving the state of your activity and how to restore it and the link.
Example using onSaveInstanceState()
You can use a singleton class to store your data.
If you prefer a simpler way you can also put your data as static, so the orientation change will not throw them away.
I think that your Activity is getting recreated again. In that case,it will load again.
1). You can handle orientation change by overriding
public void onConfigurationChanged(Configuration newConfig)
and in your activity declaration in manifest file add the following line
android:configChanges="orientation|screenSize|keyboardHidden"
2). As Aerilys said in the above answer, you can use singleton class to store data. Before displaying your loading screen check if you single ton object has data or not. If yes then skip displaying your loading screen
Is there any way I can go from one activity to another and delay the display of the screen on the target activity?
What I want to be able to do is to allow the target activity to fetch its required data but not to display anything until does.
I want the screen of the source activity to still be visible until I am ready with the data in the second activity.
Specifically, I am using an AsyncTask to fetch the data.
I know I could fetch the data in the source activity and then send it on to the target activity but this is not viable in our case.
Edit: More Info:
The whole reason I want this is because I am trying to change the structure of certain parts of the current code.
At present the way it works is the the first activity gets the data and then sends it to the second activity as a bundle.
This created problems when the second activity could be invoked from multiple places. It resulted in loads of duplicate code.
So, I decided to move the fetching of the data into the target activity thus cutting out any need for repeating code.
it also makes more sense for the activity to fetch its own data rather than relying on something else sending it.
You should first make a service that runs your async task. Then, start the service from your first activity with startService(new Intent(this, UpdaterServiceManager.class));
When the task ends in the service, start the second activity.
Click here for an excellent service tutorial.
Try to use service for this purpose.
I google a lot, but now I decided to ask..
My activity does only one simple thing - it downloads a picture from Internet using AsyncTask.
But there is a lot of problems, that I am not sure, how to solve..
1.] User rotates screen after AsyncTask have downloaded the picture.. How should I handle this screen rotation? Where to temporary save picture to be available after activity recreates?
I don't want to lock screen orientation (android:screenOrientation="landscape") or handle it by myself (android:configChanges="orientation")..
I just want to save the picture somewhere..
I know It can be done by using onRetainNonConfigurationInstance() and getLastNonConfigurationInstance(), but both are deprecated now. Bundle isn't appropriate for images, because it is made for String (Serializable) data. I can save picture to database or to file somewhere, but it is unnecessary. I read somewhere, that it can be done by Loader, but I don't know how? Is it possible?
2.] User rotates screen during AsyncTask is downloading a picture.
I want AsyncTask to continue with downloading. After download is complete, AsyncTask should save image to new (recreated) activity in onPostExecute method.
I think if AsyncTask is inner class of activity, it works..? But if I don't want it to be inner class I have to save reference to activity, for example in variable. But if I send reference to Activity as a parameter of constructor of AsyncTask, and Activity is recreated after screen rotation, the variable in AsyncTask does refer to original activity and so new one doesn't know about downloaded picture and it has to start download again..
Or is AsyncTask bad choice for downloading image? Should I use something else? Service?
So.. How to solve these problems? I am sure a lot of app developers have to solve this too.. But it is all Greek to me:(
When your AsyncTask has finished downloading the image, save it to your application's cache directory. Persist the path to the saved image in a property on your Activity class. You can then override the onSaveInstanceState method of your activity to pass the cached image path to itself during an orientation change. In your onCreate method check the savedInstanceState bundle for a saved image path. If that path exists, do not attempt to re-download the image. Instead, simply load the image from disk.
I've already answered somewhere. I found a very nice example: read this. This example for old APIs.
For new you can use setRetainInstance method of a fragment (if you use fragments).