【问题标题】:Android deeplinking Intent issueAndroid 深度链接 Intent 问题
【发布时间】:2019-03-12 18:44:58
【问题描述】:

我正在尝试在我的应用程序中实现深度链接,我在我的主要活动的 onResume 方法中添加了 getIntent 并且我能够从链接中打开我的主要活动,但我面临以下问题。

  1. 如果我第一次通过单击应用程序图标打开应用程序,则意图操作将为 Intent.ACTION_MAIN,这对于所有后续尝试都是恒定的,即当我通过链接,intent.action 应该是 Intent.ACTION_VIEW,但 action 总是 ACTION_MAIN。

  2. 如果应用程序是通过 chrome 的链接打开的,那么我可以看到我的应用程序的两个实例,即上面的 chrome 和我的应用程序本身。

    <activity
        android:name=".MainActivity"
        android:hardwareAccelerated="false"
        android:launchMode="singleTop"> // I used singleTop because i have implementd isTaskRoot in my main activity
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    
        <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="clip.myapp.tp"
                android:pathPattern="/.*"
                android:scheme="mhhds" />
        </intent-filter>
    </activity>
    

下面是我实现 getIntent 的 mainactivity.java 文件的 onResume

     @Override
    protected void onResume() {

    super.onResume();


    mIntent = getIntent();

    String appLinkAction = mIntent.getAction();
    if(mIntent.getAction().equals(Intent.ACTION_VIEW)) {

        Uri data = mIntent.getData();
        String mIntentData = data.toString();

        System.out.println("Intentdata:" + mIntentData);
    }
    }

【问题讨论】:

    标签: android android-intent deep-linking intentfilter android-deep-link


    【解决方案1】:

    这是因为singleTop 不会创建一个新的活动实例并且总是使用现有的,所以

    如果我第一次通过点击应用图标打开应用, 那么意图动作将是 Intent.ACTION_MAIN,这将是 所有成功尝试的常量,即当我通过以下方式打开应用程序时 一个链接,intent.action 应该是 Intent.ACTION_VIEW,但是 action 总是 ACTION_MAIN。

    由于上述原因,getIntent 将返回第一次收到的实例,而不是覆盖 onNewIntent,这将返回最新意图的实例,因此请使用 onNewItent 而不是 onResume

    如果应用程序是通过 chrome 的链接打开的,那么我可以看到两个 我的应用程序的实例,即上面的 chrome 和我的应用程序本身

    这是因为您的应用之前是作为独立应用打开的(现在在堆栈历史中),现在它作为搜索结果在 chrome 中打开,所以这是正常行为。

    【讨论】: