android中getSystemService詳解

my.oschina.net/yuanxulong/blog/323173
android的後臺運行在不少service,它們在系統啓動時被SystemServer開啓,支持系統的正常工做,好比MountService監聽是否有SD卡安裝及移除,ClipboardService提供剪切板功能,PackageManagerService提供軟件包的安裝移除及查看等等,應用程序能夠經過系統提供的Manager接口來訪問這些Service提供的數據。

      getSystemService是Android很重要的一個API,它是Activity的一個方法,根據傳入的NAME來取得對應的Object,而後轉換成相應的服務對象。如下介紹系統相應的服務。

                傳入的Name                |                返回的對象                    |             說明
  • WINDOW_SERVICE                      WindowManager                    管理打開的窗口程序

  • LAYOUT_INFLATER_SERVICE             LayoutInflater                   取得xml裏定義的view

  • ACTIVITY_SERVICE                    ActivityManager                  管理應用程序的系統狀態

  • POWER_SERVICE                       PowerManger                      電源的服務

  • ALARM_SERVICE                       AlarmManager                     鬧鐘的服務

  • NOTIFICATION_SERVICE                NotificationManager              狀態欄的服務

  • KEYGUARD_SERVICE                    KeyguardManager                  鍵盤鎖的服務

  • LOCATION_SERVICE                    LocationManager                  位置的服務,如GPS

  • SEARCH_SERVICE                      SearchManager                    搜索的服務

  • VEBRATOR_SERVICE                    Vebrator                         手機震動的服務

  • CONNECTIVITY_SERVICE                Connectivity                     網絡鏈接的服務

  • WIFI_SERVICE                        WifiManager                      Wi-Fi服務

  • TELEPHONY_SERVICE                   TeleponyManager                  電話服務


Currently available names are:
  • WINDOW_SERVICE ("window") 
    The top-level window manager in which you can place custom windows. The returned object is a WindowManager. 

  • LAYOUT_INFLATER_SERVICE ("layout_inflater")
    A LayoutInflater for inflating layout resources in this context. 

  • ACTIVITY_SERVICE ("activity")
    A ActivityManager for interacting with the global activity state of the system. 

  • POWER_SERVICE ("power")
    A PowerManager for controlling power management. 

  • ALARM_SERVICE ("alarm")
    A AlarmManager for receiving intents at the time of your choosing. 

  • NOTIFICATION_SERVICE ("notification")
    A NotificationManager for informing the user of background events. 

  • KEYGUARD_SERVICE ("keyguard")
    A KeyguardManager for controlling keyguard. 

  • LOCATION_SERVICE ("location")
    A LocationManager for controlling location (e.g., GPS) updates. 

  • SEARCH_SERVICE ("search")
    A SearchManager for handling search. 

  • VIBRATOR_SERVICE ("vibrator")
    A Vibrator for interacting with the vibrator hardware. 

  • CONNECTIVITY_SERVICE ("connection")
    A ConnectivityManager for handling management of network connections. 

  • WIFI_SERVICE ("wifi")
    A WifiManager for management of Wi-Fi connectivity. 

  • INPUT_METHOD_SERVICE ("input_method")
    An InputMethodManager for management of input methods. 

  • UI_MODE_SERVICE ("uimode")
    An UiModeManager for controlling UI modes. 

  • DOWNLOAD_SERVICE ("download")
    A DownloadManager for requesting HTTP downloads

Note: System services obtained via this API may be closely associated with the Context in which they are obtained from. In general, do not share the service objects between various different contexts (Activities, Applications, Services, Providers, etc.)

一個例子:

在android 獲取手機信息的時候用到這樣一段代碼: android

 

public class BasicInfo { windows


        public String getPhoneNumber() 網絡

        { app

                // 獲取手機號 MSISDN,極可能爲空 ide

                TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 函數

                StringBuffer inf = new StringBuffer(); ui

                switch(tm.getSimState()){ //getSimState()取得sim的狀態  有下面6中狀態   this

                        case TelephonyManager.SIM_STATE_ABSENT :inf.append("無卡");return inf.toString();    spa

                        case TelephonyManager.SIM_STATE_UNKNOWN :inf.append("未知狀態");return inf.toString();  .net

                        case TelephonyManager.SIM_STATE_NETWORK_LOCKED :inf.append("須要NetworkPIN解鎖");return inf.toString();  

                        case TelephonyManager.SIM_STATE_PIN_REQUIRED :inf.append("須要PIN解鎖");return inf.toString();  

                        case TelephonyManager.SIM_STATE_PUK_REQUIRED :inf.append("須要PUK解鎖");return inf.toString();  

                        case TelephonyManager.SIM_STATE_READY :break;  

        }

 

        String phoneNumber = tm.getLine1Number();

        return phoneNumber;

}


在另一個activity類裏面調用的時候 老是出現進程關閉 沒法獲取手機信息。後來發現


getSystemService這個方法基於context,只有存在TextView控件的窗體中這個方法纔會被激活~

 

因而:

1. 給BasicInfo 添加一個帶參數Context的構造函數:

public BasicInfo (Context context)

{

        this.context = context;

}

 

2. getPhoneNumber()函數裏面改爲:

context.getSystemService(Context.TELEPHONY_SERVIC);

 

3. 在調用類裏面 BasicInfo bi = new BasicInfo(this);

bi.getPhoneNumber();

相關文章
相關標籤/搜索