简体   繁体   English

MainActivity销毁后如何使用Service调用MainActivity方法

[英]How to call MainActivity method using Service after MainActivity's destroyed

I'm using a service which repeatedly needs to call a MainActivity method.我正在使用重复需要调用 MainActivity 方法的服务。 The service runs in background after MainActivity is destroyed. MainActivity 销毁后,该服务在后台运行。 But when calling the said method, it crashes.但是当调用上述方法时,它会崩溃。

So, how do I access classes using service even after app is killed.那么,即使在应用程序被杀死后,我如何使用服务访问类。

My service class's method我的服务类的方法

public int onStartCommand(Intent intent, int flags, int startId) {

    // This service runs in background 

    broadcastIntent(); // Start my MainActivity. Because the app is killed

}

Maybe with some LiveData?也许有一些 LiveData? I've started to learn how it works and maybe it might help you我已经开始了解它是如何工作的,也许它可以帮助你

I found the answer in case someone's looking for it:如果有人在寻找它,我找到了答案:

Classes declared in AndroidManifest can always be accessed using intents no matter app is running or not.无论应用程序是否正在运行,都可以使用意图访问 AndroidManifest 中声明的类。

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

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