android中getSystemService详解
2015-09-24 18:35
344 查看
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类里面调用的时候 总是出现进程关闭 无法获取手机信息。后来发现
于是:
1. 给BasicInfo添加一个带参数Context的构造函数:
public BasicInfo (Context context)
{
this.context =context;
}
2. getPhoneNumber()函数里面改成:
context.getSystemService(Context.TELEPHONY_SERVIC);
3. 在调用类里面 BasicInfo bi = newBasicInfo(this);
bi.getPhoneNumber();
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();
相关文章推荐
- Android开发艺术探索读书笔记(一)
- Android的权限permission
- android避免大量图片内存泄露问题
- Android Studio 添加工程依赖(更新中)
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- Android性能专项测试之Memory Monitor工具
- Android性能优化
- 下载Android 源码(高墙之下,只能用镜像啦)
- Android电话拨打流程源码分析
- Android页面初始化时让控件失去焦点
- android selector 控件
- Android中的音乐播放
- android oom 全解析
- 如何反编译 android 中 /data/dalvik-cache/arm 下的文件
- android 读取远程html内容
- Android : GestureDetector手势检测
- Android ant自动打包 crunch 报错
- android 事件总线 -- Otto(三) ThreadEnforcer
- Android release版关闭日志logcat
- android:layout_weight的真实含义