如果应用程序是由已经深入链接打开Android的深层链接无法正常工作

问题描述 投票:47回答:7

如果应用程序是由深层链接打开已深层链接无法正常工作。

不过,如果我打开应用程序不会被触发深层链接,如单击应用程序图标,打开应用程序。然后引发深层链接后会一直工作。


来到这里的细节:

所以,我有我的活动设置了这样的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

其实我也试图与一个网站,使用这种铬意图:

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

它会表现得一样。

android deep-linking
7个回答
10
投票

在项目的清单文件,你需要后续添加到您的主要活动。

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


8
投票

在清单中添加android:launchMode="singleTop"在LaunchActivity活动标签


7
投票

在项目的清单文件,你需要后续添加到您的主要活动。

android:launchMode="singleTask"

和处理内部onNewIntent()深层链接

@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);
    }
}

6
投票

我发现,添加android:launchMode="singleTask"工作。 singleTop并没有为我工作。


4
投票

清单文件看起来是这样的样本

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

“清单中与您的活动” singleTop:“launchMode =添加机器人”

如果你的启动模式是默认那么,该警报来“警告:尚未开始活动,其当前的任务已经放到了前面”,因为每次它创建活动的新实例时间,而如果使用单路顶部发射模式,然后onNewIntent()方法调用,而不是创建一个新对象


2
投票

从日志它说:“警告:尚未开始活动,其当前的任务已经放到了前面”,因此不创建活动的新实例。在这种情况下,新的意图会被重定向到活动的onNewIntent(意向意图)。

你的情况,我怀疑你没有覆盖此方法,并会从活动的onCreate()mehtod提取信息。

相反,创建一个方法类似extractDataFromIntentAndProcess(意向意图),并从您的活动onCreate方法(extractDataFromIntentAndProcess(getIntent()))也从onNewIntent方法(extractDataFromIntentAndProcess(意向))调用它。

© www.soinside.com 2019 - 2024. All rights reserved.