java - Service insert more than expected on database -
i'm getting contacts calllog , inserting on database using service contentobserver. so, contentobserver keep watching if new changes happens calllog, , if yes, inside method onchange() calls method data in calllog , insert on database.
but, problem that, if make new call, there 1 new data, so, must inserted in database, 1 new row, inserts 3 rows. don't know why. tested on android 2.3 emulator , on emulator ok, on real 2.3 device happens. , in 2.1 emulator happens too.
so here code. service posted. thanks!
here service myapplicationservice.java
public class myapplicationservice extends service { private handler handler = new handler(); private sqlitedatabase db; private openhelper helper; class myapplicationcontentobserver extends contentobserver { public myapplicationcontentobserver(handler h) { super(h); helper = new openhelper(getapplicationcontext()); db = helper.getwritabledatabase(); } @override public boolean deliverselfnotifications() { return true; } @override public void onchange(boolean selfchange) { super.onchange(selfchange); searchinsert(); } } @override public ibinder onbind(intent arg0) { return null; } @override public void oncreate() { db = datahandlerdb.createdb(this); registercontentobservers(); } @override public void ondestroy() { db.close(); } private void searchinsert() { cursor cursor = getcontentresolver().query( android.provider.calllog.calls.content_uri, null, null, null, android.provider.calllog.calls.date + " desc "); if (cursor.movetofirst()) { int numbercolumnid = cursor .getcolumnindex(android.provider.calllog.calls.number); int durationid = cursor .getcolumnindex(android.provider.calllog.calls.duration); int contactnameid = cursor .getcolumnindex(android.provider.calllog.calls.cached_name); int numtypeid = cursor .getcolumnindex(android.provider.calllog.calls.cached_number_type); int calltypeid = cursor .getcolumnindex(android.provider.calllog.calls.type); date dt = new date(); int hours = dt.gethours(); int minutes = dt.getminutes(); int seconds = dt.getseconds(); string currtime = hours + ":" + minutes + ":" + seconds; simpledateformat dateformat = new simpledateformat("m/d/yyyy"); date date = new date(); cursor.movetofirst(); string contactnumber = cursor.getstring(numbercolumnid); string contactname = (null == cursor.getstring(contactnameid) ? "" : cursor.getstring(contactnameid)); string duration = cursor.getstring(durationid); string numtype = cursor.getstring(numtypeid); string calltype = cursor.getstring(calltypeid); contentvalues values = new contentvalues(); values.put("contact_id", 1); values.put("contact_name", contactname); values.put("number_type", numtype); values.put("contact_number", contactnumber); values.put("duration", duration); values.put("date", dateformat.format(date)); values.put("current_time", currtime); values.put("cont", 1); values.put("type", calltype); if (!db.isopen()) { db = getapplicationcontext() .openorcreatedatabase( "/data/data/com.myapp.app/databases/my_database.db", sqlitedatabase.open_readwrite, null); } db.insert(datahandlerdb.table_name_2, null, values); cursor.close(); } } public void registercontentobservers() { this.getapplicationcontext() .getcontentresolver() .registercontentobserver( android.provider.calllog.calls.content_uri, true, new myapplicationcontentobserver(handler)); } }
Comments
Post a Comment