Android經常使用Manager

Android經常使用Manager


1. ActivityManager:與系統中正在運行的全部活動進行交互。


獲取ActivityManager對象的方法是在擁有context的環境下使用下面的方法。
ActivityManager activityManager= (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager經常使用的方法:segmentfault

  • getRunningAppProcesses():獲取系統中正在運行的全部的進程的信息。微信

  • getRunningServices:獲取系統中正在運行的全部的服務的信息。網絡

  • getMemoryInfo():獲取系統內存信息。ide

  • getProcessMemoryInfo:獲取某個或某幾個進程佔用的內存信息。ui

  • getDeviceConfigurationInfo():獲取設備的配置屬性。this

2. FragmentManager:在Activity中與Fragment進行交互的接口


獲取FragmentManager對象的方法是在Activity中使用下面的方法。
FragmentManager fragmentManager=getSupportFragmentManager();
AlarmManager經常使用方法:spa

  • getFragments():獲取FragmentManager中全部的Fragment。code

  • findFragmentById():經過id找到對應的Fragment。對象

  • beginTransaction():開啓FragmentManager的事務。blog

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個狀態中用的最多的狀態之一,該狀態下鬧鐘也是用絕對時間。

文章連接:http://www.javashuo.com/article/p-gyrtctrq-gn.html

歡迎關注個人微信公衆號:Android技術漫談

相關文章
相關標籤/搜索