Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>mparaz,</p> <p>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. </p> <p>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.</p> <p><strong>A Simple Solution</strong></p> <p>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).</p> <p>Extending the Application:</p> <pre><code>public class MyApplication extends Application { static public boolean uiInForeground = false; } </code></pre> <p>Setting the flag: In your Activity...</p> <pre><code>//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; } </code></pre> <p>In your Service (where you call the Toast)</p> <pre><code> if (MyApplication.uiInForeground) Toast.makeText(someContext, myMsg).show(); </code></pre> <p>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</p> <pre><code>&lt;application android:name=".MyApplication" .... &gt; &lt;!-- All other components --&gt; &lt;/application&gt; </code></pre> <p>Hope this helps,</p> <p>FuzzicalLogic</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload