Android getSystemService()的API

来源:互联网 发布:千牛mac登不上去 编辑:程序博客网 时间:2024/05/17 21:59
概述:
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.)

举例:
1.网络服务:
boolean flag=false;ConnectivityManager mConManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);if(mConManager .getActiveNetworkInfo() != null){flag = mConManager.getActiveNetworkInfo().isAvailable();}
这是网络是否连接的判断

2.电话服务:
TelephonyManager deviceinfo = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);String telephone_number = deviceinfo.getLine1Number();String telephone_imei = deviceinfo.getDeviceId();
这是获取电话号码和imei号信息



0 0