1. ActivityManager:與系統中正在運行的所有活動進行交互。
獲取ActivityManager對象的方法是在擁有context的環境下使用下面的方法: ActivityManager activityManager= (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager常用的方法:
getRunningAppProcesses()
:獲取系統中正在運行的所有的進程的信息。
getRunningServices
:獲取系統中正在運行的所有的服務的信息。
getMemoryInfo()
:獲取系統內存信息。
getProcessMemoryInfo
:獲取某個或某幾個進程占用的內存信息。
getDeviceConfigurationInfo()
:獲取設備的配置屬性。
2. FragmentManager:在Activity中與Fragment進行交互的接口
獲取FragmentManager對象的方法是在Activity中使用下面的方法: FragmentManager fragmentManager=getSupportFragmentManager();
AlarmManager常用方法:
getFragments()
:獲取FragmentManager中所有的Fragment。
findFragmentById()
:通過id找到對應的Fragment。
beginTransaction()
:開啟FragmentManager的事務。
FragmentManager只能直接對Fragment進行查詢操作,不能直接進行增加,刪除,更新操作,要進行這些操作必須在FragmentManager開啟的事務中進行。開啟的事務的任務都完成后要提交事務。
fragmentManager.beginTransaction().replace(R.id.fragment,fragment).commit();
3. PackageManager:檢索當前安裝在設備上的應用程序包相關的各種信息
獲取PackageManager對象的方法是在在擁有context的環境下使用下面的方法: PackageManager packageManager=getPackageManager();
PackageManager常用的方法:
getInstalledApplications()
:返回在設備上安裝的所有應用程序包的列表。
getInstalledPackages()
:返回在設備上安裝的所有包的列表。
getActivityInfo()
:獲取對應組件名的Activity的信息。
注意其實PackageManager是一個抽象類。
4. DownloadManager:下載管理器是一個系統服務,處理長時間運行的HTTP下載
DownloadManager的基本使用方法:
/** * DownloadManager的基本使用 */ public void downloadManager(){ //獲取系統服務的DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE); //創建一個DownloadManager的請求 DownloadManager.Request request=new DownloadManager.Request(Uri.parse("http://i.imgur.com/iXgyWbG.png")); //設置請求允許的聯網方式:移動網絡與wifi都可以 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE| DownloadManager.Request.NETWORK_WIFI); //禁止發出通知,既后臺下載 //request.setShowRunningNotification(false);該方法被setNotificationVisibility取代了 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); //顯示下載界面 request.setVisibleInDownloadsUi(true); //設置下載后文件存放的位置,存放在/sdcard/Android/data/<包名>/files/Pictures目錄下面 request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_PICTURES, "iXgyWbG.png"); //將下載請求放入隊列 downloadManager.enqueue(request); }
5. ConnectivityManager:關于網絡連接狀態的查詢的類
ConnectivityManager的主要作用是:
監視網絡連接(Wi-Fi、GPRS、UMTS、等)
當網絡連通性的變化發送廣播意圖
當連接的網絡丟失,會自動連接別的網絡
提供一個允許應用程序查詢可用網絡的粗粒度或細粒度的應用程序接口
提供一個允許應用程序請求和選擇網絡的應用程序的接口
ConnectivityManager的基本使用方法:
/** * 檢測當的網絡(WLAN、3G/2G)狀態 * @param context Context * @return true 表示至少有一種網絡處于連接狀態 */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { // 當前網絡是連接的 if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } return false; }
6. WindowManager:應用程序使用的界面和窗口管理器
WindowManager是一個接口,基本使用方法如下:
WindowManager windowManager=getWindowManager(); //向windowManager中添加視圖 windowManager.addView(view); //刪除windowManager中的視圖 windowManager.removeView(view);
7. NotificationManager:通知用戶發生的事件
NotificationManager的基本使用方法:
NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE); PendingIntent intent=PendingIntent.getActivity(this,0,new Intent(MainActivity.this,MainActivity.class),0); Notification notification=new NotificationCompat.Builder(this) .setTicker("有新通知了") .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("通知標題") .setContentText("通知內容") .setWhen(0) .setContentIntent(intent) .build(); notificationManager.notify(0,notification);
8. TelephonyManager:提供訪問設備上的電話服務的信息
獲取TelephonyManager對象的方法是在擁有context的環境下使用下面的方法: TelephonyManager telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
TelephonyManager常用方法:
getCallState()
:返回電話狀態。
TelephonyManager.CALL_STATE_IDLE
無任何狀態時
TelephonyManager.CALL_STATE_OFFHOOK
接起電話時
TelephonyManager.CALL_STATE_RINGING
電話進來時
getCellLocation()
:獲取當前電話的位置
getDataActivity()
:獲取數據活動狀態
TelephonyManager.DATA_ACTIVITY_IN
活動,正在接受數據
TelephonyManager.DATA_ACTIVITY_OUT
活動,正在發送數據
TelephonyManager.DATA_ACTIVITY_INOUT
活動,正在接受和發送數據
TelephonyManager.DATA_ACTIVITY_NONE
活動,但無數據發送和接受
getDeviceId()
:返回設備id(當前移動終端的唯一標識)
getLine1Number()
:返回手機號碼
9. LocationManager:提供了系統位置服務的訪問
獲取LocationManager對象的方法是在擁有context的環境下使用下面的方法: LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationManager常用方法:
getAllProviders()
:獲取所有能提供位置服務的Provider
getLastKnownLocation()
:獲取上次開啟位置服務記錄的位置
requestLocationUpdates()
:注冊位置更新的監聽者
10. AlarmManager:提供系統報警服務的訪問
獲取AlarmManager對象的方法是在擁有context的環境下使用下面的方法: AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager常用方法:
setTime(long millis)
:設置系統時鐘時間。
setTimeZone(String timeZone)
:設置系統時鐘時區。
setAlarmClock(AlarmClockInfo info, PendingIntent operation)
:設置一個警報來代表鬧鐘。
set(int type, long triggerAtMillis, PendingIntent operation)
:設置一次性鬧鐘,第一個參數表示鬧鐘類型,第二個參數表示鬧鐘執行時間,第三個參數表示鬧鐘響應動作。
setRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation)
:設置重復鬧鐘,第一個參數表示鬧鐘類型,第二個參數表示鬧鐘首次執行時間,第三個參數表示鬧鐘兩次執行的間隔時間,第三個參數表示鬧鐘響應動作。
setInexactRepeating(int type, long triggerAtMillis,long intervalMillis, PendingIntent operation)
:設置重復鬧鐘,與setRepeating
方法類似,區別是setRepeating
鬧鐘兩次執行的間隔時間固定,而setInexactRepeating
鬧鐘兩次執行的間隔時間不固定。
上述方法中的type
參數有五種值:
AlarmManager.ELAPSED_REALTIME
:表示鬧鐘在手機睡眠狀態下不可用,該狀態下鬧鐘使用相對時間(相對于系統啟動開始)。
AlarmManager.ELAPSED_REALTIME_WAKEUP
:表示鬧鐘在睡眠狀態下會喚醒系統并執行提示功能,該狀態下鬧鐘也使用相對時間。
AlarmManager.RTC
:表示鬧鐘在睡眠狀態下不可用,該狀態下鬧鐘使用絕對時間,即當前系統時間。
AlarmManager.RTC_WAKEUP
:表示鬧鐘在睡眠狀態下會喚醒系統并執行提示功能,該狀態下鬧鐘使用絕對時間。
AlarmManager.POWER_OFF_WAKEUP
:表示鬧鐘在手機關機狀態下也能正常進行提示功能,所以是5個狀態中用的最多的狀態之一,該狀態下鬧鐘也是用絕對時間。