2012-01-29 200 views
2

我一直在試圖用瀏覽器打開一個kml文件的鏈接。這樣,當它進入鏈接時,它將下載並在Google地圖(或Google地球)中打開文件。但是,當我在模擬器中點擊它時,似乎沒有任何事情發生。有任何想法嗎?在瀏覽器中打開鏈接?

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中顯示任何內容。

+0

提供更多信息。 – JoxTraex 2012-01-29 08:22:03

+0

你想要什麼信息?它什麼都不做。我點擊調用這個函數的按鈕,它什麼也不做。 LogCat中沒有顯示任何內容,控制檯中沒有顯示任何內容。就好像我沒有甚至沒有點擊按鈕。爲什麼不打開這個鏈接? – tylerbhughes 2012-01-29 08:31:50

+0

您是否確定它通過日誌實際進入該方法? – JoxTraex 2012-01-29 08:32:43

回答

2

對於該方法startActivity()開始你Intent你必須無論是從Context類或子類(如活動,FragmentActivity)調用或獲取上下文的引用,並調用它。

因爲您的類dl_viewKML不是Context的子類,所以您必須獲取對上下文的引用。你實例的dl_viewKML類,你會做這樣的事情

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

    } 

} 

在你Activity

dl_viewKML obj = new dl_viewKML(this); 

您可以通過添加一個構造帶 Context參數像這樣這樣做
dl_viewKML obj = new dl_viewKML(getApplicationContext()); 
+0

謝謝,它得到它的工作。現在,如果我只想知道如何在Google地圖中打開它,而不是在瀏覽器中顯示文本。 – tylerbhughes 2012-01-29 11:09:50

+0

@RandomlyKnighted我沒有Google地圖的使用經驗,但嘗試用Uri.parse(「geo:0,0?q =」+ encodedURL)替換'Uri.parse(encodedURL) – Luksprog 2012-01-29 11:57:26

相關問題