SystemService

系統級的 Service,以 Notification 為例,取得的方法如下:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE)

每個 Service 和 Class 名稱都有對應,這在[官方說明](http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String) getSystemService() 方法裡面都有寫。

條列如下:

Class NameConstant NameDescriptionAPI Level
AccessibilityManagerACCESSIBILITY_SERVICE4
AccountManagerACCOUNT_SERVICE5
ActivityManagerACTIVITY_SERVICE1
AlarmManagerALARM_SERVICE1
AppOpsManagerAPP_OPS_SERVICE19
AudioManagerAUDIO_SERVICE1
BluetoothAdapterBLUETOOTH_SERVICE18
CaptioningManagerCAPTIONING_SERVICE19
ClipboardManagerCLIPBOARD_SERVICE1
ConnectivityManagerCONNECTIVITY_SERVICE1
ConsumerIrManagerCONSUMER_IR_SERVICE19
DevicePolicyManagerDEVICE_POLICY_SERVICE8
DisplayManagerDISPLAY_SERVICE17
framework:android:systemservice:downloadmanagerDOWNLOAD_SERVICE9
DropBoxManagerDROPBOX_SERVICE8
InputMethodManagerINPUT_METHOD_SERVICE3
InputManagerINPUT_SERVICE16
KeyguardManagerKEYGUARD_SERVICE1
LayoutInflaterLAYOUT_INFLATER_SERVICE1
LocationManagerLOCATION_SERVICE1
MediaRouterMEDIA_ROUTER_SERVICE16
NfcManagerNFC_SERVICE10
NotificationManagerNOTIFICATION_SERVICE1
NsdManagerNSD_SERVICE16
PowerManagerPOWER_SERVICE1
PrintManagerPRINT_SERVICE19
SearchManagerSEARCH_SERVICE1
SensorManagerSENSOR_SERVICE1
StorageManagerSTORAGE_SERVICE9
TelephonyManagerTELEPHONY_SERVICE1
TextServicesManagerTEXT_SERVICES_MANAGER_SERVICE14
UiModeManagerUI_MODE_SERVICE8
UsbManagerUSB_SERVICE12
UserManagerUSER_SERVICE17
WallpaperServiceWALLPAPER_SERVICE1
WifiP2pManagerWIFI_P2P_SERVICE14
WifiManagerWIFI_SERVICE1
WindowManagerWINDOW_SERVICE1

DownloadManager 提供相關的 class 如下:

Class NameDescription
DownloadManager下載管理器
DownloadManager.Query查詢下載的訊息
DownloadManager.Request請求下載的詳細內容

首先要能下載,第一步是需要權限,在 AndroidManifest.xml加入以下權限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

DownloadManager 是系統層級的 Service,所以要在 Activity 下用 getSystemService() 取得

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

再來是初始化 Request

String url = "http://www.example.com/file.zip";
DownloadManager.Request request = new DownloadManager.Request(url);
// 設定request
request.setTitle("Title");
request.setDescription("Description");
// 排程下載,並取得唯一碼
long downloadId = downloadManager.enqueue(request);

設定 request 的 method 如下,下面每個 method 都可 chaining

MethodDescription
addRequestHeader(String header, String value)設定 HTTP 表頭
setAllowedNetworkTypes(int flags)允許下載的網路型態為何,可以為 NETWORK_MOBILE和NETWORK_WIFI
setAllowedOverMetered(boolean allow)是否允許計量網路下載
setAllowedOverRoaming(boolean allowed)是否允許漫遊時下載,預設是true
setDescription(CharSequence description)通知訊息裡的小字
setDestinationInExternalFilesDir(Context context, String dirType, String subPath)設定檔案存放位置,使用系統的目錄
setDestinationInExternalPublicDir(String dirType, String subPath)設定檔案存放位置,使用自定的目錄
setDestinationUri(Uri uri)直接用 Uri 當存檔位置
setMimeType(String mimeType)設定檔案的 MimeType。下載完成要開檔案時,即會用這個 MimeType 開啟
setNotificationVisibility(int visibility)設定通知列是否顯示 Android Developer有說明傳入值為何
setShowRunningNotification(boolean show)deprecated in API level 11
setTitle(CharSequence title)通知訊息裡的標題
setVisibleInDownloadsUi(boolean isVisible)是否要在系統的下載頁面出現

在 setNotificationVisibility() 時,設定成 VISIBILITY_HIDDEN 時,要記得加入權限:

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

否則會出現錯誤訊息:

java.lang.SecurityException: Invalid value for visibility: 2