ContextmContext;NetworkmNetwork;publicvoidrequestPremiumCapabilityNetwork(@NetCapabilityintcapability){ConnectvityManagercm=mContext.getSystemService(ConnectivityManager.class);NetworkRequestrequest=NetworkRequest.Builder().addCapability(capability).build();cm.requestNetwork(request,newNetworkCallback(){@OverridepublicvoidonAvailable(Networknetwork){log("Premium capability %d network is available.",capability);mNetwork=network;}@OverridepublicvoidonLost(Networknetwork){log("Premium capability %d network is not available.",capability);mNetwork=null;}});}
建構 NetworkRequest 物件時,您新增的功能「不是」傳遞至 TelephonyManager API 的功能。下表將 TelephonyManager 類別的常數對應至 NetworkCapabilities 中的對應常數。
ContextmContext;publicbooleanisPremiumCapabilityAvailableForPurchase(@PremiumCapabilityintcapability){TelephonyManagertm=mContext.getSystemService(TelephonyManager.class);booleanisAvailable=tm.isPremiumCapabilityAvailableForPurchase(capability);log("Premium capability %d %s available to purchase.",capability,isAvailable?"is":"is not");returnisAvailable;}
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Use network slicing\n\n5G network slicing gives carriers the ability to provide network performance\nboosts for specific use cases. This guide explains how an app can use the\nnetwork slicing feature.\n\nThis guide also covers how to trigger the [network slicing upsell UX\nflow](#ux-flow) in cases where a purchase is required before the app can\naccess the premium connection.\n\n### Step 1: Declare premium capability intents\n\nIn order for your app's request for a premium slicing capability to be honored,\nyour app must declare its intent to request that capability in the app manifest.\nOtherwise, the network request fails throwing a `SecurityException`.\n\nTo do this, your app must declare the\n[`PackageManager.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES`](/reference/android/content/pm/PackageManager#PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES)\nproperty in the `AndroidManifest.xml` file and include a corresponding XML\nresource file.\n\nA capability declaration in the manifest file looks like this: \n\n \u003cproperty android:name=\"android.net.PROPERTY_SELF_CERTIFIED_NETWORK_CAPABILITIES\"\n android:resource=\"@xml/network_capabilities\" /\u003e\n\nThe corresponding `network_capabilities.xml` resource file looks like this: \n\n \u003cnetwork-capabilities-declaration\u003e xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003cuses-network-capability android:name=\"NET_CAPABILITY_PRIORITIZE_LATENCY\"/\u003e\n \u003c/network-capabilities-declaration\u003e\n\n| **Note:** The only premium capability supported in Android 14 is [`NET_CAPABILITY_PRIORITIZE_LATENCY`](/reference/android/net/NetworkCapabilities#NET_CAPABILITY_PRIORITIZE_LATENCY).\n\n### Step 2: Verify if the premium capability is available\n\nCall the\n[`requestNetwork()`](/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback))\nAPI method to determine whether the premium capability is available. \n\n Context mContext;\n Network mNetwork;\n\n public void requestPremiumCapabilityNetwork(@NetCapability int capability) {\n ConnectvityManager cm = mContext.getSystemService(ConnectivityManager.class);\n NetworkRequest request = NetworkRequest.Builder()\n .addCapability(capability)\n .build();\n cm.requestNetwork(request, new NetworkCallback() {\n @Override\n public void onAvailable(Network network) {\n log(\"Premium capability %d network is available.\", capability);\n mNetwork = network;\n }\n\n @Override\n public void onLost(Network network) {\n log(\"Premium capability %d network is not available.\", capability);\n mNetwork = null;\n }\n });\n }\n\nWhen you build a `NetworkRequest` object, the capability that you add is **not**\nthe same capability that you pass to the `TelephonyManager` APIs.\nThe following table maps the constants from the `TelephonyManager` class to the\ncorresponding constants in `NetworkCapabilities`.\n\n| `TelephonyManager` constant | `NetworkCapabilities` constant |\n|--------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|\n| [`PREMIUM_CAPABILITY_PRIORITIZE_LATENCY`](/reference/android/telephony/TelephonyManager#PREMIUM_CAPABILITY_PRIORITIZE_LATENCY) | [`NET_CAPABILITY_PRIORITIZE_LATENCY`](/reference/android/net/NetworkCapabilities#NET_CAPABILITY_PRIORITIZE_LATENCY) |\n\n### Step 3: If the premium capability is not available, check availability to purchase\n\n| **Note:** The network slicing upsell feature is only available in Android 14 and higher for apps that have the [`READ_BASIC_PHONE_STATE`](/reference/android/Manifest.permission#READ_BASIC_PHONE_STATE) permission.\n\nCall the [`isPremiumCapabilityAvailableForPurchase()`](/reference/android/telephony/TelephonyManager#isPremiumCapabilityAvailableForPurchase(int))\nAPI method to determine whether the selected premium capability is available.\nThis method returns `true` if the capability is available for purchase from the\ncarrier using the upsell notification workflow. \n\n Context mContext;\n\n public boolean isPremiumCapabilityAvailableForPurchase(@PremiumCapability int capability) {\n TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);\n boolean isAvailable = tm.isPremiumCapabilityAvailableForPurchase(capability);\n log(\"Premium capability %d %s available to purchase.\",\n capability,\n isAvailable ? \"is\" : \"is not\");\n return isAvailable;\n }\n\n### Step 4: Initiate the upsell notification flow\n\nAfter confirming that the premium capability is available, your app should call\n[`purchasePremiumCapability()`](/reference/android/telephony/TelephonyManager#purchasePremiumCapability(int,%20java.util.concurrent.Executor,%20java.util.function.Consumer%3Cjava.lang.Integer%3E))\nto initiate the upsell notification flow. If the user has not already purchased\nthe specified capability and all preconditions are satisfied, then the platform\nshows the user a notification to let them know that performance boost options\nmight be available from their carrier. If the user taps the notification, the\nplatform opens the carrier's webview so that the purchase process can continue. \n\n Context mContext;\n\n public void purchasePremiumCapability(@PremiumCapability int capability) {\n TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);\n tm.purchasePremiumCapability(capability, Runnable::run, new Consumer\u003cInteger\u003e() {\n @Override\n public void accept(Integer result) {\n log(\"Purchase premium capability %d result: %d\", capability, result);\n int purchaseResult = result;\n }\n });\n }\n\nThe `parameter` callback passed to `purchasePremiumCapability()` returns a\nresult code for the purchase request.\n\nThe result codes\n[`PURCHASE_PREMIUM_CAPABILITY_RESULT_SUCCESS`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_SUCCESS)\nand\n[`PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_PURCHASED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_PURCHASED)\nrepresent successful results where your app may proceed to requesting the\nselected premium capability.\n\nThe result codes in the following list represent failed purchase requests. See\nthe API reference to learn more about them.\n\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_IN_PROGRESS`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_IN_PROGRESS)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_ERROR`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_ERROR)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_ENTITLEMENT_CHECK_FAILED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_ENTITLEMENT_CHECK_FAILED)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_FEATURE_NOT_SUPPORTED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_FEATURE_NOT_SUPPORTED)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_NETWORK_NOT_AVAILABLE`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_NETWORK_NOT_AVAILABLE)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_FOREGROUND`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_FOREGROUND)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_PENDING_NETWORK_SETUP`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_PENDING_NETWORK_SETUP)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_TIMEOUT`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_TIMEOUT)\n- [`PURCHASE_PREMIUM_CAPABILITY_RESULT_USER_CANCELED`](/reference/android/telephony/TelephonyManager#PURCHASE_PREMIUM_CAPABILITY_RESULT_USER_CANCELED)\n\nIf the purchase request fails, your app may use the default network instead.\nThere is no automatic fallback behavior if the premium slice request cannot be\nfulfilled.\n\n### UX flow for slicing upsell"]]