自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
实现流量套餐
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 9 可让运营商直接在“设置”应用中向用户提供权威的套餐详情,以减少用户的困惑和支持来电次数。在运行 Android 4.0 或更高版本的设备上,用户可以在“设置”应用中手动配置特定于运营商的流量套餐详情,例如,设置警告和上限以管理其流量用量。
运营商配置
如需配置流量套餐,运营商可以使用 SubscriptionPlan
API 向其现有 Android 应用中添加功能。这些 API 用于支持各种类型的流量套餐,包括可续费和不可续费套餐以及随时间而变化的套餐。
以下是如何配置按月续费的常见流量套餐类型的示例:
SubscriptionManager sm =
context.getSystemService(SubscriptionManager.class);
sm.setSubscriptionPlans(subId, Lists.newArrayList(
SubscriptionPlan.Builder.createRecurringMonthly(
ZonedDateTime.parse("2016-12-03T10:00:00Z"))
.setTitle("G-Mobile")
.setDataLimit(4_000_000_000L,
SubscriptionPlan.LIMIT_BEHAVIOR_BILLED)
.setDataUsage(200_493_293L, dataUsageTimestamp)
.build()));
仅当符合以下某个条件时,设备才会让应用配置流量套餐:
若符合前两个条件,运营商应用可由用户直接安装,而无需在出厂前预安装在系统映像中。操作系统强制要求(且 CDD 要求)所有已配置的流量套餐详情一律受到保护,且仅供最初向操作系统提供相应详情的运营商应用使用。
建议的一种设计方案是,让运营商应用使用空闲维护服务来每天更新一次流量套餐详情,但运营商可以自由选择使用各种机制,例如通过运营商内部短信来接收流量套餐详情。最好通过使用 setRequiresDeviceIdle()
和 setRequiresCharging()
的 JobScheduler
作业实现空闲维护服务。
操作系统使用情况
操作系统通过以下方式使用由 SubscriptionPlan API 提供的流量套餐详情:
- 通过“设置”应用来显示套餐详情,以向用户显示准确的流量用量数据,并提供指向运营商应用的直接深层链接以获得升级/追加销售的机会。
- 系统会根据套餐详情自动配置流量消耗警告和上限通知阈值;用量达到上限的 90% 时便会发出警告提醒。
- 如果运营商暂时提示网络“拥挤”,操作系统将延迟可进行时移的 JobScheduler 作业,从而减少运营商网络的负载。
- 如果运营商暂时提示网络“不按流量计费”,操作系统会将移动网络连接报告为“不按流量计费”,直到运营商清除替换内容或达到超时值(如果提供)。
- 通过将用户当前的流量用量与总体流量上限进行比较,操作系统会估算用户在结算周期结束时的正常流量用量,并适当分配 10% 的剩余流量以改善用户体验,例如让应用使用来自多个途径的流量。
自定义和验证
Android 的“设置”应用将显示由运营商配置的所有流量套餐详情,从而确保用户可以看到最准确的运营商关系状态,并为用户提供进入运营商应用的途径以升级其套餐。建议选择自定义“设置”应用的设备制造商,继续显示相关详情。
本页所述的 SubscriptionManager
API 由 android.telephony.cts.SubscriptionManagerTest
进行测试,这样可确保流量套餐详情可通过运营商应用配置,且操作系统内部会进行相应的更改。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Implement data plans\n\nAndroid 9 lets carriers directly provide authoritative\nplan details to users in the Settings app to reduce user confusion and support\ncalls. On devices running Android 4.0 and higher, users are able to manually\nconfigure their carrier-specific data plan details in the Settings app, for\nexample, setting warnings and limits to manage their data usage.\n\nConfiguration by carrier\n------------------------\n\nTo configure data plans, carriers can add functionality to their existing\nAndroid apps using the\n[`SubscriptionPlan` APIs](https://developer.android.com/reference/android/telephony/SubscriptionPlan.Builder).\nThe APIs are designed to support a wide range of data plan types, including both\nrecurring and non-recurring plans, and plans that change over time.\n\nHere's an example of how to configure a common type of data plan that recurs\nmonthly: \n\n SubscriptionManager sm =\n context.getSystemService(SubscriptionManager.class);\n sm.setSubscriptionPlans(subId, Lists.newArrayList(\n SubscriptionPlan.Builder.createRecurringMonthly(\n ZonedDateTime.parse(\"2016-12-03T10:00:00Z\"))\n .setTitle(\"G-Mobile\")\n .setDataLimit(4_000_000_000L,\n SubscriptionPlan.LIMIT_BEHAVIOR_BILLED)\n .setDataUsage(200_493_293L, dataUsageTimestamp)\n .build()));\n\nThe device only lets an app configure data plans under one of these conditions:\n\n- The SIM card has explicitly defined an app that can manage it, as defined by [`SubscriptionManager.canManageSubscription()`](https://developer.android.com/reference/android/telephony/SubscriptionManager.html#canManageSubscription(android.telephony.SubscriptionInfo)).\n- The carrier has pushed the [`KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING`](https://developer.android.com/reference/android/telephony/CarrierConfigManager#KEY_CONFIG_PLANS_PACKAGE_OVERRIDE_STRING) value via `CarrierConfigManager` to indicate which app can manage the carrier's data plans.\n- The device has an app built into the system image that has the `MANAGE_SUBSCRIPTION_PLANS` permission.\n\nThe first two conditions enable the carrier app to be installed by the user,\nwithout requiring that it be pre-installed into the system image at the factory.\nThe OS enforces (and the CDD requires) that all configured data plan details are\nprotected and are only made available to the carrier app that originally\nprovided the details to the OS.\n\nOne suggested design is for a carrier app to use an idle maintenance service to\nupdate data plan details on a daily basis, but carriers are free to use a wide\nrange of mechanisms, such as receiving data plan details through carrier-internal\nSMS messages. Idle maintenance services are best implemented with a\n`JobScheduler` job that uses\n[`setRequiresDeviceIdle()`](https://developer.android.com/reference/android/app/job/JobInfo.Builder#setRequiresDeviceIdle(boolean))\nand\n[`setRequiresCharging()`](https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setRequiresCharging(boolean)).\n\nUsage by OS\n-----------\n\nThe OS uses the data plan details provided by the SubscriptionPlan APIs in the\nfollowing ways:\n\n- The plan details are surfaced via the Settings app to display accurate data usage to users and to provide [direct deep links into the carrier app](https://developer.android.com/reference/android/telephony/SubscriptionManager.html#ACTION_MANAGE_SUBSCRIPTION_PLANS) for upgrade/upsell opportunities.\n- The data usage warning and limit notification thresholds are automatically configured based on the plan details; the warning is set to 90% of the limit.\n- If the carrier temporarily indicates the network is [\"congested\"](https://developer.android.com/reference/android/telephony/SubscriptionManager.html#setSubscriptionOverrideCongested(int,%20boolean,%20long)), the OS delays JobScheduler jobs that can be time-shifted, reducing the load on the carrier network.\n- If the carrier temporarily indicates the network is [\"unmetered\"](https://developer.android.com/reference/android/telephony/SubscriptionManager#setSubscriptionOverrideUnmetered(int,%20boolean,%20long)), the OS can report the cellular connection as \"unmetered\" until the carrier clears the override, or until the timeout value (if provided) is reached.\n- By comparing the user's current data usage with the overall data limit, the OS estimates the user's normal data usage at the end of the billing cycle and conservatively allocates 10% of any surplus data to improve the user experience, for example, by letting apps use multi-path data.\n\nCustomization and validation\n----------------------------\n\nThe Android Settings app displays all carrier-configured data plan details,\nensuring that users see the most accurate status of their carrier relationship,\nand offering users a path into the carrier app to upgrade their plan. Device\nmanufacturers choosing to customize the Settings app are recommended to continue\nsurfacing these details.\n\nThe `SubscriptionManager` APIs described on this page are tested by\n`android.telephony.cts.SubscriptionManagerTest`, which ensures that data plan\ndetails can be configured by carrier apps and that changes are propagated within\nthe OS."]]