Android判斷Service是否開啟

2021-07-05 10:25:17 字數 655 閱讀 3025

最近改bug的時候,發現有個service總是開啟,關閉,重複執行,於是對他做了點小的改動,判斷是否啟動,為真的話則不做任何事,為假,startservice,下面奉上程式片段,給自己做個記錄,也方便下大家。

主要思路:通過activitymanager來迴圈判斷service是否存在。

必要條件:宣告許可權

判斷函式:

/**

* 判斷服務是否開啟

** @param mcontext

* 上下文

* @param classname

* classname

* @return true:開啟 false:未開啟

*/public static boolean isworked(context mcontext, string classname)

}return false;

}

使用例項

if (buildconfig.debug)  else 

}

結果:

Android不同應用間通過Service進行通訊

步驟一 接收方應用定義乙個service,清單檔案中註冊 android enabled true android exported true android process remote 必備屬性 步驟二 主動發起通訊方應用啟動service intent new intent intent.pu...

Android測試教程 8 測試Service

android 測試框架也提供對service測試的支援,基本類為servicetestcase,因為service類通常假定和它是和client是分開使用的,因此你可以無需使用instrumentation 來測試service。當你設計乙個service時,你應該考慮測試用例中如何檢查servi...

Android測試教程 8 測試Service

android 測試框架也提供對service測試的支援,基本類為servicetestcase,因為service類通常假定和它是和client是分開使用的,因此你可以無需使用instrumentation 來測試service。當你設計乙個service時,你應該考慮測試用例中如何檢查servi...