我的应用程序使用第三方库,该库启动了 START_STICKY 服务。所以即使应用被销毁,服务被杀死,它也会自动重启。
我的问题是,有什么方法可以像更改应用程序一样更改此服务生命周期吗?服务在应用启动时启动,服务在应用停止时停止。
最佳答案
在 list 文件中向该服务添加一个intent Filter
<service
android:name="ServiceName"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="IntentName"/>
</intent-filter>
</service>
然后使用此 Intent 在您想要的任何地方启动或停止服务
String intentName="IntentName";
Intent i=new Intent(itentName);
context.stopService(i);
或
context.startService(i);
关于Android 如何停止 START_STICKY 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28961419/