Starting and activity with startActivity(intent) from an app.Application object
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.
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.
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. If someone knows a better way, please, let me know.
Thanks in advance.
Have you tried setting Intent Flags to FLAG_ACTIVITY_NEW_TASK ?
Intent intent = new Intent(this, AnyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
精彩评论