android - 在pathPrefixes/abc 和/abc/def 上开启不同的Activity

标签 android intentfilter

我有两个网址。例如:

  1. http://host.com/abc/12345
  2. http://host.com/abc/def/12345

12345 - 是一些 id。

我想为这些 url 打开不同的 Activity 。

目前我在 AndroidManifest.xml 中有下一个实现:

<activity
    android:name=".ui.activities.Activity1"
    android:windowSoftInputMode="adjustResize">
    <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="http" android:host="host.com" android:pathPrefix="/abc"/>
    </intent-filter>
</activity>
<activity
    android:name=".ui.activities.Activity2"
    android:windowSoftInputMode="adjustResize">
    <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="http" android:host="host.com" android:pathPrefix="/abc/def"/>
    </intent-filter>
</activity>

第一个 url 效果很好:当我点击它时,android 建议我使用一些其他应用 或我的 Activity1

但是当我点击第二个 url 时出现问题:android 建议我使用一些其他应用 或我的Activity1 或我的Activity2

所以我的问题是:有没有办法从建议列表中排除 Activity1

我尝试使用 pathPattern 并尝试使用谷歌搜索如何从 IntentFilter 中排除 url,但我失败了。

最佳答案

由于我没有通过`AndroidManifest.xml'找到优雅的解决方案,我决定通过代码实现部分“选择”逻辑。所以这就是我所拥有的:

在 list 文件中,我们定义了 UrlActivity,它将响应任何 url,如“http://host.com/abc/ ...”,因此它看起来像:

<activity
    android:name=".ui.activities.UrlActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize">
    <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="http" android:host="host.com" android:pathPrefix="/abc"/>
    </intent-filter>
</activity>

然后我们定义专门为UrlActivity 调优的接口(interface)Component(您可以根据行为选择任何名称)。在简单的情况下,它可以从 Activity 中复制类似的方法。例如:

public interface Component {
    void onStart();
    void onStop();
}

然后在您的 UrlActivity 中检索 uri 并选择适当的组件实现。例如:

private Component component;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Uri uri = getIntent().getData();
    List<String> segments = uri.getPathSegments();

    if (segments.contains("def")) {
        component = new Component2(...);
    } else {
        component = new Component1(...);
    }
}

@Override
protected void onStart() {
    super.onStart();
    component.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    component.onStop();
}

与@KamranAhmed 解决方案相比,此解决方案具有优势:可扩展性、减少重复。但它也有一个缺陷:您需要编写更多代码并为该解决方案考虑合适的架构。所以它确实比@KamranAhmed 方法更难,但对我来说它更干燥和灵活。

关于android - 在pathPrefixes/abc 和/abc/def 上开启不同的Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40278501/

相关文章:

android - 文件已存在。当我试图让我的 .apk 在 android 中签名时发生错误

android - 如何在横向模式下启动启动画面?

android - 如何通过点击外部链接打开特定 ID 的特定 Activity

java - EasyMock "Unexpected method call"尽管有 expect 方法声明

java - 如何等待广播接收器完成

android - 注册一个应用程序以在 SEND 操作中接收所有文件类型

java - 如何在 Android 中使用 PHP MYSQL 更新数据

android - Android可以同时录制当前播放的内容吗?

android - 注册 Android 应用程序以接收某些文件类型

android - 从命令行构建android项目