android - Cannot get sent sms (text messages) from device -
i sent sms (text messages) device. can of them inbox with:
public list<sms> getallinboxsms(context ctx) { list<sms> inboxsmslist = new arraylist<>(); try { uri urisms = uri.parse("content://sms/inbox"); cursor c = ctx.getcontentresolver().query(urisms, new string[]{"_id", "thread_id", "address", "person", "date", "body"}, "read=0", null, null); if (c != null && c.movetofirst()) { { inboxsmslist.add(new sms(c)); } while (c.movetonext()); } } catch (exception e) { log.e("getallinboxsms", e.tostring()); } log.i("inbox", "size: " + inboxsmslist.size()); log.i("inbox", inboxsmslist.tostring()); return inboxsmslist; }
however if modify
uri.parse("content://sms/inbox");
to
uri.parse("content://sms/sent");
the returned list size 0.
i tried set app as default sms app doesn't work in way either.
i work on lollipop (android 5).
please if can.
to read messages inbox or sent programmatically in android use following function.
public list<sms> getallsms(string foldername) { list<sms> lstsms = new arraylist<sms>(); sms objsms = new sms(); uri message = uri.parse("content://sms/"+foldername); contentresolver cr = mactivity.getcontentresolver(); cursor c = cr.query(message, null, null, null, null); mactivity.startmanagingcursor(c); int totalsms = c.getcount(); if (c.movetofirst()) { (int = 0; < totalsms; i++) { objsms = new sms(); objsms.setid(c.getstring(c.getcolumnindexorthrow("_id"))); objsms.setaddress(c.getstring(c .getcolumnindexorthrow("address"))); objsms.setmsg(c.getstring(c.getcolumnindexorthrow("body"))); objsms.setreadstate(c.getstring(c.getcolumnindex("read"))); objsms.settime(c.getstring(c.getcolumnindexorthrow("date"))); lstsms.add(objsms); c.movetonext(); } } // else { // throw new runtimeexception("you have no sms in " + foldername); // } c.close(); return lstsms;}
call above function messages inbox or send folder.
getallsms("inbox"); // sms inbox getallsms("sent"); // sms sent
to sms inbox or sent folder use following function
public list<sms> getallsms() { list<sms> lstsms = new arraylist<sms>(); sms objsms = new sms(); uri message = uri.parse("content://sms/"); contentresolver cr = mactivity.getcontentresolver(); cursor c = cr.query(message, null, null, null, null); mactivity.startmanagingcursor(c); int totalsms = c.getcount(); if (c.movetofirst()) { (int = 0; < totalsms; i++) { objsms = new sms(); objsms.setid(c.getstring(c.getcolumnindexorthrow("_id"))); objsms.setaddress(c.getstring(c .getcolumnindexorthrow("address"))); objsms.setmsg(c.getstring(c.getcolumnindexorthrow("body"))); objsms.setreadstate(c.getstring(c.getcolumnindex("read"))); objsms.settime(c.getstring(c.getcolumnindexorthrow("date"))); if (c.getstring(c.getcolumnindexorthrow("type")).contains("1")) { objsms.setfoldername("inbox"); } else { objsms.setfoldername("sent"); } lstsms.add(objsms); c.movetonext(); } } // else { // throw new runtimeexception("you have no sms"); // } c.close(); return lstsms;}
sms class below:
public class sms{ private string _id; private string _address; private string _msg; private string _readstate; //"0" have not read sms , "1" have read sms private string _time; private string _foldername; public string getid(){ return _id; } public string getaddress(){ return _address; } public string getmsg(){ return _msg; } public string getreadstate(){ return _readstate; } public string gettime(){ return _time; } public string getfoldername(){ return _foldername; } public void setid(string id){ _id = id; } public void setaddress(string address){ _address = address; } public void setmsg(string msg){ _msg = msg; } public void setreadstate(string readstate){ _readstate = readstate; } public void settime(string time){ _time = time; } public void setfoldername(string foldername){ _foldername = foldername; } }
don’t forget define following permission in androidmanifest.xml
<uses-permission android:name="android.permission.read_sms" />
for marshmallow device support check runtime permission
for reference runtime permission
Comments
Post a Comment