【问题标题】:How to specify URL deeplink android manifest如何指定 URL 深层链接 android 清单
【发布时间】:2019-01-10 21:36:28
【问题描述】:

问题是我不知道如何从此链接“my.app//id=819”建立深层链接

我尝试了一些数据变体,但无法理解路径模式

<intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
       <data 
        android:scheme="my.app" 
        android:host="id" 
        android:pathPattern="=.*"/>

预期是午餐意图并取 id 的值

【问题讨论】:

  • 该链接是外部链接还是内部应用程序链接?我认为id=819 更像是一个参数而不是主机

标签: android xml android-manifest android-deep-link


【解决方案1】:

你的意思是my.app//host?id=819,对吧?

您围绕my.app//host 构建intent-filter,不包含参数。

Add intent filters for incoming links:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="my.app"
          android:host="host" />
</intent-filter>

然后一旦进入活动,你就可以解析参数,Read data from incoming intents

Intent intent = getIntent();
Uri data = intent.getData();

data,你可以使用Uri#getQueryParameter()id获取819

String id = data.getQueryParameter("id");

【讨论】:

    【解决方案2】:

    使用&lt;intent-filter&gt;,您可以指定您的活动应该处理的网址。一旦系统检测到您的应用程序中的 Activity 能够处理该 url,它就会打开该 Activity,您就可以在 Extra.getData() 对象中获取数据。

    在您的 AndroidManifest.xml 文件中:

    <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter android:label="My app preview">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
    
                <data
                    android:host="www.myapp.com/api/id"
                    android:pathPrefix="/api/" <!-- optional, you can skip this field if your url doesn't have prefix-->
                    android:scheme="http"/>
                <data
                    android:host="www.myapp.com/api/id"
                    android:pathPrefix="/api/" <!-- optional, you can skip this field -->
                    android:scheme="https"/>
            </intent-filter>
    </activity>
    

    在您的活动中:

    public class MainActivity extends Activity {    
           Bundle bundle = getIntent.getExtras();
    
           @Override
           protected void onCreate(@Nullable Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               handleExtra(getIntent());
           }
    
           // If your app is in opened in background and you attempt to open an url, it 
           // will call this function instead of onCreate()
           @Override
           protected void onNewIntent(Intent intent) {
               super.onNewIntent(intent);
               setIntent(intent);
               handleExtra(getIntent());
           }
    
           private void handleExtra(Intent intent) {
               String myID = intent.getData().getLastPathSegment();
               ....
           }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      相关资源
      最近更新 更多