2017-04-20 55 views
0

我在Android Studio中創建了一個運行Web應用程序的應用程序。在那個應用程序中,有一些我希望從Chrome瀏覽器打開的鏈接不在應用程序的webview中。試圖在Android應用程序中打開Chrome中打開的外部URL鏈接

我已經添加了我已經在這裏查看過的鏈接,並試圖添加到我的代碼中,但目前鏈接仍然在我的應用程序中打開,而不是在Chrome中,我是否錯過了某些明顯的內容?謝謝。

文章,我添加的代碼從

WebView link click open default browser

代碼爲我的應用程序:

import android.content.Intent; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class MainActivity extends AppCompatActivity { 

WebView tpappview; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    setPage(); 
} 

private void setPage(){ 

    tpappview = (WebView) findViewById(R.id.tpViewId); 
    WebSettings tpsetting =tpappview.getSettings(); 
    tpsetting.setJavaScriptEnabled(true); 
    tpappview.loadUrl("http://example.com/Login"); 
    tpappview.setWebViewClient(new WebViewClient()); 
} 

private class MyWebViewClient extends WebViewClient { 
    @SuppressWarnings("deprecation") 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     if (Uri.parse(url).getHost().contains 
("http://example.com")) { 
      // This is my web site, so do not override; let my WebView load 
the page 
      return false; 
     } 
     // Otherwise, the link is not for a page on my site, so launch 
another Activity that handles URLs 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(intent); 
     return true; 
    } 
} 

@Override 
public void onBackPressed() { 

    if (tpappview.canGoBack()) 
     tpappview.goBack(); 
    else 
    super.onBackPressed(); 
} 

}

回答

0

通過包

String url = "http://www.example.com"; 
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    i.setPackage("com.android.chrome"); 
    try { 
     startActivity(i); 
    } catch (ActivityNotFoundException e) { 
     // Chrome is probably not installed 
     // Try with the default browser 
     i.setPackage(null); 
     startActivity(i); 
    } 

通過方案

String url = "http://www.example.com"; 
try { 
    Uri uri = Uri.parse("googlechrome://navigate?url=" + url); 
    Intent i = new Intent(Intent.ACTION_VIEW, uri); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(i); 
} catch (ActivityNotFoundException e) { 
    // Chrome is probably not installed 
} 

或者:

String url = "http://www.example.com"; 
    try { 
     Intent i = new Intent("android.intent.action.MAIN"); 
     i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main")); 
     i.addCategory("android.intent.category.LAUNCHER"); 
     i.setData(Uri.parse(url)); 
     startActivity(i); 
    } 
    catch(ActivityNotFoundException e) { 
     // Chrome is probably not installed 
    } 
相關問題