1

问题是我不知道如何从此链接“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 的值

4

2 回答 2

2

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

您围绕 构建意图过滤器my.app//host,不包含参数。

为传入链接添加意图过滤器

<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>

然后一旦进入活动,您可以解析参数,从传入的意图中读取数据

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

data,您可以使用Uri#getQueryParameter()819从以下获取id

String id = data.getQueryParameter("id");
于 2019-01-10T22:13:58.373 回答
0

使用<intent-filter>您指定您的活动应该处理的网址。一旦系统检测到您的应用程序中的活动能够处理该 url,它就会打开该活动,您就可以获取数据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();
           ....
       }
}
于 2019-01-10T22:05:47.053 回答