編輯:關於 Android 程式設計
想列出裝置上目前正在背景執行的服務,靠的是 ActivityManager 提供的 getRunningServices()。它回傳 RunningServiceInfo 清單,每筆資料都帶有 process、pid、client 數量、崩潰次數等資訊,做診斷工具時很實用。
public static String getRunningServicesInfo(Context context) {
StringBuffer serviceInfo = new StringBuffer();
final ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 最多取 100 筆
List<RunningServiceInfo> services = activityManager.getRunningServices(100);
Iterator<RunningServiceInfo> l = services.iterator();
while (l.hasNext()) {
RunningServiceInfo si = l.next();
serviceInfo.append("\npid: ").append(si.pid);
serviceInfo.append("\n\nname: ").append(si.service);
serviceInfo.append("\n\nprocess: ").append(si.process);
serviceInfo.append("\n\ncrashCount: ").append(si.crashCount);
serviceInfo.append("\n\nclientCount: ").append(si.clientCount);
serviceInfo.append("\n============================");
serviceInfo.append("\n\n\n");
}
return serviceInfo.toString();
}
這個方法從 Android 5.0(API 21)起已被標記為 @Deprecated,官方說明是僅回傳呼叫者自己的服務,無法再看到其他 App 的服務——這其實是隱私權限收緊的結果,早期版本那種「能看到全機服務」的行為在現代 Android 上已不可行。
因此若你的目的是:
ServiceCompat 或自行維護一份服務註冊表,並搭配 startForeground() 的回報機制。UsageStatsManager(需 PACKAGE_USAGE_STATS 特殊權限)與 NetworkStatsManager,是官方推薦的替代方案。adb shell dumpsys activity services 的輸出,資訊比這個 API 完整得多。
Android 高仿微信轉帳金額輸入框規則
微信轉帳輸入框的金額輸入規則,重點在於小數點處理、位數限制與首字驗證。以下整理常見規則與實作方式。 輸入規則 小數點後最多兩位。 使用者若一開始就輸入小數
Objective-C Runtime 深入解析:從核心數據結構到消息轉發與方法交換
前言 Runtime 是一套相對底層、以純 C 語言實現的 API 集合。我們日常編寫的 Objective-C 代碼,在程序真正運行時,最終都會被轉譯為 Runtime
Android 開發步步為營(31):TabActivity 用法與把 Tab 顯示在底部
Tab 標籤頁是非常常用的控制項。.NET 裡有 multipage+tabstrip 的組合透過標籤切換頁面,HTML 裡常用 frameset 來實現;而在 Andr
Android ViewFlipper 使用示例:打造註冊引導頁
ViewFlipper 的工作機制非常直白:它把加入其中的子 View 按順序輪流顯示,同一時刻只有一個子 View 可見,其餘全部置為 GONE。子 View 既可以寫