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

   

android中getSystemService详解的更多相关文章

  1. Android中Context详解 ---- 你所不知道的Context(转)

    Android中Context详解 ---- 你所不知道的Context(转)                                               本文出处 :http://b ...

  2. Android中Context详解 ---- 你所不知道的Context

    转自:http://blog.csdn.net/qinjuning/article/details/7310620Android中Context详解 ---- 你所不知道的Context 大家好,  ...

  3. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

  4. Android中Handle详解

    上图为本人总结的Handler,网上发现一片总结很好的博客就copy过来:作为参考 Handler有何作用?如何使用? 一 .Handler作用和概念 包含线程队列和消息队列,实现异步的消息处理机制, ...

  5. Android中Context详解

    大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiver.A ...

  6. 转:Android中Context详解 ---- 你所不知道的Context

    转:http://blog.csdn.net/qinjuning/article/details/7310620 转:http://blog.csdn.net/lmj623565791/article ...

  7. android中LayoutInflater详解与使用

    android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局.inflater即为填充的意思,也就是说 ...

  8. android中Actionbar详解

    1.什么是Action BarAction Bar被认为是新版Android系统中最重要的交互元素,在程序运行中一直置于顶部,主要起到的作用在于:1)突出显示一些重要操作(如“最新”.“搜索”等)2) ...

  9. Android中IntentService详解

    简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service ...

随机推荐

  1. 团队项目之Sprint计划会议

    一.我们团队在4月15日进行了冲刺计划会议,会议过程大致如下: 1.总结目前的工作进展,再一次确定所做项目的方向: 2.将之前的调查问卷的结果进行统计,做了需求分析,大致了解了用户的想法: 3.根据初 ...

  2. OpenWrt刷机后LAN口无法连通的问题

    [路由器开发板硬件固件配置] MTK双频:MT7620a + MT7612e 内存:256 MB 闪存:16 MB 固件:MTK自带SDK中的OpenWrt固件(mtksdk-openwrt-2.6. ...

  3. BZOJ 4302 Buildings 解题报告

    这个题好像很有趣的样子. 题目分析: 房间都是 $1\times k$ 的,也就是一条一条的.这个好像比较显然的样子. 一个房间如果要覆盖某个格子$u$,那么这个房间的面积至少为 $dis(u, Bo ...

  4. MVC4 错误: 检测到有潜在危险的 Request.Form值

    说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止.该值可能指示存在危及应用程序安全的尝试,如跨站点脚本攻击.若要允许页面重写应用程序请求验证设置,请将 httpRuntime  ...

  5. 【WCF--初入江湖】13 实战

    13 实战 在线升级 using System; using System.Collections.Generic; using System.ComponentModel; using System ...

  6. 01-04-02【Nhibernate (版本3.3.1.4000) 出入江湖】HQL查询

    public IList<Customer> GetAllHql() { IList<Customer> result = null; ISession session = _ ...

  7. EasyTouch 3.1中文翻译

    Unity3D的Easy Touch 的手册最近寻找中文版本,google无果,自己动手.目前暂时只有c# ,javascript原理是一样的. 一.Quick Start 1-Import Easy ...

  8. C#中dynamic的正确用法 以及 typeof(DynamicSample).GetMethod("Add");

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  9. cojs 简单的区间问题 解题报告

    新学了些弦图和区间图的新玩意,于是就想着出一道题目 其实这道题不用弦图和区间图的理论也是可以做的 首先考虑第一问,第一问是一个NOIP普及组水平的贪心 我们把区间按照右端点从小到大排序,之后从头到尾扫 ...

  10. 从SQL Server中导入/导出Excel的基本方法(转)

    从sql server中导入/导出 excel 的基本方法 /*=========== 导入/导出 excel 的基本方法 ===========*/ 从excel文档中,导入数据到sql数据库中,很 ...