gpt4 book ai didi

android - 如何在应用程序未运行时停止 Android 中的服务。

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:08 24 4
gpt4 key购买 nike

我已经在我的一个主要 Activity 类中启动了我的服务。问题是当我的应用程序没有运行时我想停止我的服务。我该如何实现这个。是否有任何选项可以在不停止它的情况下停止它使用任何按钮点击或控件的其他点击事件。

还有什么方法可以在我的应用程序运行时指示服务是否正在运行

最佳答案

只需像这样触发一个 Intent:

Intent intent = new Intent(this, Your_service.class);

只需在您停止服务的地方添加此代码块:

stopService(intent);

更新:

将此方法添加到应用程序的每个 Activity

public boolean onKeyDown(int key, KeyEvent event)
{
switch(key.getAction())
{
case KeyEvent.KEYCODE_HOME :
Intent intent = new Intent(this, Your_service.class);
stopService(intent);
break;
}

return true;
}

关于android - 如何在应用程序未运行时停止 Android 中的服务。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7609638/

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