2014-02-07 39 views
3

我是新來的Android,我想創建一個意圖,查看谷歌網站。我的字符串聲明如下:隱含的意圖查看網址

static private final String URL = "http://www.google.com"; 

和我的意圖:

Intent browserIntent = new Intent(Intent.ACTION_VIEW); 
browserIntent.setData(Uri.parse("geo:+URL")); 
startActivity(browserIntent); 

這段代碼顯示在Eclipse中沒有錯誤,但我認爲這可能是錯誤的。

+2

它可能?你有沒有測試過它?結果是什麼? – randomizer

+1

「geo:+ URL」是完全錯誤的。這只是一個字符串,你不是真的在這裏使用你的靜態字段... – 2Dee

回答

5

你沒有正確地構建開放的,試圖串聯2字符串時,使用此:

String s = "I'm a string variable"; 

String concatenated = s + " and I'm another String variable"; 

現在的拼接的內容是

我是一個字符串變量和我「M另一個字符串變量

如果你這樣做:

String concatenated = "s + and I'm another String variable"; 

的拼接的內容是

S +和我

其次,你爲什麼用地理烏里另一個字符串變量?這是查看位置。要查看網站,只需使用URL(並且不要忘記「http://」部分):

String URL = "http://www.google.com"; 
browserIntent.setData(Uri.parse(URL)); 
+0

我可以寫嗎? browserIntent.setData(Uri.parse( 「http://www.google.com」)); – user3284769

+0

不,您不應該遺漏http部分,或者最終會出現錯誤,請使用browserIntent.setData(Uri.parse(「http://google.com」));看看這個問題的細節:http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application?rq=1 – 2Dee

+0

browserIntent.setData(Uri。解析(URL));我已經把它這樣 – user3284769

0

2Dee的回答是正確的。您也可以在一行中提供操作並設置數據uri。查看網站。

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL)); 
startActivity(browserIntent); 
2

您可以創建一個意圖,然後爲它設置數據。意圖還有一個構造函數,它可以採取行動String和數據URI。

當你想在地圖上顯示某些東西時,還需要使用geo:。因此,在瀏覽器中查看網址,您只需使用網站網址即可。您可以將它傳遞給Uri.parse()方法以獲取intents構造函數中所需的URI對象。 你可以簡單地做 -

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
Intent browserChooserIntent = Intent.createChooser(browserIntent , "Choose browser of your choice"); 
startActivity(browserChooserIntent); 
0
Intent browserIntent=null, chooser=null; 
    browserIntent= new Intent(Intent.ACTION_VIEW); 
    browserIntent.setData(Uri.parse("http://www.google.com")); 
    chooser = browserIntent.createChooser(intent,"Open Website Using..."); 
    if(browserIntent.resolveActivity(getPackageManager()) != null){ 
     startActivity(chooser); 
    }