首页 文章

如果应用程序已通过深层链接打开,则Android深层链接无效

提问于
浏览
40

如果已通过深层链接打开应用程序,则深层链接不起作用 .

但是,如果我打开应用程序而不是通过触发深层链接,例如单击应用程序图标以打开应用程序 . 然后在之后触发deeplink将始终有效 .


这里有详细信息:

所以我在AndroidManifest中设置了这样的活动,即LaunchActivity .

<activity
    android:name="some.package.name.LaunchActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.SomeTheme">
    <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:scheme="dlscheme" android:host="dlhost" />
    </intent-filter>
</activity>

在LaunchActivity中,我会在onCreate()中打印一个日志,表明它已存在 .

我用了

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name

测试深层链接 .

随着应用程序被杀,我使用了上面的命令 . 它可以打开应用程序并路由到正确的活动,没问题 . 并有以下日志 .

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Starting: Intent { act=android.intent.action.VIEW dat=dlscheme://dlhost/param pkg=some.package.name }
Status: ok
Activity: some.package.name/.activity.LaunchActivity
ThisTime: 898
TotalTime: 898
WaitTime: 919
Complete

但是,如果我再次输入相同的命令,而不会杀死该应用程序 . 它只会打开应用程序,但它不会打开正确的活动,并生成以下日志 .

adb shell am start -W -a android.intent.action.VIEW -d "dlscheme://dlhost/param" some.package.name
Starting: Intent { act=android.intent.action.VIEW dat=dlscheme://dlhost/param pkg=some.package.name }
Warning: Activity not started, its current task has been brought to the front
Status: ok
Activity: some.package.name/.activity.LaunchActivity
ThisTime: 0
TotalTime: 0
WaitTime: 6
Complete

这条额外的线

Warning: Activity not started, its current task has been brought to the front

我实际上也尝试过使用这个chrome意图的网站:

intent://dlhost/param#Intent;scheme=dlscheme;package=some.package.name;end

它会表现得一样 .

7 回答

  • 5

    在LaunchActivity活动代码中的清单中添加 android:launchMode="singleTop"

  • 6

    我发现添加 android:launchMode="singleTask" 有效 . singleTop 对我不起作用 .

  • 3

    在项目的清单文件中,您需要将跟随添加到主要活动中 .

    android:launchMode="singleTask"
    

    所以,在清单中,你会得到类似下面的内容:

    <activity android:name="some.package.name.LaunchActivity" 
          android:launchMode="singleTask">
          android:screenOrientation="portrait"
          android:theme="@style/Theme.SomeTheme">
              <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:scheme="dlscheme" android:host="dlhost" />
              </intent-filter>
     </activity>
    

    基本上它的作用是创建一个新任务,一个新实例将作为根任务推送到任务 . 但是,如果任何任务中存在任何活动实例,系统会通过onNewIntent()方法调用将意图路由到该活动实例 . 在此模式下,活动实例可以推送到同一任务 . 如果用户从当前活动中单击BACK键,系统将使用户返回上一个活动 .

    另一方面,在 singleTop 中,如果活动实例已存在于当前任务的顶部且系统路由意图此活动,则不会创建新实例,因为它将触发onNewIntent()方法而不是创建新宾语 .

    更多信息可以在here找到 .

  • 7

    Mainefest文件看起来像这个样本

    <activity
                        android:name=".user.HomeActivity"
                        android:screenOrientation="portrait"
                        android:exported="true"
                        android:launchMode="singleTop"
                        android:windowSoftInputMode="stateAlwaysHidden"
                        android:theme="@style/AppTheme.NoActionBar" >
                        <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="example"/>
                            <data android:host="example.com"
                                android:pathPrefix="/deeplink"/>
                            <action android:name="android.intent.action.MAIN" />
                        </intent-filter>
                    </activity>
    

    HomeActivity

    @Override
                protected void onNewIntent(Intent intent) {
                    super.onNewIntent(intent);
                    Uri data = intent.getData();
                    if (data != null)
                        callDeep(data);
                    setIntent(intent);
                    Log.d("DeepLinking", "new intent value==>" + data + "==== value===>");
                }
            //or in on create 
            @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_home);
              Uri data = intent.getData();
                    Log.d("DeepLinking", "intent value==>" + data + "==== value===>" + bundle + "===action==>" + action);
            }
    
  • 2
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe);
        onNewIntent(getIntent());
    }
    
    protected void onNewIntent(Intent intent) {
        String action = intent.getAction();
        String data = intent.getDataString();
        if (Intent.ACTION_VIEW.equals(action) && data != null) {
            String recipeId = data.substring(data.lastIndexOf("/") + 1);
            Uri contentUri = RecipeContentProvider.CONTENT_URI.buildUpon()
                    .appendPath(recipeId).build();
            showRecipe(contentUri);
        }
    }
    
  • 2

    “在您的活动清单中添加android:launchMode =”singleTop“

    如果你的启动模式是默认的,那么这个警告出现“警告:活动没有启动,它的当前任务已经被带到了前面”,因为每次它创建你的活动的新实例,而你使用单顶启动模式然后onNewIntent()调用方法而不是创建新对象

  • 4

    从日志中可以看出“警告:活动未启动,其当前任务已被置于最前面”,因此未创建活动的新实例 . 在这种情况下,新意图将重定向到活动的onNewIntent(Intent intent) .

    在您的情况下,我怀疑您没有覆盖此方法,并将从活动的onCreate()方法中提取信息 .

    而是创建一个类似extractDataFromIntentAndProcess(Intent intent)的方法,并从oncreate方法(extractDataFromIntentAndProcess(getIntent()))以及活动的onNewIntent方法(extractDataFromIntentAndProcess(intent))调用它 .

相关问题