Starting and activity with startActivity(intent) from an app.Application object
<p>I have the next problem. I have a class that inherites from Application. In this class I keep track of gps locations of the android terminal. I need to start an alarm when certain situation takes place and after that I need to start an activity (form) for the user to take some decision.</p>

<p>I am using the normal code for that:</p>

<pre><code>public class FondeoApplication extends Application{
  .....
  private void lanzaAlerta(){
      mediaPlayer.start(); 

      Intent intent = new Intent(FondeoApplication.this, AlertaActivity.class);
      startActivity(intent);
    }
</code></pre>

<p>but I get an exception with the next out:</p>

<pre><code>Thread [&lt;1&gt; main] (Suspended (exception AndroidRuntimeException))   
    ContextImpl.startActivity(Intent) line: 822 
    FondeoApplication(ContextWrapper).startActivity(Intent) line: 276   
    FondeoApplication.lanzaAlerta() line: 199   
    FondeoApplication.checkPosition(Location) line: 190 
    FondeoApplication.access$5(FondeoApplication, Location) line: 179   
    FondeoApplication$1.onLocationChanged(Location) line: 171   
    LocationManager$ListenerTransport._handleMessage(Message) line: 227 
    LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 160  
    LocationManager$ListenerTransport$1.handleMessage(Message) line: 176    
    LocationManager$ListenerTransport$1(Handler).dispatchMessage(Message) line: 99  
    Looper.loop() line: 132 
    ActivityThread.main(String[]) line: 4123    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 491  
    ZygoteInit$MethodAndArgsCaller.run() line: 841  
    ZygoteInit.main(String[]) line: 599 
    NativeStart.main(String[]) line: not available [native method]  
</code></pre>

<hr>

<p>I have tried to start the activity from an object that inherits from Activity and everything works fine, so I guess that the problem is that I am trying to start an activity from an Application object.</p>

<p>Does anyone knows the correct way to achieve this?</p>

<p>Maybe I am not focusing the entire application well. But the only way I know to keep some code working in background is with an Application object. If someone knows a better way, please, let me know.</p>

<p>Thanks in advance.</p>
