![](/img/trans.png)
[英]Can I stop the service which is started by bindService instead of StartService in android?
[英]Can i stop a service in Android Studio if I close the application that started it?
所以我是 Android 的初學者,我想創建一個后台服務,每 24 小時更換一次壁紙。
我制作的應用程序在 Main Activity 中有一個按鈕用於啟動服務,另一個按鈕用於停止服務。
我想知道的是,如果我啟動服務,然后關閉應用程序,如果我重新打開應用程序並按“關閉服務”按鈕,我可以停止服務嗎? 我很困惑,因為我不知道應用程序是否會知道對活動服務的引用。
這是 Context 類中 stopService(Intent service) 的文檔。
“請求停止給定的應用程序服務。如果服務沒有運行,什么都不會發生。否則它會停止。請注意,對 startService() 的調用不計算在內——無論服務啟動多少次,這都會停止服務”
所以你只需要從你的活動中調用 stopService(new Intent(this, YourService.class)) ,它就會停止 YourService 的所有實例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.