android中getSystemService詳解

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   io   os   ar   


    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 thiscontext. 
  • ACTIVITY_SERVICE ("activity")
    A ActivityManager for interacting with the global activity state ofthe system. 
  • POWER_SERVICE ("power")
    A PowerManager for controlling powermanagement. 
  • ALARM_SERVICE ("alarm")
    A AlarmManager for receiving intents at the time of yourchoosing. 
  • NOTIFICATION_SERVICE ("notification")
    A NotificationManager for informing the user of backgroundevents. 
  • 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 vibratorhardware. 
  • CONNECTIVITY_SERVICE ("connection")
    A ConnectivityManager for handling management of networkconnections. 
  • WIFI_SERVICE ("wifi")
    A WifiManager for management of Wi-Ficonnectivity. 
  • INPUT_METHOD_SERVICE ("input_method")
    An InputMethodManager for management of inputmethods. 
  • 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 closelyassociated with the Context in which they are obtained from. Ingeneral, do not share the service objects between various differentcontexts (Activities, Applications, Services, Providers,etc.)  一個例子:

在android 擷取手機資訊的時候用到這樣一段代碼:

 

public class BasicInfo {


      public StringgetPhoneNumber()

      {

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

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

            StringBufferinf = new StringBuffer();

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

                    caseTelephonyManager.SIM_STATE_ABSENT :inf.append("無卡");returninf.toString();  

                   caseTelephonyManager.SIM_STATE_UNKNOWN :inf.append("未知狀態");returninf.toString(); 

                  caseTelephonyManager.SIM_STATE_NETWORK_LOCKED:inf.append("需要NetworkPIN解鎖");return inf.toString(); 

                   caseTelephonyManager.SIM_STATE_PIN_REQUIRED:inf.append("需要PIN解鎖");return inf.toString(); 

                   caseTelephonyManager.SIM_STATE_PUK_REQUIRED:inf.append("需要PUK解鎖");return inf.toString(); 

                  caseTelephonyManager.SIM_STATE_READY :break; 

      }

 

      String phoneNumber =tm.getLine1Number();

      returnphoneNumber;

}


在另外一個activity類裡面調用的時候 總是出現進程關閉 無法擷取手機資訊。後來發現


getSystemService這個方法基於context,只有存在TextView控制項的表單中這個方法才會被啟用~

 

於是:

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

public BasicInfo (Context context)

{

      this.context =context;

}

 

2. getPhoneNumber()函數裡面改成:

context.getSystemService(Context.TELEPHONY_SERVIC);

 

3. 在調用類裡面 BasicInfo bi = newBasicInfo(this);

bi.getPhoneNumber();

   
前一篇:SimpleDateFormat使用詳解後一篇:PendingIntent

android中getSystemService詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.