導航:首頁 > APP軟體 > 安卓打開webview中的url

安卓打開webview中的url

發布時間:2023-07-29 04:02:51

① android手機怎麼才能直接打開URL鏈接文件

安卓代碼中調用瀏覽器來打開相應的網頁,一般有以下幾種方式

② android下打開Web瀏覽器的幾種常見的方法

android下打開Web瀏覽器的幾種常見的方法如下:

一。通過意圖實現瀏覽

//通過下述方法打開瀏覽器

privatevoidopenBrowser(){
//urlText是一個文本輸入框,輸入網站地址
//Uri是統一資源標識符
Uriuri=Uri.parse(urlText.getText().toString());
Intentintent=newIntent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}

注意:輸入URL時,不要忘記「http://」部分。

二。利用視圖打開網頁,是通過調用WebKit瀏覽器引擎提供的WebView實現的。

具體源代碼如下:

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayoutandroid:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="@+id/etWebSite"
android:hint="輸入網址"
android:singleLine="true"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/searchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
android:layout_marginRight="5dp"
/>
</LinearLayout>
<LinearLayoutandroid:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一頁"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一頁"
android:layout_marginRight="5dp"
/>
</LinearLayout>
<WebViewandroid:id="@+id/webView1"android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
/res/src/com.myandroid


packagecom.myandroid;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.webkit.URLUtil;
importandroid.webkit.WebView;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
{

privateButtonschBtn,backBtn,nextBtn;
privateWebViewwebView;
privateEditTextmText;

@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

schBtn=(Button)findViewById(R.id.searchBtn);
mText=(EditText)findViewById(R.id.etWebSite);
webView=(WebView)findViewById(R.id.webView1);
backBtn=(Button)findViewById(R.id.backBtn);
nextBtn=(Button)findViewById(R.id.nextBtn);
schBtn.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
//設置可以使用Javascript
webView.getSettings().setJavaScriptEnabled(true);StringstrURI=mText.getText().toString();
//檢測網站的合法性
if(URLUtil.isNetworkUrl(strURI)){
webView.loadUrl(strURI);
Toast.makeText(WebViewActivity.this,strURI,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(WebViewActivity.this,"輸入非法網站 "+strURI,Toast.LENGTH_SHORT).show();
}
}
});

backBtn.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
if(webView.canGoBack()){
webView.goBack();
}
}
});

nextBtn.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){
//TODOAuto-generatedmethodstub
if(webView.canGoForward()){
webView.goForward();
}
}
});
}
}


同時還要在AndroidManifest.xml中添加訪問網際網路的許可權:

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

③ android webview載入url怎麼緩存

當我們載入Html時候,會在我們data/應用package下生成database與cache兩個文件夾:

我們請求的Url記錄是保存在webviewCache.db里,而url的內容是保存在webviewCache文件夾下.

WebView中存在著兩種緩存:網頁數據緩存(存儲打開過的頁面及資源)、H5緩存(即AppCache)。


一、網頁緩存


1、緩存構成

/data/data/package_name/cache/

/data/data/package_name/database/webview.db

/data/data/package_name/database/webviewCache.db



綜合可以得知 webview 會將我們瀏覽過的網頁url已經網頁文件(css、圖片、js等)保存到資料庫表中


緩存模式(5種)

LOAD_CACHE_ONLY: 不使用網路,只讀取本地緩存數據

LOAD_DEFAULT: 根據cache-control決定是否從網路上取數據。

LOAD_CACHE_NORMAL: API level 17中已經廢棄, 從API level 11開始作用同LOAD_DEFAULT模式

LOAD_NO_CACHE: 不使用緩存,只從網路獲取數據.

LOAD_CACHE_ELSE_NETWORK,只要本地有,無論是否過期,或者no-cache,都使用緩存中的數據。

如:www.taobao.com的cache-control為no-cache,在模式LOAD_DEFAULT下,無論如何都會從網路上取數據,如果沒有網路,就會出現錯誤頁面;在LOAD_CACHE_ELSE_NETWORK模式下,無論是否有網路,只要本地有緩存,都使用緩存。本地沒有緩存時才從網路上獲取。

www.360.com.cn的cache-control為max-age=60,在兩種模式下都使用本地緩存數據。


扒棚歲總結:根據以上兩種模式,建議緩存策略為,判斷是否有網路,有的話,使用LOAD_DEFAULT,無網路時,使用LOAD_CACHE_ELSE_NETWORK。

設置WebView緩存模式

privatevoidinitWebView(){

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//設置緩存模式
//開啟DOMstorageAPI功能
mWebView.getSettings().setDomStorageEnabled(true);
//開啟databasestorageAPI功能
mWebView.getSettings().setDatabaseEnabled(true);
StringcacheDirPath=getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME;
//StringcacheDirPath=getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
Log.i(TAG,"cacheDirPath="+cacheDirPath);
//設置資料庫緩存路徑
mWebView.getSettings().setDatabasePath(cacheDirPath);
//設置ApplicationCaches緩存目錄
mWebView.getSettings().setAppCachePath(cacheDirPath);
//開啟ApplicationCaches功能
mWebView.getSettings().setAppCacheEnabled(true);
}
清除緩存

/**
和祥*清除WebView緩存
*/
publicvoidclearWebViewCache(){

//清理Webview緩存資料庫
try{
deleteDatabase("webview.db");
春睜deleteDatabase("webviewCache.db");
}catch(Exceptione){
e.printStackTrace();
}

//WebView緩存文件
FileappCacheDir=newFile(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
Log.e(TAG,"appCacheDirpath="+appCacheDir.getAbsolutePath());

FilewebviewCacheDir=newFile(getCacheDir().getAbsolutePath()+"/webviewCache");
Log.e(TAG,"webviewCacheDirpath="+webviewCacheDir.getAbsolutePath());

//刪除webview緩存目錄
if(webviewCacheDir.exists()){
deleteFile(webviewCacheDir);
}
//刪除webview緩存緩存目錄
if(appCacheDir.exists()){
deleteFile(appCacheDir);
}
}

④ android 裡面如何獲得第三方app中webview打開的url地址

  1. 用Xposed自己寫攔截

  2. 用chrome://inspect/#devices(不一定能用)

  3. 抓包軟體,比如packet capture

⑤ android用WebView顯示本地網頁webview.loadUrl("file:///android_asset/haitian.htm"); 圖片顯示不出來。

webView顯示不了圖片,那肯定是html中的圖片引入寫錯了,WebView(網路視圖)能載入顯示網頁,可以將其視為一個瀏覽器。它使用了WebKit渲染引擎載入顯示網頁。
如果顯示有問題,建議按以下方式去實現:
第一種方法的步驟:
1.在要Activity中實例化WebView組件:WebView webView = new WebView(this);
2.調用WebView的loadUrl()方法,設置WevView要顯示的網頁:
互聯網用:webView.loadUrl("http://www.google.com");
本地文件用:webView.loadUrl("file:///android_asset/XX.html"); 本地文件存放在:assets 文件中
3.調用Activity的setContentView( )方法來顯示網頁視圖
4.用WebView點鏈接看了很多頁以後為了讓WebView支持回退功能,需要覆蓋覆蓋Activity類的onKeyDown()方法,如果不做任何處理,點擊系統回退剪鍵,整個瀏覽器會調用finish()而結束自身,而不是回退到上一頁面
5.需要在AndroidManifest.xml文件中添加許可權,否則會出現Web page not available錯誤。
<uses-permission android:name="android.permission.INTERNET" />

第二種方法的步驟:
1、在布局文件中聲明WebView
2、在Activity中實例化WebView
3、調用WebView的loadUrl( )方法,設置WevView要顯示的網頁
4、為了讓WebView能夠響應超鏈接功能,調用setWebViewClient( )方法,設置 WebView視圖
5、用WebView點鏈接看了很多頁以後為了讓WebView支持回退功能,需要覆蓋覆蓋Activity類的onKeyDown()方法,如果不做任何處理,點擊系統回退剪鍵,整個瀏覽器會調用finish()而結束自身,而不是回退到上一頁面
6、需要在AndroidManifest.xml文件中添加許可權,否則出現Web page not available錯誤。
<uses-permission android:name="android.permission.INTERNET"/>

閱讀全文

與安卓打開webview中的url相關的資料

熱點內容
trrd資料庫是什麼 瀏覽:630
電腦上的文件在哪裡弄 瀏覽:430
導信息到app為什麼要u盤 瀏覽:488
js限制輸入字數 瀏覽:776
protel99se詳細教程 瀏覽:631
數據鏈路層按什麼傳輸 瀏覽:235
網路借貸存在哪些主要模式 瀏覽:205
鄭州孩子去哪裡學編程 瀏覽:477
鄭州網路技術有限公司怎麼樣 瀏覽:331
瀏覽器清理工具 瀏覽:723
網路適配器電源管理 瀏覽:261
c盤系統文件損壞win10 瀏覽:364
速達h5賬號密碼 瀏覽:345
哪個網站有微博數據 瀏覽:47
易觀大數據官網怎麼用 瀏覽:347
uibot怎麼設置編程語言 瀏覽:610
tt2文件可以刪除嗎 瀏覽:254
手機酷狗app怎麼關閉開機啟動 瀏覽:248
微信一元購平台 瀏覽:482
蘋果賬號玩安卓游戲嗎 瀏覽:688

友情鏈接