《系统架构系列》常用的系统Manager及对应的ManagerService

来源:互联网 发布:钢铁雄心2手机版数据 编辑:程序博客网 时间:2024/06/11 02:38

前言

最近在看源码的时候,被各种XXXManager和XXXManagerService搞得有点懵,故而想整理整理这些常用的XXXManager和XXXManagerService,增强记忆。本文使用的是Android SDK 27,源码Android 8.0。

常用Manager

Manager 说明 ManagerService ActivityManager 提供与activities,services和containing process进行交互的功能 ActivityManagerService AlarmManager 提供访问系统警报服务的功能 AlarmManagerService AppWidgetManager 提供管理widget的相关功能 AppWidgetService ConnectivityManager 提供查询网络连接状态相关功能 ConnectivityService DownloadManager 提供处理长时间运行HTTP下载的服务 FragmentManager 提供在Activity中与之交互的接口 LocationManager 提供访问系统位置的服务 LocationManagerService NotificationManager 通知用户发生的事件,常常是后台事件 NotificationManagerService PackageManager 用于检索与当前安装在设备上的应用程序包相关的各种信息 PackageManagerService TelephonyManager 提供访问设备上的电话服务相关信息的功能 WindowManager 提供窗口管理器的接口 WindowManagerService

各部分功能

1.ActivityManager

SDK定义:This class gives information about, and interacts with, activities, services, and the containing process. 

根据SDK文档的说明,这个类提供了关于与activities、services和进程交互的一些信息。其中还提到这个类的大部分方法是用来调试的,并且应用开发者不需要使用到这个类。当然,对于一些特殊的情况,该类提供了一些方法给用户调用来获取必要的信息,后面会介绍几个常见的方法。
获取ActivityManager对象的方法是在拥有context的环境下使用:

ActivityManager activityManager= (ActivityManager) getSystemService(ACTIVITY_SERVICE);或者ActivityManager activityManager= (ActivityManager) getSystemService(ActivityManager.class);

ActivityManager常用的方法:

  • clearApplicationUserData:从磁盘中清除应用的数据,但是不会移除这个已经安装的应用;
  • getAppTasks:获取当前应用的task列表;
  • getDeviceConfigurationInfo:获取设备配置属性;
  • getMemoryInfo:获取系统内存信息;
  • getProcessMemoryInfo:获取一个或多个进程占用的内存信息;
  • killBackgroundProcesses:系统立刻杀死给定包里的所有后台进程,需要KILL_BACKGROUND_PROCESSES权限;
  • moveTaskToFront:将给定id的task移到前台,这时它将对用户可见,需要REORDER_TASKS权限;

而ActivityManager的大部分方法是通过getService()来是现实的,而getService()方法返回的是IActivityManager,而ActivityManagerNative实现了IActivityManager接口,接着会调用到ActivityManagerNative的getDefault(),它返回的是ActivityManagerService对象,这期间涉及了IPC过程。那么ActivityManager和ActivityManagerService就是通过这样一种方式建立了联系。如下图所示:
这里写图片描述

2.AlarmManager

SDK定义:This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. 

根据SDK文档的说明,这个类提供了访问系统警报服务的功能,它允许你设置你的应用在未来某一个时间去运行。AlarmManager会持有CPU的唤醒锁,直到它接收到了onReceive()方法,就会释放掉这个唤醒锁。
获取AlarmManager对象的方法是在拥有context的环境下使用:

AlarmManager alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE); 或者AlarmManager alarmManager= (AlarmManager) getSystemService(AlarmManager.class); 

AlarmManager常用的方法:

  • getNextAlarmClock:获取下一个闹钟的相关信息;
  • setAlarmClock:设置闹钟;
  • setRepeating:设置重复闹钟;需要注意的是,API 19以后使用重复闹钟是不精确的,如果需要精确的闹钟应当使用一次性闹钟;
  • setTime:设置系统闹钟,需要权限android.permission.SET_TIME;
  • setTimeZone:设置系统时区;

其中参数type有以下几种类型:

  • AlarmManager.ELAPSED_REALTIME:表示闹钟在手机睡眠状态下不可用,该状态下闹钟使用相对时间(相对于系统启动开始);
  • AlarmManager.ELAPSED_REALTIME_WAKEUP:表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟也使用相对时间;
  • AlarmManager.RTC:表示闹钟在睡眠状态下不可用,该状态下闹钟使用绝对时间,即当前系统时间;
  • AlarmManager.RTC_WAKEUP:表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用绝对时间;
  • AlarmManager.POWER_OFF_WAKEUP:表示闹钟在手机关机状态下也能正常进行提示功能,所以是5个状态中用的最多的状态之一,该状态下闹钟也是用绝对时间;

AlarmManagerService是AlarmManager真正的服务,它真正的实现了alarm的功能。而采用AlarmManager将应用与服务分割开来后,使得应用程序开发者不用关心具体的服务,而是直接通过AlarmManager来使用这种服务。这也许就是客户/服务模式的好处。AlarmManager与 AlarmManagerServie之间是通过Binder来通信的,他们之间是多对一的关系。

3.AppWidgetManager

SDK定义:Updates AppWidget state; gets information about installed AppWidget providers and other AppWidget related state.

获取AppWidgetManager对象的方法是在拥有context的环境下使用:

AppWidgetManager appWidgetManager = (AppWidgetManager) getSystemService(APPWIDGET_SERVICE);或者AppWidgetManager appWidgetManager = (AppWidgetManager) getSystemService(AppWidgetManager.class);

根据SDK文档的说明,这个类是用来更新AppWidget状态,并且获取有关已安装的AppWidget提供者和其他AppWidget相关状态的信息的。
AppWidgetManager常用的方法:

  • getAppWidgetIds:获取已绑定到给定AppWidget提供者的appWidgetIds列表;
  • getAppWidgetInfo:获取关于AppWidget的可用信息;
  • getInstance:获取当前Context对象对应的AppWidgetManager实例;
  • updateAppWidget:为提供的AppWidget提供者的所有AppWidget实例设置remoteview,该方法只有在拥有该AppWidget provider的uid调用才有效;

和其他ManagerService一样,AppWidgetService提供了具体的服务为AppWidgetManager。

4.ConnectivityManager

SDK定义:Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. 

获取ConnectivityManager对象的方法是在拥有context的环境下使用:

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);或者ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(ConnectivityManager.class);

根据SDK文档的说明,这个类提供了查询网络连接状态的功能,当网络连接状态改变的时候,它会提醒应用。当网络丢失时,它会试图转移到另一个网络。
ConnectivityManager常用的方法:

  • bindProcessToNetwork:将当前进程绑定到网络;
  • getActiveNetwork:返回和当前默认的数据连接相一致的网络对象,如果当前活动的默认数据网络断开连接,返回的网络对象将不再可用。当没有默认网络时,这将返回null。需要ACCESS_NETWORK_STATE权限;
  • getAllNetworks:获取一个网络连接数组;
  • requestNetwork:请求网络连接;

5.DownloadManager

SDK定义:The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.

根据SDK文档的说明,下载管理器是一个处理长时间运行的HTTP下载的系统服务。客户端可能要求将URI下载到特定的目标文件中,下载管理器将在后台进行下载,负责处理HTTP交互,在故障或跨连接更改和系统重新启动后重新尝试下载。通过这个API请求下载的应用程序应该注册一个广播接收器,当用户在一个通知或下载的UI中点击一个正在运行的下载时,它会被点击适当地处理。
获取DownloadManager对象的方法是在拥有context的环境下使用:

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);或者DownloadManager downloadManager = (DownloadManager) getSystemService(DownloadManager.class);

DownloadManager常用的方法:

  • enqueue:一个新的下载入队,一旦下载管理器准备好,且连接可用,就会自动执行下载;
  • getMimeTypeForDownloadedFile:返回下载文件的的media类型,下载失败的时候会返回null;
  • getUriForDownloadedFile:返回下载文件的uri,下载失败的时候会返回null;
  • openDownloadedFile:以reading的方式打开下载文件,此时文件必须已经下载完成;
  • remove:取消下载并且在DownloadManager中移除他们,如果正在下载那么会停止,如果是下载文件的部分或者全部,它将被删除;

6.FragmentManager

SDK定义:Interface for interacting with Fragment objects inside of an Activity.

根据SDK文档的说明,之后简短的一句,它是用于与活动内部的fragment对象进行交互的接口,这是我们很常用的一个manager,基本要使用到fragment的地方都需要用到,比较熟悉。
获取FragmentManager对象的方法是在拥有context的环境下使用:

FragmentManager fragmentManager = getFragmentManager();

FragmentManager常用的方法;

  • beginTransaction:在与此FragmentManager相关的fragment上启动一系列的编辑操作;
  • getFragment:返回当前fragment对象的实例;
  • getFragments:返回被添加到当前FragmentManager的一系列fragment,只有那些处于added和attached状态的才会被返回,不管是隐藏还是显示。

7.LocationManager

SDK定义:This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.

根据SDK文档的说明,这个类提供了对系统位置服务的访问。这些服务允许应用程序获取设备的地理位置的周期性更新,或者当设备进入给定的地理位置时,触发应用程序指定的Intent。
获取LocationManager对象的方法是在拥有context的环境下使用:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);或者LocationManager locationManager = (LocationManager) getSystemService(LocationManager.class);

LocationManager常用的方法:

  • getAllProviders:获取所有能提供位置服务的Provider,即使是不被允许访问或者当前不可以用的;
  • requestLocationUpdates:使用指定的提供者注册位置更新,以及一个pending intent;
  • removeUpdates:为制定的监听者移除位置更新;
    定位服务的主要功能由LocationManager以bind ipc的方式转给LocationManagerService完成。

8.NotificationManager

SDK定义:Class to notify the user of events that happen. This is how you tell the user that something has happened in the background. 

根据SDK文档的说明,这个类就是用来通知用户一些事件发生了,相对简单一些。
获取NotificationManager对象的方法是在拥有context的环境下使用:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);或者NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class);

NotificationManager常用的方法:

  • cancel:取消当前显示的通知,如果通知是短暂的,view将被隐藏;如果通知是常驻的,那么将会从状态栏中被移除;
  • notify:在状态栏中显示重要的通知,如果相同id的通知已经存在,那么将替换原先的通知;

主要的功能仍然由NotificationManagerService来完成。

9.PackageManager

SDK定义:Class for retrieving various kinds of information related to the application packages that are currently installed on the device.

根据SDK文档的说明,这个类用来检索与当前安装在设备上的应用程序包相关的各种信息。
获取PackageManager对象的方法是在拥有context的环境下使用:

PackageManager packageManager = getPackageManager();

PackageManager常用的方法:

  • checkPermission:检查一个特定的包是否获得了特定的许可;
  • getActivityInfo:获取关于给定activity的所有信息;
  • getAllPermissionGroups:获取系统所有的权限集合;
  • getApplicationInfo:获取关于给定package或application的所有信息;
  • getInstalledApplications:获取安装在设备上的所有应用;

PackageManagerService是Android framework框架核心服务,用于APK的解析、权限验证、安装等。

10.TelephonyManager

SDK定义:Provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes. 

根据SDK文档的说明,这个类提供了关于设备上的电话服务的信息。应用程序可以使用这个类中的方法来确定电话服务和状态,以及访问某些类型的订阅者信息。应用程序还可以注册一个侦听器来接收电话状态更改的通知。
获取TelephonyManager对象的方法是在拥有context的环境下使用:

TelephonyManager telephonyManager= (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 

TelephonyManager常用的方法:

  • getCallState:返回电话状态。
    TelephonyManager.CALL_STATE_IDLE 无任何状态时
    TelephonyManager.CALL_STATE_OFFHOOK 接起电话时
    TelephonyManager.CALL_STATE_RINGING 电话进来时
  • getDataActivity:获取数据活动状态。
    TelephonyManager.DATA_ACTIVITY_IN 活动,正在接受数据
    TelephonyManager.DATA_ACTIVITY_OUT 活动,正在发送数据
    TelephonyManager.DATA_ACTIVITY_INOUT 活动,正在接受和发送数据
    TelephonyManager.DATA_ACTIVITY_NONE 活动,但无数据发送和接受
  • getDataNetworkType:获取当前设备所使用的网络数据类型;
  • getSimState:获取sim卡状态;

11.WindowManager

SDK定义:The interface that apps use to talk to the window manager. 

根据SDK文档的说明,这是一个应用程序用来和窗口管理器进行交互的接口。
获取WindowManager对象的方法是在拥有context的环境下使用:

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);或者WindowManager windowManager = (WindowManager) getSystemService(WindowManager.class);

WindowManager常用的方法:

  • addView:给当前Window添加一个view;
  • updateViewLayout:更新window当前view的布局;
  • removeView:删除当前window的view;

总结

常用的系统Manager基本就这些,当然除了上述常见Service之外还有PowerManager、KeyguardManager、StorageManager、MediaRouter、SubscriptionManager、CarrierConfigManager、JobScheduler、NetworkStatsManager、HardwarePropertiesManager等等,这里就不再一一总结,以后需要用到再补充好了。