You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android直接安装eSIM遇问题,onNewIntent始终返回结果码0求排查

eSIM直接安装应用调试问题:始终返回结果码0

我正在开发一款采用直接方式安装eSIM的应用,已遵循Android官方文档《Implementing eSIM》提供的示例代码,但仍未成功。使用TEST_SIM_PROFILE、TEST_SIM_PROFILE1及TEST_SIM_PROFILE3等测试激活码时,onNewIntent(intent: Intent)中始终返回结果码0,烦请帮忙检查代码是否有需要补充或优化的地方。

现有Kotlin代码

import android.app.PendingIntent
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.telephony.euicc.DownloadableSubscription
import android.telephony.euicc.EuiccManager
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    private val TEST_SIM_PROFILE =
        "LPA:1\$MTAwMDAwMDAwMzQ2MzM5Y2E2NDgwNQ==$7UMyhYJKZLBfHZt2ve9vQH0oKvE=$"
    private val TEST_SIM_PROFILE1 =
        "1\$prod.smdp-plus.rsp.goog\$3TD6-8L82-HUE1-LVN6"
    private val TEST_SIM_PROFILE2 = "LPA:1\$lpa.airalo.com\$TEST"
    private val TEST_SIM_PROFILE3 = "dfdffdf"
    private val REQUEST_CODE_INSTALL = 1

    @RequiresApi(Build.VERSION_CODES.P)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val esimButton = findViewById<Button>(R.id.esimButton)
        esimButton.setOnClickListener { downloadTestProfile() }
    }


    @RequiresApi(Build.VERSION_CODES.P)
    private fun downloadTestProfile() {
        val euiccManager = getSystemService(EUICC_SERVICE) as EuiccManager
        if (!euiccManager.isEnabled) {
            Toast.makeText(this, "eSIM is not supported on this device", Toast.LENGTH_LONG).show()
            return
        }
        Log.d("eSIMInstall", "Attempting to install profile...");
        val subscription = DownloadableSubscription.forActivationCode(TEST_SIM_PROFILE)
        euiccManager.downloadSubscription(
            subscription, true, PendingIntent.getActivity(
                this, REQUEST_CODE_INSTALL, Intent(this, MainActivity::class.java),
                PendingIntent.FLAG_IMMUTABLE
            )
        )
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Log.d("eSIMInstall", "on calling of newIntent...");
        val resultCode =
            intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0)
        Log.d("eSIMResult", "Result code: $resultCode");

        when (resultCode) {
            EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> Toast.makeText(
                this,
                "eSIM profile installed successfully!",
                Toast.LENGTH_LONG
            ).show()

            EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> { }
            else -> Toast.makeText(this, "Failed to install eSIM profile", Toast.LENGTH_LONG).show()
        }
    }
}

AndroidManifest.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.WRITE_EMBEDDED_SUBSCRIPTIONS"
        tools:ignore="ProtectedPermissions" />
    <uses-feature android:name="android.permission.MODIFY_PHONE_STATE"
        tools:ignore="ProtectedPermissions" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.EsimInstallation"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

问题分析与优化建议

1. 系统权限限制(核心问题)

WRITE_EMBEDDED_SUBSCRIPTIONS是受保护的系统权限,仅授予运营商应用、设备预装应用或拥有系统签名的应用。通过tools:ignore="ProtectedPermissions"只是忽略编译警告,实际运行时普通应用无法获得该权限,这会直接导致eSIM安装操作失败,无法触发正确的结果回调。

2. 测试激活码有效性

  • TEST_SIM_PROFILE3是无效的乱码字符串,无法被解析为合法的激活码;
  • 谷歌的prod.smdp-plus.rsp.goog测试服务器需要特定的设备权限或测试账号,普通设备可能无法访问;
  • 建议使用运营商提供的合法测试激活码,或使用设备厂商提供的eSIM测试环境。

3. 结果码获取逻辑修正

当前仅获取EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE,但应先检查主结果码EXTRA_EMBEDDED_SUBSCRIPTION_RESULT,因为当操作未被授权或直接失败时,Intent可能不会携带详细码,导致返回默认值0。修正后的onNewIntent逻辑如下:

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    Log.d("eSIMInstall", "onNewIntent triggered...")
    
    val mainResult = intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_RESULT, -1)
    val detailedResult = intent.getIntExtra(EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0)
    
    Log.d("eSIMResult", "Main result: $mainResult, Detailed result: $detailedResult")

    when (mainResult) {
        EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK -> {
            Toast.makeText(this, "eSIM安装成功!", Toast.LENGTH_LONG).show()
        }
        EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR -> {
            // 处理可解决错误,比如弹出系统权限请求或用户确认界面
            val resolutionIntent = intent.getParcelableExtra<Intent>(EuiccManager.EXTRA_RESOLUTION_INTENT)
            resolutionIntent?.let {
                startActivity(it)
            }
        }
        else -> {
            Toast.makeText(this, "eSIM安装失败,错误码:$detailedResult", Toast.LENGTH_LONG).show()
        }
    }
}

4. PendingIntent与启动模式

  • 确保REQUEST_CODE_INSTALL唯一,避免与其他PendingIntent冲突;
  • 保持singleTask启动模式,确保回调能正确传递到当前Activity实例。

5. 设备兼容性检查

除了euiccManager.isEnabled,还可以增加硬件支持检查:

private fun isEsimHardwareSupported(): Boolean {
    return packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_EUICC)
}

内容的提问来源于stack exchange,提问作者Ahmed Rehman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:54:49