AlertView that can be shown from background - java

I have a little problem with an alert View, the idea is that when the app starts, it asks on background for new data for the app, so after receiving the information an AlertView should be shown if there is new data. The problem comes because the AlertView is only shown on the main activity where I launched the asyncTask, so if I'm already in other activity, the alert will appear when I come back to the main activity and the Idea is that the AlertView is displayed in whatever activity the user is in.
My first thought was that the Context that I send to the AsyncTask is the problem so I've tried using getApplicationContext() so the app crashes in the moment of creating the alertView. So I'm, looking for a way to display this AlertView or something equivalent on the screen wherever he is on the app, does any one have ideas?
any idea would be greatly appreciated

you can use broadcastreceiver in every activity which will contineous monitor your background service contineous whenever event occoured using broadcastreceiver display alertbox with content received by receiver.

Related

Why does an activity playing a sound disturb the user interface?

I would like to show a notification and play a sound when the user taps onto that notification. It works somehow when I use an activity to play the sound, as I have written in my own answer to this question: How can I create a notification that plays an audio file when being tapped? (in this question and answer there is also the source code showing how I create the notification and how my PlaySoundActivity looks like.
Yet, I have realized, that while the sound is playing, the appearance of my main application changes and it will not be restored without closing the application.
I have created my application from the "Tabbed Activity" project template.
This is how it looks after being started:
And this is how it looks when I have tapped onto the sound notification (the sections are gone):
Can anyone explain why this happens? Is it a wrong approach to play sound using an activity? But it does not work here when I use a service, I hear nothing! How to solve that?
According to the Android developer reference, "almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)".
I had not done this. Therefore a new window was displayed, but there was no content.
The better solution for playing a sound is to use a PlaySoundService (service!!!) instead of an activity. It contains almost the same code as an activity would do, but the pending intent is created with PendingIntent.getService(...) instead of PendingIntent.getActivity(...). Using the wrong method does not result in an obvious error message, but the service won't work as expected.

Slow switching between android activities. Trying to use a Progression Dialog to show its loading

So this is a tuff one. I have a activity that calls a class that call a http downloader which is the async task. What I want to do is have a please wait loading screen notification for the user while the activity is getting all the data from the web. I don't have a clue on how to get the loading screen to be called. Usually you would have it in the async task but my async task does not have a view. How to best go about doing this?
Thank you for any help.
My http client does have a doInBackground() so should I put the on preExecute and postExecute there? But then how would my main method which is twice removed know when its down and when its not.
What is happening is that when a button is clicked it starts the activity and starts to process all the methods, but it linger on the first activity until everything is done loading. I don't want that I want the progress dialog. It just won't show. Any thoughts on this?
NEW UPDATES
Ok so now I got this set up a bit better so now at least the progress dialog now shows up. the problem is the Async task does not stop running.
You should use an AsyncTask. It doesn't have to have a view, it's just a background process just like a thread.
Refer to this on how to use an AsyncTask properly with a ProgressDialog.
Do your methods in the doInBackground() method of the AsyncTask.

How to execute code everytime app opens Android

I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}

How to get Asynctasks to update a different activity than it was launched from

I have an Splash Screen activity that launches an Asynctask that goes off and downloads and processess alot of data. It takes a while (20ish seconds) and it's a refresh of data so I dont wait for it and so send my users to a main activity where they can view the last cached data (with an indicator data is still downloading).
What is the best way (or any way) for the async task to notify the main activity (or any other current activity the user is viewing) that its done and to refresh screen and stop the indicator?
FYI, I dont want to just launch the AsyncTask from the Main as there are 3 other activitys that the user may have navigated to that all would need to know when it is done and to stop showing the indicator.
Your AsyncTask can broadcast an Intent when the data is available. The current Activity can set up a listener for that broadcast Intent and refresh the screen when it get it.

Start Activity on background

I have an app that responds to Internet message by creating a new Activity and user has 20 seconds to respond.
The problem is when the app is running on background.
I can show a notification, but when the user returns to the App the new Activity isn't started.
Is there any way to start an Activity even when the App isn't on foreground (without the activity getting focus) or any easy workaround where the activity would start right after returning to the app? (which would be worse solution, cause I would have to rework the sync timer :))
Thanks
Take a look at the Activity life cycle
Here you have the onResume() method you can override and do something before the Activity itself is shown. From there you could do some kind of check that you are returning from a notification or a check on that the user has to answer something now and launch a new Activity from the onResume() method.

Categories

Resources