android - General regarding service class and multithreading -
i running code, user selects date , time. user can select date , time in future. these dates , time stored in sqlite database. after user selects dates , time, activity calls service class, running new thread in following way
public int onstartcommand(intent intent, int flags, int startid) { final alarmmanager alarmmanager = (alarmmanager) getsystemservice(context.alarm_service); final sqlitecontroller db = new sqlitecontroller(getapplicationcontext()); new thread(new runnable() { @override public void run() { list<greetings> greetings = db.getallgreetings(); if (db.getgreetingscount() >= 0) { { (greetings g : greetings) { calendar cal = calendar.getinstance(); ......... .........//other codes
this thread access data database , matches time , date system time , date. 1 date , time matches, using alarm manager broadcast receiver this
if (dnt.equals(cdt)) { intent aint = new intent(getapplicationcontext(), alarmreceiver.class); aint.putextra("msg", msg); aint.putextra("phone", phone); aint.putextra("id", id); pendingintent pendingintent = pendingintent.getbroadcast(getapplicationcontext(), id, aint, pendingintent.flag_update_current); alarmmanager.set(alarmmanager.rtc_wakeup, cal.gettimeinmillis(), pendingintent); db.deletegreetings(g); }
i wanted know, correct way it? when run program in emulator, runs fine, other times shows "application doing work in main thread". so, doing wrong? or there better way it?
since of posted code running in background thread, not cause application doing work in main thread. problem coming source.
you try profiling app using traceview described in android documentation here. try find methods consuming time narrow down search.
as aside, should use intentservice instead of service if service creating single thread running task in background. implement onhandleintent method instead of onstartcommand method. intentservice handle operations receives in single background looper.
Comments
Post a Comment