Service 不提供 findViewById() 方法。若需在 Service 中操作視圖(如通知欄自訂視圖),必須先用 LayoutInflater 膨脹佈局,再對根視圖呼叫 findViewById。
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.my_layout, null); // 父視圖
TextView textView = rootView.findViewById(R.id.my_text); // 在父視圖上查找
// 後續操作 textView...
RemoteViews。SYSTEM_ALERT_WINDOW 權限)。Service 本身無視圖層級,setContentView 僅存在於 Activity。