android中getSystemService详解

摘要:
GetSystemService是Android的重要API。这是一种活动方法。它根据传入的NAME获取相应的对象,然后将其转换为相应的服务对象。以下描述了系统的相应服务。
http://blog.sina.com.cn/s/blog_71d1e4fc0100o8qr.html
http://blog.csdn.net/bianhaohui/article/details/6220135
 
    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详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇FreeRTOS-Qemu 实现三任务同步通信机制以及API信息sqlalchemy 单表增删改查下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android自定义组合控件

今天和大家分享下组合控件的使用。很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法。今天就来介绍下如何使用组合控件,将通过两个实例来介绍。第一个实现一个带图片和文字的按钮,如图所示: 整个过程可以分四步走。第一步,定义一个lay...

Spring AOP之使用注解创建切面

上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象。那么现在就让我们使用AspectJ注解来定义切面吧。 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audience: package com.spring.aop.service.aop; import org.aspectj.lang.ProceedingJoi...

谷歌浏览器 html5的声音和视频不能自动播放处理

AI模型开发就选MindSpore!新特性、新工具上线!>>> 声音无法自动播放这个在IOS/Android上面一直是个惯例,桌面版的Safari在2017年的11版本也宣布禁掉带有声音的多媒体自动播放功能,紧接着在2018年4月份发布的Chrome 66也正式关掉了声音自动播放,也就是说<audio autopaly><...

Android 设置Activity样式 透明度

一、设置Activity透明度有几种方法:1>.在清单文件中配置Activity时声明android:theme="@android:style/Theme.Translucent"2>.使用自定义主题,先看看自定义主题中需要用到的一些属性设置说明 <style name="custom"parent="@android:style/Th...

Android开发之查看应用包名package和入口activity名称的方法

使用android自动化测试工具monkeyrunner启动应用时,需要填写被测程序的包名和启动的Activity,以下有两种查看应用包名package和入口activity名称的方法:方法一:使用aapt //aapt是sdk自带的一个工具,在sdkuilds-tools目录下1.以ES文件浏览器为例,命令行中切换到aapt.exe目录执行:aapt d...

【原】iOS学习之Quartz2D(1)

什么是Quartz2D  1、Quartz 2D 是一个二维绘图引擎,同时支持iOS和Mac系统  2、Quartz 2D 能完成的工作: 绘制图形 : 线条三角形矩形圆弧等 绘制文字 绘制生成图片(图像) 读取生成PDF 截图裁剪图片 自定义UI控件 图形上下文  1、图形上下文(Graphics Context):是一个 CGContextRef...