(轉)android中getSystemService詳解


http://blog.sina.com.cn/s/blog_71d1e4fc0100o8qr.html
http://blog.csdn.net/bianhaohui/article/details/6220135
 
    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 獲取手機信息的時候用到這樣一段代碼:

 

public class BasicInfo {

 

        public String getPhoneNumber()

        {

                // 獲取手機號 MSISDN,很可能為空

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

                StringBuffer inf = new StringBuffer();

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

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

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

                        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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM