How to fix StrictMode policy violation in android while fetching all mobile number? -
i fetching mobile numbers using cursor query. have enabled strict mode getting exception.
here code fetching mobile number :
public string queryforphonenumber(string contactid) { final string[] projection = new string[] { phone.number, phone.type, }; final cursor phone = getactivity().getcontentresolver().query( phone.content_uri, projection, data.display_name + "=?", new string[]{contactid}, null); if(phone.movetofirst()) { final int contactnumbercolumnindex = phone.getcolumnindex(phone.number); //final int contacttypecolumnindex = phone.getcolumnindex(phone.type); while(!phone.isafterlast()) { mfmfmusermobile = phone.getstring(contactnumbercolumnindex); //final int type = phone.getint(contacttypecolumnindex); phone.movetonext(); } } phone.close(); if(mfmfmusermobile.contains(" ")){ mfmfmusermobile.replace(" ", ""); } //mfmfmusermobile.substring(mfmfmusermobile.length()-10,mfmfmusermobile.length() return mfmfmusermobile; } exception :
04-25 09:51:15.035: d/strictmode(27661): strictmode policy violation; ~duration=31 ms: android.os.strictmode$strictmodediskreadviolation: policy=2335 violation=2 04-25 09:51:15.035: d/strictmode(27661): @ android.os.strictmode$androidblockguardpolicy.onreadfromdisk(strictmode.java:1135) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.sqlite.sqliteconnection.applyblockguardpolicy(sqliteconnection.java:1041) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.sqlite.sqliteconnection.executeforcursorwindow(sqliteconnection.java:842) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.sqlite.sqlitesession.executeforcursorwindow(sqlitesession.java:836) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.sqlite.sqlitequery.fillwindow(sqlitequery.java:62) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.sqlite.sqlitecursor.fillwindow(sqlitecursor.java:144) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.sqlite.sqlitecursor.getcount(sqlitecursor.java:133) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.cursortobulkcursoradaptor.getbulkcursordescriptor(cursortobulkcursoradaptor.java:148) 04-25 09:51:15.035: d/strictmode(27661): @ android.content.contentprovidernative.ontransact(contentprovidernative.java:118) 04-25 09:51:15.035: d/strictmode(27661): @ android.os.binder.exectransact(binder.java:404) 04-25 09:51:15.035: d/strictmode(27661): @ dalvik.system.nativestart.run(native method) 04-25 09:51:15.035: d/strictmode(27661): # via binder call stack: 04-25 09:51:15.035: d/strictmode(27661): android.os.strictmode$logstacktrace 04-25 09:51:15.035: d/strictmode(27661): @ android.os.strictmode.readandhandlebindercallviolations(strictmode.java:1705) 04-25 09:51:15.035: d/strictmode(27661): @ android.os.parcel.readexceptioncode(parcel.java:1447) 04-25 09:51:15.035: d/strictmode(27661): @ android.database.databaseutils.readexceptionfromparcel(databaseutils.java:134) 04-25 09:51:15.035: d/strictmode(27661): @ android.content.contentproviderproxy.query(contentprovidernative.java:413) 04-25 09:51:15.035: d/strictmode(27661): @ android.content.contentresolver.query(contentresolver.java:461) 04-25 09:51:15.035: d/strictmode(27661): @ android.content.contentresolver.query(contentresolver.java:404) 04-25 09:51:15.035: d/strictmode(27661): @ com.newtglobal.fmfm.invitefriendfragement.queryforphonenumber(invitefriendfragement.java:954) 04-25 09:51:15.035: d/strictmode(27661): @ com.newtglobal.fmfm.invitefriendfragement$contactsadapter.bindview(invitefriendfragement.java:747) 04-25 09:51:15.035: d/strictmode(27661): @ android.support.v4.widget.cursoradapter.getview(cursoradapter.java:256) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.abslistview.obtainview(abslistview.java:2255) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.listview.makeandaddview(listview.java:1790) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.listview.filldown(listview.java:691) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.listview.fillgap(listview.java:655) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.abslistview.trackmotionscroll(abslistview.java:5143) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.abslistview.scrollifneeded(abslistview.java:3243) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.abslistview.ontouchmove(abslistview.java:3587) 04-25 09:51:15.035: d/strictmode(27661): @ android.widget.abslistview.ontouchevent(abslistview.java:3431) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.view.dispatchtouchevent(view.java:7736) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2212) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1945) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtransformedtouchevent(viewgroup.java:2218) 04-25 09:51:15.035: d/strictmode(27661): @ android.view.viewgroup.dispatchtouchevent(viewgroup.java:1959) 04-25 09:51:15.035: d/strictmode(27661): @ com.android.internal.policy.impl.phonewindow$decorview.superdispatchtouchevent(phonewindow.java:2068) 04-25 09:51:15.035: d/strictmode(27661): @ com.android.internal.policy.impl.phonewindow.superdispatchtouchevent(phonewindow.java:1515) 04-25 09:51:15.035: d/strictmode(27661): @ android.app.activity.dispatchtouchevent(activity.java:2466) 04-25 09:51:15.035: d/strictmode(27661): @ android.support.v7.app.actionbaractivitydelegateics$windowcallbackwrapper.dispatchtouchevent(actionbaractivitydelegateics.java:268) 04-25 09:51:15.035: d/strictmode(27661): @ c any helpful me. in advance.
you should not perform work on ui thread, instead create worker thread , of work there. simplest solution use asynctask:
public class strictmodeactivity extends appcompatactivity { private static final string tag = strictmodeactivity.class.getsimplename(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); strictmode.setthreadpolicy(new strictmode.threadpolicy.builder() .detectdiskreads() .detectdiskwrites() .detectnetwork() .penaltylog() .penaltydeath() .build()); new queryforphonenumbertask().execute("your contact display name"); } private class queryforphonenumbertask extends asynctask<string, void, string> { @override protected string doinbackground(string... strings) { string mfmfmusermobile = null; string contactid = strings[0]; final string[] projection = new string[]{ contactscontract.commondatakinds.phone.number, contactscontract.commondatakinds.phone.type, }; final cursor phone = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri, projection, contactscontract.data.display_name + "=?", new string[]{contactid}, null); if (phone.movetofirst()) { final int contactnumbercolumnindex = phone.getcolumnindex(contactscontract.commondatakinds.phone.number); //final int contacttypecolumnindex = phone.getcolumnindex(phone.type); while (!phone.isafterlast()) { mfmfmusermobile = phone.getstring(contactnumbercolumnindex); //final int type = phone.getint(contacttypecolumnindex); phone.movetonext(); } } phone.close(); if (mfmfmusermobile != null && mfmfmusermobile.contains(" ")) { mfmfmusermobile.replace(" ", ""); } //mfmfmusermobile.substring(mfmfmusermobile.length()-10,mfmfmusermobile.length() return mfmfmusermobile; } @override protected void onpostexecute(string result) { log.d(tag, result); } } }
Comments
Post a Comment