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

引自:http://blog.sina.com.cn/s/blog_7cb2c5d50101c26t.html

Android——getSystemService的更多相关文章

  1. Android -- getSystemService

    Android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardServic ...

  2. Android操作系统服务(Context.getSystemService() )

    getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.下面介绍系统相应的服务: 传入 ...

  3. android中getSystemService详解

        android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...

  4. Android:getContext().getSystemService()

    一.介绍 getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象 二.语法 Windo ...

  5. Android Service GetSystemService

    http://blog.sina.com.cn/s/blog_71d1e4fc0100o8qr.html http://blog.csdn.net/bianhaohui/article/details ...

  6. Android之getSystemService

    getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.以下介绍系统相应的服务. 传入 ...

  7. android window(二)从getSystemService到WindowManagerGlobal

    在Activity调用getSystemService(WINDOW_SERVICE) 调用的是父类ContextThemeWrapper package android.view; public c ...

  8. 【Android】 context.getSystemService()浅析

    同事在进行code review的时候问到我context中的getSystemService方法在哪实现的,他看到了一个ClipBoardManager来进行剪切板存储数据的工具方法中用到了cont ...

  9. Android指纹解锁

    Android6.0及以上系统支持指纹识别解锁功能:项目中用到,特此抽离出来,备忘. 功能是这样的:在用户将app切换到后台运行(超过一定的时长,比方说30秒),再进入程序中的时候就会弹出指纹识别的界 ...

随机推荐

  1. PO、VO、BO、DTO

    J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...

  2. mongodb不同版本之间有很大的差异

    今天主要说下我为了给mongodb数据库添加authorization,大家应该知道,mongo默认是无auth运行的.这可能是方便小伙伴学习命令吧. 由于之前发布的一个项目,在亚马逊的云上,处于内部 ...

  3. mysql批量更新

    UPDATE ta INNER JOIN tb ON ta.id=tb.id SET ta.col1=tb.col1, ta.col2=tb.col2 以上代码用来批量更新mysql中的记录

  4. JS如何调用隐藏按钮的click事件

    js如何调用隐藏按钮的click事件:1.设定隐藏不要使用Visiable属性,使用style.display=none:2.触发JS事件可以使用fireEvent方法,如:document.getE ...

  5. 【initrd】向虚拟文件系统initrd.img中添加驱动

    虚拟文件系统:initrd-2.6.18-194.el5.img 希望添加网卡或SCSI等驱动 步骤: 解压initrd-2.6.18-194.el5.img: 添加*.ko文件,并修改init可执行 ...

  6. 使用Cyclone IV控制DDR2

    根据你的DDR2手册配置好megacore,megacore会生成一个example top: 在quartus中运行megacore生成的xxx_pin_assignments.tcl,指定DDR2 ...

  7. 【转】Redis主从复制简介

    一.Redis的Replication:    这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了.相信在阅读完这篇Blog之后你也可以轻松做到.这里我们还是先列出一些理 ...

  8. 深度优化LNMP

    优化前准备工作 Centos准备及配置 准备安装包及软件:http://pan.baidu.com/s/1chHQF  下载解压到U盘即可安装http://pan.baidu.com/s/15TUWf ...

  9. onNewIntent调用时机

    在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestroy  onNewIntent 一 ...

  10. nginx软负载的搭建

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,在高连接并发的情况下Nginx 是 Apa ...