我正在开发一款能够读取/查看浏览器历史记录的Android应用 带日期和时间戳(可选) 我的论文项目的一部分。
帮助我:(
我尝试了这段代码,它让我发送错误
这是在主活动.java
上package com.example.delli5.bclean;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.provider.Browser;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> titles;
private ArrayList<String> urls;
private ArrayList<Bitmap> bitmaps;
private ContentResolver cr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createLists();
}
protected void onResume(){
super.onResume();
getBH();
showHistoryBookmarks();
}
public void createLists(){
titles=new ArrayList<String>();
urls=new ArrayList<String>();
bitmaps=new ArrayList<Bitmap>();
}
public void getBH(){
Bitmap icon;
cr=getContentResolver();
String order=Browser.BookmarkColumns.DATE+" DESC";
String[] projection={Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL,Browser.BookmarkColumns.FAVICON};
//String selection=projection[0]+"=?";
//String args[]={"Google"};
Cursor rows=cr.query(Browser.BOOKMARKS_URI,projection, null,null,order);
if(rows.getCount()>0){
while(rows.moveToNext()) {
//read title
String title=rows.getString(rows.getColumnIndex(projection[0]));
//read url
String url=rows.getString(rows.getColumnIndex(projection[1]));
//read icon
byte[] bicon=rows.getBlob(rows.getColumnIndex(projection[2]));
if(bicon!=null){
//convert blob image data to Bitmap
icon=BitmapFactory.decodeByteArray(bicon,0,bicon.length);
}
else{
//default icon for history and bookmarks that do not icons
icon=BitmapFactory.decodeResource(getResources(),R.drawable.noicon);
}
//add to lists
addToList(title,url,icon);
}
//close the cursor
rows.close();
}
}
public void addToList(String title,String url, Bitmap bitmap){
titles.add(title);
urls.add(url);
bitmaps.add(bitmap);
}
public void showHistoryBookmarks(){
ListView l=(ListView) findViewById(R.id.hb_list);
if(l!=null){
if(titles.size()>0){
ListAdapterModel aa=new ListAdapterModel(this,R.layout.listlayout,R.id.hbtitle,titles,urls,bitmaps);
l.setAdapter(aa);
}
else{
Toast.makeText(this, "This is no bookmark or history.", Toast.LENGTH_SHORT).show();
}
}
}
public void cleanHB(){
if(Browser.canClearHistory(cr)){
Browser.clearHistory(cr); //clear history data
createLists(); //recreate the lists
onResume(); //update the list
}
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.action_clean)
{
cleanHB();
}
return false;
}
}
}
}
,这是在listadaptermodel.java
上package com.example.delli5.bclean;
/**
* Created by delli5 on 1/9/2016.
*/
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapterModel {
extends ArrayAdapter<String>
{
int groupid;
ArrayList<String> titles;
ArrayList<String> urls;
ArrayList<Bitmap> bitmaps;
Context context;
String path;
public ListAdapterModel(Context context, int vg, int id, ArrayList<
String > titles, ArrayList < String > urls, ArrayList < Bitmap > bitmaps){
super(context, vg, id, titles);
this.context = context;
groupid = vg;
this.titles = titles;
this.urls = urls;
this.bitmaps = bitmaps;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.icon);
imageView.setImageBitmap(bitmaps.get(position));
TextView textTitle = (TextView) itemView.findViewById(R.id.hbtitle);
String title = titles.get(position);
textTitle.setText(title);
TextView textURL = (TextView) itemView.findViewById(R.id.hburl);
String url = urls.get(position);
textURL.setText(url);
//make the url clickable
Linkify.addLinks(textURL, Linkify.ALL);
return itemView;
}
}
答案 0 :(得分:0)
试一试:
public class TaskReadHistoric extends AsyncTask<Void, Void, Void> {
static final String className = "TaskReadHistoric";
Context context;
String idAparelho;
// String manufacturer = Build.MANUFACTURER;
// String brand = Build.BRAND;
// String product = Build.PRODUCT;
// String model = Build.MODEL;
// http://stackoverflow.com/questions/28040445/android-browser-bookmarks-uri-does-not-work-on-all-devices-how-to-find-out-the
public TaskReadHistoric(Context c) {
context = c;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
Preferences pref = new Preferences(context);
idAparelho = pref.get(Preferences.idAparelho);
}
@Override
protected Void doInBackground(Void... params) {
try {
if (Functions.ConnectedInternet(context)) {
} else {
return null;
}
browserHistoryDOM();
} catch (Exception e) {
//App.makeLog(e);
App.makeLog(e, className, "doInBackground", "erro genérico");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// new TaskReadHistoric(context).execute();
}
private void browserHistoryDOM() {
try {
Cursor mCur = this.context.getContentResolver().query(
Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null,
null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
Pattern p = Pattern.compile(".*?([^.]+\\.[^.]+)");
while (!mCur.isAfterLast()) {
Historico historico = new Historico();
long callDate = 0;
try {
callDate = Long.parseLong(mCur.getString(3));
historico.timestamp = String.valueOf(callDate);
} catch (Exception e) {
App.makeLog(e, className, "browserHistoryDOM", "erro ao converter data do histórico");
}
if (callDate == 0) {
historico.datetime = "Sem registro";
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(callDate);
historico.datetime = dateFormat.format(calendar
.getTime());
}
historico.url = mCur.getString(1);
Matcher m = p.matcher(new URI(historico.url).getHost());
if (m.matches()) {
historico.titulo = m.group(1);
}
if (historico.titulo != null) {
historico.titulo = historico.titulo.replace("#", " - ");
}
if (historico.url != null) {
historico.url = historico.url.replace("#", "-|-|-|-");
historico.url = historico.url.replace("&", "|||||||");
}
if (historico.datetime != null) {
historico.datetime = historico.datetime.replace("#",
" - ");
}
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority(Server.Ip)
.appendPath("get.php")
.appendQueryParameter("action", "setHistorico")
.appendQueryParameter("datetime",
historico.datetime)
.appendQueryParameter("titulo",
historico.titulo)
.appendQueryParameter("timestamp",
historico.timestamp)
.appendQueryParameter("url",
historico.url)
.appendQueryParameter("data", historico.datetime)
.appendQueryParameter(Preferences.idAparelho,
this.idAparelho);
String url = builder.build().toString();
url = url.replace("%40", "@");
url = url.replace(" ", "%20");
url = url.replace("%3A", ":");
url = url.replace("%2F", "/");
String result = JsonHttp.Execute(url);
mCur.moveToNext();
}
}
mCur.close();
} catch (Exception e) {
App.makeLog(e, className, "browserHistoryDOM", "erro generico");
}
}
}