I have a Toast
that is called from inside a Service
.
It always appears regardless of the state of the app, as designed.
How can make it appear only when my app is in the foreground, and not when other apps are in front? That is, without moving it to Activity
code.
Foreground refers to the active apps which consume data and are currently running on the mobile. Background refers to the data used when the app is doing some activity in the background, which is not active right now.
Use the makeText() method, which takes the following parameters: The application Context . The text that should appear to the user. The duration that the toast should remain on the screen.
Positioning your Toast You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset. For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this: toast.
setGravity(Gravity. CENTER, 0, 0); toast.
mparaz,
There are a number of ways to do what you want very easily. Before I do that, I'd like to address a very important flaw in your question. An App is not an Activity, and an Activity is not an App. As such, an app cannot be in the foreground or background. That is reserved only for Activities.
A quick example and then to you answer. Your Service is part of your Application. The application is loaded though the Activity is not. If your Application were not loaded, your Service could not run. I only emphasize this because it is an important philosophy in Android and understanding the correct use of these terms and the concepts that define them will make it so much easier for you in the future.
A Simple Solution
You may extend the Application object and hold a simple public flag in the class. Then you can set the flag to false anytime the activity goes to the background and true when it comes to the foreground (or vice versa, of course).
Extending the Application:
public class MyApplication extends Application
{
static public boolean uiInForeground = false;
}
Setting the flag: In your Activity...
//This may be done in onStart(), onResume(), onCreate()...
//... whereever you decide it is important.
//Note: The functions here do not include parameters (that's Eclipse's job).
public void onStart()
{//Notice the static call (no instance needed)
MyApplication.uiInForeground = true;
}
public void onPause()
{
MyApplication.uiInForeground = false;
}
In your Service (where you call the Toast)
if (MyApplication.uiInForeground)
Toast.makeText(someContext, myMsg).show();
It's really as simple as that. Oh yeah... Don't forget to tell the manifest that you are extending the Application. For your Application declaration in AndroidManifest.xml
<application android:name=".MyApplication" .... >
<!-- All other components -->
</application>
Hope this helps,
FuzzicalLogic
Just place some flag
public class MyActivity {
private boolean isInForeground;
protected onResume() {
isInForeground = true;
}
protected onPause() {
isInForeground = false;
}
private displayToast() {
if(isInForeground) {
Toast.makeToast().show();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With