android - How to read sms using Service -


i receiving sms using broadcast receiver.it working fine. want read sms inbox using service, (which received broadcast receiver).

i want retreive sms inbox using service. sms retreival must happen in background not in main thread.no activity should used. //broadcast receiver receive sms , starting service via intent public class smsreceiver extends broadcastreceiver{

@override public void onreceive(context context, intent intent) {     bundle  bundle = intent.getextras();     smsmessage[] message = null;     string str = "";     if(bundle != null){         object[] pdus = (object[])bundle.get("pdus");         message = new smsmessage[pdus.length];         for(int = 0; i<message.length; i++){             message[i] = smsmessage.createfrompdu((byte[])pdus[i]);             str += "new sms cloudy contacts " + message[i].getoriginatingaddress();         }          toast.maketext(context, str, toast.length_long).show();          intent intent1 = new intent(context,myservice.class);         context.startservice(intent1);     }  } 

}

servie public class myservice extends service{ readsms readsms;

@override public ibinder onbind(intent arg0) {        return null; }  public void oncreate(bundle savedinstancestate){     log.d("service","inside oncreate of service");   }  public void ondestroy(){     log.d("service", "destroyed");  }  public void onstart(){     log.d("service","starting service read sms inbox");     toast.maketext(this,"reading sms inbox",toast.length_long).show();     readsms = new readsms();     arraylist list = readsms.readsms("inbox");  }  public class readsms{        public arraylist readsms(string inbox){         arraylist sms = new arraylist();         uri uri = uri.parse("content://sms/inbox");         cursor cursor = getcontentresolver().query(uri, new string[]{"_id","address","date","body"},null,null,null);         cursor.movetolast();         string address = cursor.getstring(1);         string body = cursor.getstring(3);         sms.add(address+" "+body);         return sms;       } } 

}

the messages in inbox wouldn't received broadcast receiver since it's not broadcast. have read native database on device.

take here

edit: first of all, doing service doesn't automatically spawn background thread. service run on main thread.

caution: service runs in main thread of hosting process—the service not create own thread , not run in separate process (unless specify otherwise). means that, if service going cpu intensive work or blocking operations (such mp3 playback or networking), should create new thread within service work. using separate thread, reduce risk of application not responding (anr) errors , application's main thread can remain dedicated user interaction activities.

then can of course spawn thread yourself, or use intentservice. start reading here.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -