我正在使用WebView
构建一个Android网络应用,我想为其添加历史记录功能。我似乎无法在其他任何地方找到解决方案,所以如果有人能帮助我,那就太好了。如何让应用在新Activity
中保存所有历史记录
(History.java)
?
* activity_main.xml *
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="490dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" />
<Button
android:id="@+id/go"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/button_fix" />
<EditText
android:id="@+id/editText1"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_alignBaseline="@+id/go"
android:layout_alignBottom="@+id/go"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/go"
android:ems="10"
android:hint="@string/type_url" >
<requestFocus />
</EditText>
</RelativeLayout>`
的 MainActivity.java 的
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "blaze web view 2";
private WebView wv;
private Button go;
private EditText url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new BlazeWebViewClient());
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setSupportMultipleWindows(true);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setDisplayZoomControls(false);
try {
wv.loadUrl("http://www.google.com");
} catch (Exception e) {
e.printStackTrace();
}
go = (Button) findViewById(R.id.go);
go.setOnClickListener(this);
url = (EditText) findViewById(R.id.editText1);
url.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.go:
String theWebsite = url.getText().toString();
if ((theWebsite.isEmpty())) {
go.setText("GO");
wv.loadUrl("http://www.google.com");
}
else {
theWebsite = "http://" + url.getText().toString();
wv.loadUrl(theWebsite);
go.setText("Home");
}
url.setText("");
break;
case R.id.editText1:
go.setText("GO");
Log.i("EDITTEXT", "EDIT TEXT WAS CLICKED");
}
}
private class BlazeWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String Url) {
webview.loadUrl(Url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "On Start");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "On Restart");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "On Pause");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "On Resume");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "On Destroy");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "On Stop");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
wv.saveState(outState);
Log.i(TAG, "onSavedInstanceState");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
wv.restoreState(savedInstanceState);
Log.i(TAG, "RestoreInstanceState");
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
back();
return true;
case R.id.forward:
forward();
return true;
case R.id.refresh:
refresh();
return true;
case R.id.chistory:
history();
return true;
case R.id.History:
History();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void History() {
Intent i = new Intent(this, History.class);
startActivity(i);
}
private void history() {
wv.clearHistory();
Log.i("H", "Cleared History");
}
private void refresh() {
wv.reload();
Log.i("R", "Page Reloaded");
}
private void forward() {
wv.goForward();
Log.i("F", "Page Foward");
}
private void back() {
wv.goBack();
Log.i("B", "Page Backward");
}
}
答案 0 :(得分:0)
您是否正在寻找doUpdateVisitedHistory和getVisitedHistory API?第一个让您知道用户何时进入新页面(因此您可以将其保存在历史数据库中),第二个(可选)让您告诉WebView历史数据库的内容,以便访问过的链接具有不同的颜色。然后,您的历史记录活动必须读取您的历史数据库并列出它包含的条目。