【问题标题】:Opening a link in the browser?在浏览器中打开链接?
【发布时间】:2012-02-21 12:53:58
【问题描述】:

我一直在尝试使用意图来使用浏览器打开指向 kml 文件的链接。这样,当它转到链接时,它将下载并在谷歌地图(或谷歌地球)中打开文件。但是,当我在模拟器中单击它时,似乎什么也没有发生。有什么想法吗?

package shc_BalloonSat.namespace;

import android.content.Intent;
import android.net.Uri;

public class dl_viewKML
{
    void downloadFile()
    {
        String encodedURL = "http://" + "www.wktechnologies.com/shc_android_app/data.kml";

        Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(encodedURL));
        startActivity(webIntent);

    }

    private void startActivity(Intent webIntent)
    {
        // TODO Auto-generated method stub

    }
}

Eclipse 没有显示任何问题,并且在 LogCat 中也没有显示任何内容。

【问题讨论】:

  • 你想要什么信息?它根本没有做任何事情。我单击调用此函数的按钮,但它不执行任何操作。 LogCat 中没有任何内容,控制台中也没有任何内容。就好像我什至没有点击按钮一样。为什么打不开这个链接?
  • 您是否确保它实际上是通过日志进入该方法的?
  • 可能是因为dl_viewKML 类中的startActivity(...) 方法没有做任何事情。
  • @MisterSquonk 这更好吗? public class dl_viewKML { String encodedURL = "http://" + "www.wktechnologies.com/shc_android_app/data.kml"; void downloadFile() { Intent webIntent = new Intent(Intent.ACTION_VIEW);开始活动(网络意图); } private void startActivity(Intent webIntent) { // TODO 自动生成的方法存根 Uri.parse(encodedURL); } }

标签: android file browser web kml


【解决方案1】:

对于方法 startActivity() 要启动 Intent,您必须从 Context 的类或子类(如 Activity、FragmentActivity)中调用它,或者获取对上下文的引用并调用它。

因为您的类 dl_viewKML 不是 Context 的子类,您必须获取对上下文的引用。您可以通过添加带有Context 参数的构造函数来做到这一点,如下所示:

package shc_BalloonSat.namespace;

import android.content.Intent;
import android.net.Uri;

public class dl_viewKML {
private Context ctx

public dl_viewKML(Context ctx) {
this.ctx = ctx;
}
    void downloadFile()
    {
        String encodedURL = "http://" + "www.wktechnologies.com/shc_android_app/data.kml";

        Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(encodedURL));
        ctx.startActivity(webIntent);

    }

}

在您实例化 dl_viewKML 类的 Activity 中,您将执行以下操作:

dl_viewKML obj = new dl_viewKML(this);

dl_viewKML obj = new dl_viewKML(getApplicationContext());

【讨论】:

  • 感谢它的工作。现在,如果我能弄清楚如何在谷歌地图中打开它而不是在浏览器中显示文本。
  • @RandomlyKnighted 我没有使用 Google 地图的经验,但尝试将 Uri.parse(encodedURL) 替换为 Uri.parse("geo:0,0?q=" + encodedURL)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
相关资源
最近更新 更多