有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何通过深度链接传递数据?

我在我的应用程序中有一个报价列表,每个列表项上都有一个共享按钮。当任何用户单击共享链接时,我使用deeplink打开我的应用程序的详细活动。我所处的情况是,当有人单击链接时,我的详细信息页面活动会被触发,但我如何知道,当有人单击共享的深度链接时,会提供详细信息活动来显示


共 (3) 个答案

  1. # 1 楼答案

    下面的@Vikas答案是一种简化的方法-

    应用程序1:此应用程序将通过深度链接向另一个应用程序发送数据

    String deepUrl = "app://anotherapp?key1=value1&key2=value2"; //key1 and key2 for sending data to other application
    Intent intent = new Intent (Intent.ACTION_VIEW);
    intent.setData (Uri.parse(deepUrl));
    startActivity(intent);
    

    此应用程序将以另一个数据链接的意图启动

    现在我们需要在第二个应用程序中处理相应的链接,如下所示:

    应用程序2的清单文件来处理深度链接,将intent filter添加到将处理数据的相应活动中,在我的例子中MainActivity就可以了

    <activity android:name=".MainActivity">
                ...
                <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:host="anotherapp"
                        android:scheme="app" />
                </intent-filter>
            </activity>
    

    然后在你的活动中,你可以得到如下数据:

     Intent intent = getIntent();
     Uri data = intent.getData();
     String data1= data.getQueryParameter("key1"); // you will get the value "value1" from application 1 
     String data2= data.getQueryParameter("key2");
    

    有关深度链接的更多信息,请参阅官方link

    注意:如果手机中未安装应用程序2,则您可以从应用程序1获取一个错误,说明ActivityNotFoundException

  2. # 2 楼答案

    清单文件将保持与此链接中指定的相同https://developer.android.com/training/app-indexing/deep-linking.html

    但是,您可以通过www.example在发送给用户的链接中提供额外的数据。com/gizmos?key=valueToSend

    然后在活动中你可以做

    Uri data = intent.getData();
    
    data.getQueryParameter("key");
    
  3. # 3 楼答案

    假设您为每个项目生成单独的共享链接。你可以将一些参数与深度链接URL一起发送,然后在应用程序中接收它们。任何身份证都可以。 (来源:this

    <intent-filter android:label="@string/filter_title_viewgizmos">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!  Accepts URIs that begin with "http://www.example.com/gizmos”  >
        <data android:scheme="http"
              android:host="www.example.com"
              android:pathPrefix="/gizmos" />
        <!  note that the leading "/" is required for pathPrefix >
        <!  Accepts URIs that begin with "example://gizmos”
        <data android:scheme="example"
              android:host="gizmos" />
         >
    </intent-filter>
    

    举个例子,如果应用程序在这里进行深度链接,你可以在相应的活动(这里:com.example.android.GizmosActivity)中收到你的意图,并从中提取信息