Android的服务停止当应用程序被关闭应用程序、Android

2023-09-12 04:21:33 作者:别让梦想只是梦想

我开始从我的主要的Andr​​oid活性的服务如下:

I am starting a service from my main Android activity as follows:

final Context context = base.getApplicationContext();
final Intent intent = new Intent(context, MyService.class);
startService(intent);

当我从最近的应用程序列表刷卡它关闭活动页面,该服务停止运行,并重新启动一段时间后。我无法使用持久性服务,因为我的应用程序要求的通知。我怎样才能使服务无法重新启动或关机,只是不断在应用程序退出运行?

When I close the activity page by swiping it out from the recent apps list, the service stops running and restarts after some time. I can't use persistent services with notifications because of my app requirements. How can I make the service NOT restart or shutdown and just keep on running on app exit?

推荐答案

我在同样的情况,到目前为止,我学会了当应用程序被关闭关闭掉也因为它们是在一个线程服务,因此服务应该是为了另一个线程FOT它不会被关闭,考虑这一点,考虑保持服务活着报警经理在这里的一个例子的 http://www.vogella.com/articles/AndroidServices/article.html 这样你的服务就不会在通知中显示。

I'm in the same situation, so far I learned when the app is closed the service get closed also because they are in a one thread, so the service should be on another thread in order fot it not to be closed, look into that and look into keeping the service alive with alarm manager here an example http://www.vogella.com/articles/AndroidServices/article.html this way your service won't be shown in notification.

最后,所有的研究,我已经做了我逐渐认识到一个长时间运行的服务的最佳的使用开始后前景();因为它是为和实际的系统服务以及处理。

lastly, after all the research I've done I'm coming to realize that the best use of a long running service is start foreground(); because it is made for that and the system actually deals with your service well.

好运我的朋友。

 
精彩推荐
图片推荐