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

求助:如何通过点击短信链接打开Android应用指定Activity?

Hey there! Let's get this set up for you—opening a targeted Activity when users tap a link in an SMS is straightforward, and I’ll break down exactly what you need to do, especially since your app is built on an HTTP service.

1. Configure Your Target Activity in AndroidManifest.xml

First, you need to tell Android that your specific Activity can handle incoming links. There are two common approaches here, depending on whether you want to use a custom scheme or tie it to your HTTP domain:

Option 1: Custom Scheme (e.g., myapp://)

This is great if you don’t want to rely on a web domain. Add an <intent-filter> inside your target Activity’s tag in AndroidManifest.xml:

<activity
    android:name=".YourTargetActivity"
    android:exported="true"> <!-- Required for Android 12+ -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" /> <!-- Lets links from SMS/browsers trigger this -->
        
        <!-- Define your custom scheme, host, and path rules -->
        <data
            android:scheme="myapp"
            android:host="open"
            android:pathPrefix="/target" />
    </intent-filter>
</activity>

Since your app uses an HTTP service, you might prefer a link like https://yourdomain.com/app/open—this is called App Links, and it lets Android open your app directly without asking the user (once verified).

First, add this intent-filter to your Activity:

<activity
    android:name=".YourTargetActivity"
    android:exported="true">
    <intent-filter android:autoVerify="true"> <!-- Enables automatic domain verification -->
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        
        <data
            android:scheme="https"
            android:host="yourdomain.com"
            android:pathPrefix="/app/open" />
    </intent-filter>
</activity>

Next, add a assetlinks.json file to your web server at https://yourdomain.com/.well-known/assetlinks.json to prove you own the domain. Replace the values below with your app’s package name and SHA-256 fingerprint:

[
  {
    "relation": ["delegate_permission/common.handle_all_urls"],
    "target": {
      "namespace": "android_app",
      "package_name": "com.yourcompany.yourapp",
      "sha256_cert_fingerprints": ["YOUR_APP_SHA256_FINGERPRINT"]
    }
  }
]

You can get your SHA-256 fingerprint from Android Studio (Build > Generate Signed Bundle/APK > view keystore details) or via the keytool command.

Once the Activity opens via the link, you’ll likely want to extract parameters from the URL. Add this code in your YourTargetActivity’s onCreate method:

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_target)
    
    // Fetch incoming link data
    val linkData = intent.data
    linkData?.let { uri ->
        // Extract parameters (e.g., from myapp://open/target?userId=123)
        val userId = uri.getQueryParameter("userId")
        userId?.let {
            // Use the parameter to load relevant data, show a toast, etc.
            Toast.makeText(this, "Opening for user: $it", Toast.LENGTH_SHORT).show()
        }
    }
}

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_target);
    
    Uri linkData = getIntent().getData();
    if (linkData != null) {
        String userId = linkData.getQueryParameter("userId");
        if (userId != null) {
            Toast.makeText(this, "Opening for user: " + userId, Toast.LENGTH_SHORT).show();
        }
    }
}

Craft the link to include in your SMS:

  • For custom scheme: myapp://open/target?userId=123
  • For HTTP App Links: https://yourdomain.com/app/open?userId=123

When users tap this link in their SMS app, Android will either open your Activity directly (for verified App Links) or prompt them to choose your app (for custom schemes or unverified links).

4. Test It Out

Before sending real SMS, verify everything works with:

  • ADB command to simulate the intent:
    # Test custom scheme
    adb shell am start -W -a android.intent.action.VIEW -d "myapp://open/target?userId=123" com.yourcompany.yourapp
    
    # Test HTTP link
    adb shell am start -W -a android.intent.action.VIEW -d "https://yourdomain.com/app/open?userId=123" com.yourcompany.yourapp
    
  • Or send a test SMS to your device and tap the link.

That’s all! You should now have your app opening the specific Activity when users tap the SMS link.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:00:26