Android P系统服务接口调用原理

本文探讨了在Android P中如何调用系统服务,特别是如何获取和使用StorageManager来访问存储服务。通过getSystemService()方法在Context.java和ContextImpl.java中解析,详细阐述了SystemServiceRegistry如何注册和管理系统服务。特别强调了CachedServiceFetcher在创建并返回StorageManager实例中的作用,使得应用程序能够顺利获取到服务接口并进行操作。

1、调用系统服务方式
通过以下方式获取StorageManager的实例对象,通过StorageManager调用系统存储服务。
StorageManager是系统存储服务的接口。
getVolumes()可返回系统上可用卷的集合,例如USB设备。

StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
List<VolumeInfo> volInfoList = storageManager.getVolumes();

2、getSystemService()方法解析
/framework/base/core/java/android/content/Context.java
从下述的注释中可以看到各种系统级服务的Manager。通过名称可获取对应的系统级服务句柄。

    /**
     * Return the handle to a system-level service by name. The class of the
     * returned object varies by the requested name. Currently available names
     * are:
     *
     * <dl>
     *  <dt> {@link #WINDOW_SERVICE} ("window")
     *  <dd> The top-level window manager in which you can place custom
     *  windows.  The returned object is a {@link android.view.WindowManager}.
     *  <dt> {@link #LAYOUT_INFLATER_SERVICE} ("layout_inflater")
     *  <dd> A {@link android.view.LayoutInflater} for inflating layout resources
     *  in this context.
     *  <dt> {@link #ACTIVITY_SERVICE} ("activity")
     *  <dd> A {@link android.app.ActivityManager} for interacting with the
     *  global activity state of the system.
     *  <dt> {@link #POWER_SERVICE} ("power")
     *  <dd> A {@link android.os.PowerManager} for controlling power
     *  management.
     *  <dt> {@link #ALARM_SERVICE} ("alarm")
     *  <dd> A {@link android.app.AlarmManager} for receiving intents at the
     *  time of your choosing.
     *  <dt> {@link #NOTIFICATION_SERVICE} ("notification")
     *  <dd> A {@link android.app.NotificationManager} for informing the user
     *   of background events.
     *  <dt> {@link #KEYGUARD_SERVICE} ("keyguard")
     *  <dd> A {@link android.app.KeyguardManager} for controlling keyguard.
     *  <dt> {@link #LOCATION_SERVICE} ("location")
     *  <dd> A {@link android.location.LocationManager} for controlling location
     *   (e.g., GPS) updates.
     *  <dt> {@link #SEARCH_SERVICE} ("search")
     *  <dd> A {@link android.app.SearchManager} for handling search.
     *  <dt> {@link #VIBRATOR_SERVICE} ("vibrator")
     *  <dd> A {@link android.os.Vibrator} for interacting with the vibrator
     *  hardware.
     *  <dt> {@link #CONNECTIVITY_SERVICE} ("connection")
     *  <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for
     *  handling management of network connections.
     *  <dt> {@link #IPSEC_SERVICE} ("ipsec")
     *  <dd> A {@link android.net.IpSecManager IpSecManager} for managing IPSec on
     *  sockets and networks.
     *  <dt> {@link #WIFI_SERVICE} ("wifi")
     *  <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of Wi-Fi
     *  connectivity.  On releases before NYC, it should only be obtained from an application
     *  context, and not from any other derived context to avoid memory leaks within the calling
     *  process.
     *  <dt> {@link #WIFI_AWARE_SERVICE} ("wifiaware")
     *  <dd> A {@link android.net.wifi.aware.WifiAwareManager WifiAwareManager} for management of
     * Wi-Fi Aware discovery and connectivity.
     *  <dt> {@link #WIFI_P2P_SERVICE} ("wifip2p")
     *  <dd> A {@link android.net.wifi.p2p.WifiP2pManager WifiP2pManager} for management of
     * Wi-Fi Direct connectivity.
     * <dt> {@link #INPUT_METHOD_SERVICE} ("input_method")
     * <dd> An {@link android.view.inputmethod.InputMethodManager InputMethodManager}
     * for management of input methods.
     * <dt> {@link #UI_MODE_SERVICE} ("uimode")
     * <dd> An {@link android.app.UiModeManager} for controlling UI modes.
     * <dt> {@link #DOWNLOAD_SERVICE} ("download")
     * <dd> A {@link android.app.DownloadManager} for requesting HTTP downloads
     * <dt> {@link #BATTERY_SERVICE} ("batterymanager")
     * <dd> A {@link android.os.BatteryManager} for managing battery state
     * <dt> {@link #JOB_SCHEDULER_SERVICE} ("taskmanager")
     * <dd>  A {@link android.app.job.JobScheduler} for managing scheduled tasks
     * <dt> {@link #NETWORK_STATS_SERVICE} ("netstats")
     * <dd> A {@link android.app.usage.NetworkStatsManager NetworkStatsManager} for querying network
     * usage statistics.
     * <dt> {@link #HARDWARE_PROPERTIES_SERVICE} ("hardware_properties")
     * <dd> A {@link android.os.HardwarePropertiesManager} for accessing hardware properties.
     * </dl>
     *
     * <p>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.)
     *
     * <p>Note: Instant apps, for which {@link PackageManager#isInstantApp()} returns true,
     * don't have access to the following system services: {@link #DEVICE_POLICY_SERVICE},
     * {@link #FINGERPRINT_SERVICE}, {@link #SHORTCUT_SERVICE}, {@link #USB_SERVICE},
     * {@link #WALLPAPER_SERVICE}, {@link #WIFI_P2P_SERVICE}, {@link #WIFI_SERVICE},
     * {@link #WIFI_AWARE_SERVICE}. For these services this method will return <code>null</code>.
     * Generally, if you are running as an instant app you should always check whether the result
     * of this method is null.
     *
     * @param name The name of the desired service.
     *
     * @return The service or null if the name does not exist.
     *
     * @see #WINDOW_SERVICE
     * @see android.view.WindowManager
     * @see #LAYOUT_INFLATER_SERVICE
     * @see android.view.LayoutInflater
     * @see #ACTIVITY_SERVICE
     * @see android.app.ActivityManager
     * @see #POWER_SERVICE
     * @see android.os.PowerManager
     * @see #ALARM_SERVICE
     * @see android.app.AlarmManager
     * @see #NOTIFICATION_SERVICE
     * @see android.app.NotificationManager
     * @see #KEYGUARD_SERVICE
     * @see android.app.KeyguardManager
     * @see #LOCATION_SERVICE
     * @see android.location.LocationManager
     * @see #SEARCH_SERVICE
     * @see android.app.SearchManager
     * @see #SENSOR_SERVICE
     * @see android.hardware.SensorManager
     * @see #STORAGE_SERVICE
     * @see android.os.storage.StorageManager
     * @see #VIBRATOR_SERVICE
     * @see android.os.Vibrator
     * @see #CONNECTIVITY_SERVICE
     * @see android.net.ConnectivityManager
     * @see #WIFI_SERVICE
     * @see android.net.wifi.WifiManager
     * @see #AUDIO_SERVICE
     * @see android.media.AudioManager
     * @see #MEDIA_ROUTER_SERVICE
     * @see android.media.MediaRouter
     * @see #TELEPHONY_SERVICE
     * @see android.telephony.TelephonyManager
     * @see #TELEPHONY_SUBSCRIPTI
<think>我们正在处理关于Android P系统流量统计接口的查询。根据用户的问题和提供的引用,我们需要总结出Android P系统中的流量统计方法。 从引用[2]中我们得知:从Android 9(即Android P)开始,设备必须使用基于eBPF的网络流量监控记帐模块,而不是旧的xt_qtaguid。 引用[1]中提到的TrafficStats类,在Android P及更高版本中,其底层实现可能已经改为使用eBPF。因此,虽然API接口(如getTotalRxBytes, getMobileRxBytes等)可能保持不变,但底层机制已经改变。 因此,我们可以告诉用户,在Android P系统中,流量统计的接口仍然是使用TrafficStats类提供的方法,但是底层实现已经改为基于eBPF。 同时,我们注意到引用[4]提到了Android 2.2版本之前的流量统计,但这与Android P无关,因此我们主要关注引用[1]和[2]。 所以,回答应包含: 1.Android P中,仍然可以使用TrafficStats类来获取流量统计信息。 2. 底层实现从xt_qtaguid切换到了eBPF,这提高了灵活性和可维护性。 另外,用户可能需要知道如何使用这些接口,我们可以给出一个简单的示例。 注意:根据系统指令,我们需要使用正确的LaTeX格式(虽然这个问题可能涉及较少数学公式,但如果有变量或代码,要正确标注),并使用中文回答,结构清晰。 同时,根据要求,我们需要生成相关问题。 计划回答结构: 1. 直接回答:Android P系统流量统计接口仍然是TrafficStats类提供的方法。 2. 解释底层变化:从Android P开始使用eBPF代替xt_qtaguid。 3. 列举TrafficStats类中常用的方法(参考引用[1])。 4. 给出一个简单的代码示例(使用Java或Kotlin)展示如何获取总接收流量和发送流量。 5. 生成相关问题。 注意:引用标记的使用。我们在引用[1]和[2]的地方添加引用标识。 代码部分,由于是Android开发,我们使用Java或Kotlin。这里选择Java,因为更通用。 但是,我们也要注意,用户可能想知道eBPF带来的变化,但用户的问题主要是关于接口,所以我们重点放在接口上。 </think> ### Android P 系统流量统计接口详解 在 Android P (Android 9) 及以上版本中,流量统计接口的核心变化是底层监控架构的升级。主要特点如下: 1. **基础架构升级** Android P 使用基于 **eBPF (extended Berkeley Packet Filter)** 的流量监控系统取代了传统的 `xt_qtaguid` 模块。该架构具备更强的灵活性和可维护性,且无需外部内核代码支持[^2]。 2. **API 接口层** 应用层仍通过 `TrafficStats` 类获取流量数据,主要方法包括: ```java // 总接收流量(包括所有网络接口) long totalRx = TrafficStats.getTotalRxBytes(); // 总发送流量(包括所有网络接口) long totalTx = TrafficStats.getTotalTxBytes(); // 移动网络接收流量(排除WiFi) long mobileRx = TrafficStats.getMobileRxBytes(); // 移动网络发送流量(排除WiFi) long mobileTx = TrafficStats.getMobileTxBytes(); // 指定UID进程的接收流量 long uidRx = TrafficStats.getUidRxBytes(uid); // 指定UID进程的发送流量 long uidTx = TrafficStats.getUidTxBytes(uid); ``` 这些方法统计从设备启动到调用时刻的累计流量[^1]。 3. **实现原理变更** - **Android P 之前**:依赖 `xt_qtaguid` 内核模块进行流量标记和统计。 - **Android P 及之后**:通过 eBPF 程序在内核空间直接收集网络数据包元数据,效率更高且减少内核依赖。 - 数据流向:网络数据包 → eBPF 程序过滤统计 → `/sys/fs/bpf/traffic_stats` 映射 → `TrafficStats` API 读取。 4. **开发者注意事项** - 权限要求:`<uses-permission android:name="android.permission.READ_NETWORK_USAGE_HISTORY" />` - 系统限制:部分定制 ROM 可能修改统计逻辑,需真机验证。 - 实时性:数据更新存在延迟(通常 <5 分钟)。 ```mermaid graph LR A[网络数据包] --> B(eBPF 程序) B --> C[/sys/fs/bpf/traffic_stats/] C --> D[TrafficStats API] D --> E[应用程序] ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaolin2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值