【问题标题】:Intent for opening an URL doesn't work for one app but does for another打开 URL 的意图不适用于一个应用程序,但适用于另一个应用程序
【发布时间】:2020-02-03 11:27:43
【问题描述】:

我的应用想通过这个方法打开一个网页链接(比如https://www.google.com):

public void openUrl(Activity activity, String urlParam) {
    String url = Uri.encode(urlParam);
    Intent browserIntent = new Intent(Intent.CATEGORY_BROWSABLE, Uri.parse(url));

    browserIntent.setAction(ACTION_VIEW);

    activity.startActivity(browserIntent);
}

我收到此错误:

android.content.ActivityNotFoundException:未找到处理 Intent 的 Activity { act=android.intent.action.VIEW dat=https://www.google.com }

如果我让应用调用选择器以供用户选择应用,则设备上会显示一条消息,指出没有应用可以处理该操作。

startActivity(Intent.createChooser(intent, "Browse with"));

所以,这也行不通。

这很奇怪,我看到很多类似的问题但没有解决方案。网址正确。

这是 Gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

这怎么可能?我有另一个使用类似代码的应用程序,它能够调用浏览器在同一设备上显示 URL。

这是它的 Gradle 文件:

apply plugin: 'android'

android {
    //noinspection GradleCompatible
    compileSdkVersion 28
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.myappname.app"
        versionCode 62

        minSdkVersion 19
        targetSdkVersion 28
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:28.0.0'
}

如何解决这个问题?

【问题讨论】:

标签: java android url android-intent android-gradle-plugin


【解决方案1】:

实际问题在于Uri.encode(urlParam) 的使用。您正在对您的 URL 进行编码,但稍后您并未对其进行解码,以便 ACTION_VIEW 了解意图数据!

    String url = "https://www.google.com/";
    String query = Uri.encode(url, "UTF-8");
    Intent browserIntent = new Intent(CATEGORY_BROWSABLE, Uri.parse(Uri.decode(query)));
    browserIntent.setAction(ACTION_VIEW);
    startActivity(browserIntent);

【讨论】:

  • 是的,似乎 url 的不同部分必须以合适的方式编码。
  • 编码和解码后的目的是什么?而且,Uri.parse方法是否足以“清理”uri字符串?
  • 这取决于你,我从未见过有人在将 url 传递给 Intent 之前对其进行编码。你为什么这么做?
  • 网址由用户编辑。请您说明解析url是否足够?
  • 您不需要对整个 URL 进行编码,只需要对来自“不可靠来源”的部分进行编码。
【解决方案2】:
Uri uri = Uri.parse("https://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

我得到了同样的错误 android.content.ActivityNotFoundException: No Activity found to handle Intent

问题是因为我的手机中没有安装任何可以打开 URL 的应用程序(即浏览器)。所以安装浏览器后问题就解决了。

确保至少有一个应用可以处理您调用的意图

【讨论】:

  • 如前所述,我在同一台设备上还有另一个可以打开网址的应用程序。
  • 您在不同的场景中是对的,但对于这个场景,它与 URL 编码有关!
猜你喜欢
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多