2017-05-03 36 views
8

我在我的android應用程序中有一個webview。
我已經加載了網址:https://imsnsit.org/imsnsit/notifications.php
有鏈接到各種通知。當我點擊它們時,我的WebView無所事事。 它正在處理所有其他事情 - Chrome(Android),Chrome(桌面)。鏈接很好。我注意到的一件事情是使用PHP文件的href。只是導航到該鏈接不起作用。我得到無效操作。此外,鏈接不是靜態的,每次刷新頁面時都會更改(僅限plum_url.php的參數)。
我已經在使用這些功能。沒什麼幫助。
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setDomStorageEnabled(true);點擊無法在WebView中工作

回答

3

你可以在網頁視圖在新窗口打開這樣

WebView webviewContact=(WebView)findViewById(R.id.webviewcontact); 

webviewContact.loadUrl("https://imsnsit.org/imsnsit/notifications.php"); 

webviewContact.clearCache(true); 

webviewContact.clearHistory(); 
//webviewContact.getSettings().setDomStorageEnabled(true); 
//webviewContact.getSettings().setSupportMultipleWindows(true); 
webviewContact.getSettings().setJavaScriptEnabled(true);  webviewContact.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
webviewContact.setWebViewClient(new WebViewClient(){ 
/*For open new window in webview*/ 
@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) {         Toast.makeText(getApplicationContext(),url,Toast.LENGTH_SHORT).show(); 
       view.loadUrl(url); 
       return false; 
      } 
     }); 
+0

已經試過這個,沒有工作 –

+0

你正在得到什麼問題? – MageNative

+0

當我點擊鏈接它什麼也沒有,這就是問題 –

1

我建議你開始使用自定義選項卡的更快,更好。具體實施是在https://developer.chrome.com/multidevice/android/customtabs

// Use a CustomTabsIntent.Builder to configure CustomTabsIntent. 
// Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent 
// and launch the desired Url with CustomTabsIntent.launchUrl() 

String url = ¨https://paul.kinlan.me/¨; 
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); 
CustomTabsIntent customTabsIntent = builder.build(); 
customTabsIntent.launchUrl(this, Uri.parse(url)); 

自定義選項卡,如果你是從你的Web視圖計劃沒有重定向的應用程序將是一個很好的解決方案。 webview的問題對你的實現非常具體,所以不能在這裏提出soln。請粘貼您如何打開網頁視圖的實施,以及您嘗試打開的php內容或鏈接是什麼。

+0

其實我有我的應用程序中的通知列表。我想在用戶點擊時打開通知。事情是我想用Javascript來打開第一個通知(讓我們說),所以自定義選項卡不是選項 –

5

我檢查了您的鏈接,並且所有鏈接都打開了一個pdf文件,該文件在android web視圖中不受支持。您可以在Google文檔中打開這些鏈接,或者在您的設備中打開某些應用。 實現shouldOverrideUrlLoading像這樣。

@Override 
public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    if (urlIsPDF(url)){ 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.parse(url), "application/pdf"); 
     try{ 
      view.getContext().startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
      //user does not have a pdf viewer installed 
     } 
    } else { 
     webview.loadUrl(url); 
    } 
    return true; 
} 
2

https://www.mkyong.com/android/android-webview-example/
創建兩個Android的佈局文件 - 「RES /佈局/ main.xml中」和「RES /佈局/網頁流量.XML「。

文件:RES /佈局/ main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/buttonUrl" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Go to http://www.google.com" /> 

</LinearLayout> 

文件:RES /佈局/ main.xml中 - 例如web視圖

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/> 

兩個活動類,活動顯示一個按鈕,另一個活動使用預定義的URL顯示WebView。

文件:MainActivity.java

package com.mkyong.android; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    private Button button; 

    public void onCreate(Bundle savedInstanceState) { 
     final Context context = this; 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     button = (Button) findViewById(R.id.buttonUrl); 

     button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
      Intent intent = new Intent(context, WebViewActivity.class); 
      startActivity(intent); 
      } 

     }); 

    } 

} 

文件:WebViewActivity.java

package com.mkyong.android; 

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 

public class WebViewActivity extends Activity { 

    private WebView webView; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webview); 

     webView = (WebView) findViewById(R.id.webView1); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadUrl("http://www.google.com"); 

    } 

} 

的WebView要求INTERNET權限,下面加入到AndroidManifest.xml中。

<uses-permission android:name="android.permission.INTERNET" /> 

File:AndroidManifest.xml - 請參閱完整示例。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.mkyong.android" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".WebViewActivity" 
      android:theme="@android:style/Theme.NoTitleBar" /> 

     <activity 
      android:label="@string/app_name" 
      android:name=".MainActivity" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

它會幫助你。

https://www.mkyong.com/android/android-webview-example/

-1

你試圖打開目標=「_空白」中的Android的WebView鏈接,以便嘗試下面的代碼。

首先補充一點:

webView.setWebChromeClient(webChromeClient); 
webView.getSettings().setSupportMultipleWindows(true); 


    WebChromeClient webChromeClient = new WebChromeClient() { 

    @Override 
    public boolean onCreateWindow(WebView view, boolean isDialog, 
            boolean isUserGesture, Message resultMsg) { 

     WebView.HitTestResult result = view.getHitTestResult(); 
     String data = result.getExtra(); 
     Context context = view.getContext(); 
     Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data)); 
     context.startActivity(browserIntent); 
     return false; 
    } 
}; 

使用這個你可以打開默認的瀏覽器該鏈接。