简体   繁体   English

从app.Application对象使用startActivity(intent)进行启动和活动

[英]Starting and activity with startActivity(intent) from an app.Application object

I have the next problem. 我有下一个问题。 I have a class that inherites from Application. 我有一个从Application继承的类。 In this class I keep track of gps locations of the android terminal. 在此类中,我跟踪android终端的gps位置。 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. 在某些情况下,我需要启动警报,然后,我需要启动一个活动(窗体),以便用户做出一些决定。

I am using the normal code for that: 我为此使用普通代码:

public class FondeoApplication extends Application{
  .....
  private void lanzaAlerta(){
      mediaPlayer.start(); 

      Intent intent = new Intent(FondeoApplication.this, AlertaActivity.class);
      startActivity(intent);
    }

but I get an exception with the next out: 但我得到了一个例外,接下来:

Thread [<1> 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]  

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. 我试图从继承自Activity的对象开始活动,并且一切正常,所以我想问题是我试图从Application对象开始活动。

Does anyone knows the correct way to achieve this? 有谁知道实现此目标的正确方法?

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. 但是,我知道让某些代码在后台运行的唯一方法是使用Application对象。 If someone knows a better way, please, let me know. 如果有人知道更好的方法,请告诉我。

Thanks in advance. 提前致谢。

Have you tried setting Intent Flags to FLAG_ACTIVITY_NEW_TASK ? 您是否尝试过将意图标志设置为FLAG_ACTIVITY_NEW_TASK?

Intent intent = new Intent(this, AnyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM