天天看點

Android Service GetSystemService

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類裡面調用的時候 總是出現程序關閉 無法擷取手機資訊。後來發現

<code>getsystemservice這個方法基于context,隻有存在textview控件的窗體中這個方法才會被激活~</code>

于是:

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