gpt4 book ai didi

android - 如何一个接一个地连续加载URL

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:53:45 26 4
gpt4 key购买 nike

我想一个一个地加载 URL。我使用 String 数组来存储 URL。我的要求是,如果 webview 加载第一个 url,它应该在页面启动和页面结束时打印消息“page started”应该显示“页面完成”。第一个 url 加载完成后,它应该加载第二个 URL 并继续相同的过程。

我写的代码如下:

package com.browser;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class browser extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String[] url={"http://www.yahoo.com","http://www.google.com","http://www.ibnlive.com"};
final MyWebView mwv = new MyWebView(this);
mwv.setWebViewClient(new myweb());
new Thread(new Runnable(){
public void run(){
Log.d("runThread","runthread");
for(int i=0;i<2;i++){
openbrowser(url[i]);
}

}

private void openbrowser(String url) {

mwv.getSettings().setJavaScriptEnabled(true);
mwv.loadUrl(url);
Log.d("",""+url);
setContentView(mwv);
}
}).start();
}


public class MyWebView extends WebView{

public MyWebView(Context context) {
super(context);

}
}
public class myweb extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("LOADING");

return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

System.out.println("PageStarted: " + url);
}

@Override
public void onPageFinished(WebView view, String url){
System.out.println("PageFinished: " + url);
}
}


}
///indented code <--remove this

它只加载最后一个 URL。

最佳答案

您可以使用以下代码在 webview 中逐个加载 url。此代码只是将您只能看到或看不到所有网址但您可以看到最后一个的网址一个一个地加载。

public class WebViewsScreenActivity extends Activity {

private WebView mwebview;
int i =0;
private WebViewsScreenActivity _activity;
ProgressDialog _dilog;
private String[] Urls = {"http://www.google.com","http://www.gmail.com","http://www.yahoo.com"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
mwebview = new WebView(this);
setContentView(mwebview);
_activity = this;
mwebview.getSettings().setJavaScriptEnabled(true);
mwebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

if(checkInternetConnection(_activity)){
if(savedInstanceState==null)
mwebview.loadUrl(Urls[i]);
else
mwebview.restoreState(savedInstanceState);
}
else{
//showAlert "Unable to Connect Server"
}
mwebview.setWebChromeClient(new WebChromeClient() {

@Override
public void onProgressChanged(WebView view, int progress) {
if(mwebview.getVisibility()==View.VISIBLE)
{
WebViewsScreenActivity.this.setProgress(progress * 100);

}
}
});
mwebview.setWebViewClient(new HelloWebViewClient());
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
mwebview.goBack();
return true;
}
else
return super.onKeyUp(keyCode, event);
}


//To check whether network connection is available on device or not
private boolean checkInternetConnection(Activity _activity) {
ConnectivityManager conMgr = (ConnectivityManager) _activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
return true;
else
return false;
}//checkInternetConnection()



//HelloWebViewClient class for webview
private class HelloWebViewClient extends WebViewClient {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
Toast.makeText(getApplicationContext(), "Loading started...!"+Urls[i], Toast.LENGTH_SHORT).show();

}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);

}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(), "Loading done...!"+Urls[i], Toast.LENGTH_SHORT).show();
i++;
if(i<Urls.length)
view.loadUrl(Urls[i]);
}
} //HelloWebViewClient-class
}//AccountsScreenActivity-class

在 manifeast 文件中添加如下权限:

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

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".WebViewsScreenActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

关于android - 如何一个接一个地连续加载URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5297535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com