【问题标题】:Intent to open url results in wrong url on browser意图打开 url 导致浏览器上的 url 错误
【发布时间】:2014-08-22 05:10:56
【问题描述】:

我正在尝试使用意图在浏览器中打开一个 URL。

我的代码是

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
startActivity(i);

这将导致我尝试过的任何浏览器出现错误,因为浏览器会尝试打开类似这样的内容:

http://http//onair99.com//

据我尝试,我只遇到过这个特定网址的问题。

有人知道为什么吗?

谢谢。

【问题讨论】:

  • 尝试从源网址中删除“http://”?
  • 然后我得到 "ActivityNotFoundException" (AndroidRuntime(1427): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.somewebsite .com })
  • 我复制粘贴了您的代码,它在我的三星 Galaxy Young 中运行良好。您是否应该更改任何浏览器设置本身?
  • @suitianshi 如果源网址有https:// ??
  • @VnyKumar 我也尝试过不同的设备和模拟器。我总是遇到同样的错误......

标签: android url android-intent uri


【解决方案1】:

按照post中提到的做

String url = "http://www.somewebsite.com";

在生产级代码中,你可能想检查url是否以http或https开头......最好检查一下

if (!url.startsWith("http://") && !url.startsWith("https://"))  
  url = "http://" + url;

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

这是documentation of Intent.ACTION_VIEW.

【讨论】:

  • 谢谢,但这并不能解决我的问题。我提供给意图的地址是一个常量,我知道它以 http 开头。
  • 如果你确定那么它不会进入 if 块,而是直接进入意图部分。使用此意图代码执行 startactivity 时遇到什么问题?
  • 我更新了我的问题。我实际上发现只有当我尝试打开的特定 url 是 www.onair99.com 时才会发生这种情况。有人可以试试吗?
  • 那么服务器上的 .htaccess 文件可能存在问题。因为它全部定义了如何处理请求。你的安卓代码没有问题。我建议尝试使用 http://onair99.com。在将其转换为 href 时保留空间。
  • 如果我按照你说的那样使用空间,我仍然会出错。现在浏览器尝试打开%20onair99.com
【解决方案2】:

试试这个代码

main.xml

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Go to link" />

    </LinearLayout>

MyAndroidAppActivity.java

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

    Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.onair99.com"));
                startActivity(browserIntent);

            }

        });

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyAndroidAppActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【讨论】:

  • 谢谢,但这并不能解决我的问题。我提供给意图的地址是一个常量,我知道它以 http 开头。
  • 嘿!你找到解决方案了吗?
  • 不...我开始认为我在代码的某个地方搞砸了。我在互联网上到处都读到这是开始意图的正确方法。更改地址/浏览器/设备根本没有帮助......所以它一定是我的代码中的东西......
  • 然后尝试创建新项目看看我发送一个链接下载示例并查看其是否正常
  • 我更新了我的问题。我实际上发现这仅在我尝试打开的特定 url 是 www.onair99.com 时发生。有人可以试试吗?
猜你喜欢
  • 1970-01-01
  • 2012-01-16
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2018-03-21
  • 2011-03-01
  • 1970-01-01
相关资源
最近更新 更多