Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问Android应用程序上的浏览器历史记录_Java_Android_Xml - Fatal编程技术网

Java 访问Android应用程序上的浏览器历史记录

Java 访问Android应用程序上的浏览器历史记录,java,android,xml,Java,Android,Xml,我创建了一个带有edittext、go按钮和history按钮的简单浏览器应用程序。 请建议我需要为history按钮实现什么编码,这样当我按下按钮时,我的浏览历史就会显示在hi.xml布局上。请建议我需要在xml文件和java文件中进行哪些编码更改 MainActivity.java: package com.example.history; import android.os.Bundle; import android.app.Activity; import android.conte

我创建了一个带有edittext、go按钮和history按钮的简单浏览器应用程序。 请建议我需要为history按钮实现什么编码,这样当我按下按钮时,我的浏览历史就会显示在hi.xml布局上。请建议我需要在xml文件和java文件中进行哪些编码更改

MainActivity.java:

package com.example.history;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener{

EditText t;
Button g,h;
WebView w;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    g=(Button) findViewById(R.id.button1);
    h=(Button) findViewById(R.id.button2);
    t=(EditText) findViewById(R.id.editText1);
    w=(WebView) findViewById(R.id.webView1);
    h.setOnClickListener(this);
    g.setOnClickListener(this);
    w.getSettings().setJavaScriptEnabled(true);
    w.getSettings().setLoadWithOverviewMode(true);
    w.getSettings().setUseWideViewPort(true);
    w.setWebViewClient(new Callback());


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId())
    {
    case R.id.button1:
        String c;
        c=t.getText().toString();
        String theWebsite=("http://").concat(c);
        t.setText(theWebsite);
        w.loadUrl(theWebsite);
        InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(w.getWindowToken(),0);


        break;
    case R.id.button2:
        Intent a=new Intent(MainActivity.this,h.class);
        startActivity(a);

        break;



    }


}

}
class Callback extends WebViewClient{   

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    return (false);
}

}
activity_main.xml:

<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="16dp"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="34dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Go" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_centerVertical="true"
    android:text="History" />

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

</LinearLayout>
hi.xml:

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

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

使用网络视图打开URL时,它会存储在手机的网络历史记录中

您可以使用以下代码访问它。稍后将其添加到您在hi.xml中使用的listview中

try{

   Cursor mCur = null;
    try{
     String sortOrder = Browser.BookmarkColumns.DATE + " ASC";
     mCur= context.getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, sortOrder);

     mCur.moveToFirst();
     if (mCur.moveToFirst() && mCur.getCount() > 0) {
      while (mCur.isAfterLast() == false) {   
            String title = mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
            String url = mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
            long date = mCur.getLong(Browser.HISTORY_PROJECTION_DATE_INDEX);  
            mCur.moveToNext();
      }
         }else{
         mCur.close();
        }

    }catch(Exception e){

    }finally{
     mCur.close();
    }

  }catch(Exception e){

  }
您还需要在清单文件中授予以下权限。希望这有帮助

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />

您需要在WebView客户端类上实现onPageFinished方法

您需要将加载的url保存到localdatabase,或者如果您希望应用程序特定会话的历史记录,那么您也可以使用其他结构,如arraylist

class Callback extends WebViewClient{   

   public boolean shouldOverrideUrlLoading(WebView view, String url) {
   return (false);
 }
  @Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

           /// Save the url to the localdatabase or to any structure if you want temporary
   }

}
当用户按下history按钮时,您需要将数据从localdatabase提取到cursor

并使用cursoradapter显示历史url的列表


如果您还有任何疑问,请随时问我……

看起来您发布了一个新的Android项目。您应该删除所有股票向导代码,并向我们显示您编写的代码和您尝试过的内容。我希望我能投两次票来结束这个问题。谢谢你的代码,你能告诉我两件事吗?首先,上面代码中的上下文是什么,以及如何将它链接到hi.xml中的listview。上下文是应用程序上下文,你可以使用getApplicationContext()方法或ClassName.class(例如:Test.class)。我将在下一篇评论中给你一个代码示例给我你的电子邮件id将给你发送java类示例。你可以在这个id中给我发邮件刚刚给你发了一封电子邮件请详细解释,因为我是初学者,关于如何使用游标适配器以及我需要在我的java文件(h.java)中放置什么代码要使用列表视图显示浏览history@krunal首先,您需要了解如何存储数据和从本地数据检索数据。这是一个很好的教程,对于游标适配器,此链接将为您提供示例
class Callback extends WebViewClient{   

   public boolean shouldOverrideUrlLoading(WebView view, String url) {
   return (false);
 }
  @Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

           /// Save the url to the localdatabase or to any structure if you want temporary
   }

}