how to read browser history in android phone -
i'm working on app in android can read/view browser history date , time stamp (optional) part on thesis project.
help me :(
i tried code , keeps me sending errors
this on main activity.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 bitmap icon=bitmapfactory.decodebytearray(bicon,0,bicon.length); } else{ //default icon history , bookmarks not icons icon=bitmapfactory.decoderesource(getresources(),r.drawable.noicon); } //add lists addtolist(title,url,icon); } //close 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 no bookmark or history.", toast.length_short).show(); } } } public void cleanhb(){ if(browser.canclearhistory(cr)){ browser.clearhistory(cr); //clear history data createlists(); //recreate lists onresume(); //update list } } @override public boolean oncreateoptionsmenu(menu menu) { //inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public boolean onoptionsitemselected(menuitem item){ if(item.getitemid()==r.id.action_clean) { cleanhb(); } return false; } } } }
and on listadaptermodel.java
package com.example.delli5.bclean; /** * created 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 url clickable linkify.addlinks(texturl, linkify.all); return itemview; }
}
try it:
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 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"); } }
}
Comments
Post a Comment