gpt4 book ai didi

android - 从 app.Application 对象使用 startActivity(intent) 启动和 Activity

转载 作者:行者123 更新时间:2023-11-30 04:30:47 25 4
gpt4 key购买 nike

我有下一个问题。我有一个继承自 Application 的类。在这个类中,我跟踪 android 终端的 gps 位置。我需要在某些情况发生时启动警报,然后我需要启动一个 Activity (表单)供用户做出决定。

我正在为此使用普通代码:

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

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

但我得到了下一个异常(exception):

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]

我试图从一个继承自 Activity 的对象启动 Activity ,并且一切正常,所以我猜问题是我试图从一个应用程序对象启动一个 Activity 。

有谁知道实现此目标的正确方法吗?

也许我没有很好地关注整个应用程序。但我知道让一些代码在后台运行的唯一方法是使用 Application 对象。如果有人知道更好的方法,请告诉我。

提前致谢。

最佳答案

您是否尝试过将 Intent Flags 设置为 FLAG_ACTIVITY_NEW_TASK ?

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

startActivity(intent);

关于android - 从 app.Application 对象使用 startActivity(intent) 启动和 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7753422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com