2016-02-27 32 views
1

我剛開始開發,爲我的第一個應用程序,我正在嘗試製作一個Web瀏覽器。我的問題是我不能讓我的webview從共享的意圖加載URL。我得到的只是一個空白頁面。我花了幾天的時間搜索網頁,任何幫助將不勝感激。先謝謝你!如何讓我的webview加載從另一個應用程序共享的網址?

我的代碼,以獲得意圖(在OnCreate中和的onResume)

Intent receivedIntent = getIntent(); 
     String receivedAction = receivedIntent.getAction(); 
     String receivedType = receivedIntent.getType(); 
     try { 
      if (receivedAction.equals(Intent.ACTION_VIEW)) { 
       String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT); 
      if (receivedText != null) { 
       if (receivedType.equals("text/plain")) { 
        webView.loadUrl(receivedText); 
       } 
      } 

我的清單

<intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:scheme="http" /> 
      <data android:scheme="https" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.SEND" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <data android:mimeType="text/plain" /> 

     </intent-filter> 

    </activity> 
+0

你登錄的receivedText,以確保它是一個有效的網址? – chRyNaN

+0

@chRyNaN好想法!繼承人我的日誌。 –

+0

I/CLOCKS:CLOCKS Intent {act = android.intent.action.VIEW dat = http://www.cbc.ca/news/business/television-changes-channels-skinny-1.3465542?cmp = rss I/CLOCKS: CLOCKS android.intent.action.VIEW I/CLOCKS:CLOCKS null –

回答

0

你可以做這樣的事情

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="org.sakaiproject.sakai.WebViewActivity" > 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ProgressBar 
     android:id="@+id/webview_progressbar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="match_parent" 
     android:layout_height="4dp" 
     android:layout_alignParentTop="true" 
     android:focusable="false" /> 
</RelativeLayout> 

現在的Activity你必須得到的網址與getDataString()

public class WebViewActivity extends AppCompatActivity { 
    private WebView webView; 
    private ProgressBar progressBar; 
    private String url = null; 

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

     // get the url 
     url = getIntent().getDataString(); 
     progressBar = (ProgressBar) findViewById(R.id.webview_progressbar); 
     webView = (WebView) findViewById(R.id.webview); 
     webView.setWebChromeClient(new WebChromeClient() { 

      @Override 
      public void onProgressChanged(WebView view, int progress) { 
     // update progressbar 
       progressBar.setProgress(progress); 
      } 
     }); 
     webView.setWebViewClient(new WebClient()); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadUrl(url); 
    } 

    @Override 
    public void onBackPressed() { 
     if (webView.canGoBack()) 
      webView.goBack(); 
     else 
      super.onBackPressed(); 
    } 

    public class WebClient extends WebViewClient { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      progressBar.setVisibility(View.GONE); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // return super.shouldOverrideUrlLoading(view, url); 
      progressBar.setVisibility(View.VISIBLE); 
      view.loadUrl(url); 
      return false; 
     } 

    } 
} 

和清單

<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <data android:scheme="http" /> 
    <data android:scheme="https" /> 
</intent-filter> 
+0

在你的代碼中,url = getIntent()。getDataString();什麼是「網址」? –

+0

呵呵我忘了宣佈它...這是一個字符串 –

+0

這樣做了!非常感謝! –

相關問題