android - Using RunOnUI to pass data from a Service to an Activity -
need bit of here, trying create service uses microphone listen peaks in audio frequency. service should run in background , update ui frequency information when there peak. app launches , fc's due seems running out of memory. i've tried debugging , following threads new @ , can't seem decipher doing wrong.
here activity:
public class rtaactivity extends activity { private static final string tag = "rtatest"; private intent intent; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.rta_view); intent = new intent(this, analyzeservice.class); } private broadcastreceiver broadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { android.os.debug.waitfordebugger(); // check logcat updateui(intent); } }; @override public void onresume() { super.onresume(); startservice(intent); registerreceiver(broadcastreceiver, new intentfilter(analyzeservice.analyze_action)); } @override public void onpause() { super.onpause(); unregisterreceiver(broadcastreceiver); stopservice(intent); } private void updateui(intent intent) { string freq = intent.getstringextra("freq"); string db = intent.getstringextra("db"); log.d(tag, freq); log.d(tag, db); textview tvfreq = (textview) findviewbyid(r.id.tv_freq); textview decibels = (textview) findviewbyid(r.id.tv_decibels); tvfreq.settext(freq); decibels.settext(db); } }
and here service:
public class analyzeservice extends service { private static final string tag = "analyzerservice"; public static final string analyze_action = "com.kjacksonmusic.glassrta.displayevent"; private final handler handler = new handler(); intent intent; @override public void oncreate() { super.oncreate(); intent = new intent(analyze_action); } @override public void onstart(intent intent, int startid) { handler.removecallbacks(runonui); //sendupdatestoui handler.postdelayed(runonui, 500); // 0.5 second } private runnable runonui = new runnable() { public void run() { //android.os.debug.waitfordebugger(); // check logcat displayaudioinfo(); handler.postdelayed(this, 1000); // 1 seconds } }; private void displayaudioinfo() { log.d(tag, "entered displayaudioinfo"); int frequency; audiorecord recorder; boolean recording; int numcrossing,p; short audiodata[]; int buffersize = audiorecord.getminbuffersize(8000,audioformat.channel_configuration_mono, audioformat.encoding_pcm_16bit)*3; //get buffer size use audio record recorder = new audiorecord (audiosource.mic,8000,audioformat.channel_configuration_mono, audioformat.encoding_pcm_16bit,buffersize); //instantiate audiorecorder recording=true; //variable use start or stop recording audiodata = new short [buffersize]; //short array pcm data put into. while (recording) { //loop while recording needed if (recorder.getstate()==android.media.audiorecord.state_initialized) // check see if recorder has initialized yet. if (recorder.getrecordingstate()==android.media.audiorecord.recordstate_stopped) recorder.startrecording(); //check see if recorder has stopped or not recording, , make record. else { recorder.read(audiodata,0,buffersize); //read pcm audio data audiodata array //now need decode pcm data using 0 crossings method numcrossing=0; //initialize number of 0 crossings 0 (p=0;p<1000;p+=4) { if (audiodata[p]>0 && audiodata[p+1]<=0) numcrossing++; if (audiodata[p]<0 && audiodata[p+1]>=0) numcrossing++; if (audiodata[p+1]>0 && audiodata[p+2]<=0) numcrossing++; if (audiodata[p+1]<0 && audiodata[p+2]>=0) numcrossing++; if (audiodata[p+2]>0 && audiodata[p+3]<=0) numcrossing++; if (audiodata[p+2]<0 && audiodata[p+3]>=0) numcrossing++; if (audiodata[p+3]>0 && audiodata[p+4]<=0) numcrossing++; if (audiodata[p+3]<0 && audiodata[p+4]>=0) numcrossing++; }//for p (p=(buffersize/4)*4;p<buffersize-1;p++) { if (audiodata[p]>0 && audiodata[p+1]<=0) numcrossing++; if (audiodata[p]<0 && audiodata[p+1]>=0) numcrossing++; } frequency=numcrossing*4; intent.putextra("freq", frequency); intent.putextra("db", 100); ///place holder now. sendbroadcast(intent); }//else recorder started } //while recording if (recorder.getstate()==android.media.audiorecord.recordstate_recording) recorder.stop(); //stop recorder before ending thread recorder.release(); //release recorders resources recorder=null; //set recorder garbage collected. } // run @override public ibinder onbind(intent intent) { return null; } @override public void ondestroy() { handler.removecallbacks(runonui); //sendupdatestoui super.ondestroy(); } }
logcat output:
06-25 21:54:20.380: i/activitymanager(3122): start u0 {cmp=com.kjacksonmusic.glassrta/.rtaactivity} pid 21580 06-25 21:54:20.458: w/resources(3122): converting boolean: typedvalue{t=0x3/d=0x208 "res/anim/accelerate_interpolator.xml" a=1 r=0x10a0005} 06-25 21:54:20.607: i/systemupdateservice(3573): cancelupdate (empty url) 06-25 21:54:20.716: i/systemupdateservice(3573): cancelupdate (empty url) 06-25 21:54:20.833: i/activitymanager(3122): displayed com.kjacksonmusic.glassrta/.rtaactivity: +359ms 06-25 21:54:21.107: d/analyzerservice(21580): entered displayaudioinfo 06-25 21:54:35.161: i/powermanagerservice(3122): going sleep due screen timeout... 06-25 21:54:35.442: d/surfaceflinger(120): screen released, type=0 flinger=0xb882c450 06-25 21:54:35.521: i/windowmanager(3122): screen turned off... 06-25 21:54:35.614: i/headgesturemanager(3122): unregistering listener: com.google.android.glass.server.systemserverhub$4@20ecfce0 head gesture: nudge 06-25 21:54:35.614: d/headgesturecontainer(3122): stopping detector: com.google.android.glass.head.headnudgeupdetector@20ec8be8 name=nudge 06-25 21:54:35.622: d/runnabledetector(3122): run() exiting! 06-25 21:54:35.622: i/mpl-storeload(3122): mpl state size = 5584 06-25 21:54:35.630: e/mpl-storeload(3122): caldata inv_save_mpl_states, size=2 06-25 21:54:35.630: i/mpl-storeload(3122): cal data size write = 5584 06-25 21:54:35.630: i/mpl-storeload(3122): bytes written = 5584 06-25 21:54:35.638: i/headgesturemanager(3122): registering listener: com.google.android.glass.server.systemserverhub$4@20ecfce0 head gesture: global_look_up 06-25 21:54:35.638: v/glassinputhub(3122): cancelling event sequences. 06-25 21:54:35.638: v/glassinputhub(3122): cancelling event sequence: camera key. 06-25 21:54:35.638: v/glassinputhub(3122): cancelling event sequence: power key. 06-25 21:54:35.638: v/glassinputhub(3122): cancelling event sequence: viewfinder switch. 06-25 21:54:35.638: v/glassinputhub(3122): cancelling event sequence: touchpad. 06-25 21:54:35.708: i/voiceengine[20e0026c](3363): screen off , current config not allow screen off. 06-25 21:54:35.724: w/voiceengine[20e0026c](3363): transition voiceconfig [] requested, in config. 06-25 21:54:35.817: i/glassusereventservice[20dd9364](3708): performance stats: [object: com.google.common.logging.glassextensionsnano$glassusereventperformancestats { batterychargewhenfulluah_: 403000 totalkernelms_: 62401830 totalbytessent_: 123808401 bitfield0_: 8191 boardtemperaturemillicentigrade_: 43810 frequencyscalinggovernor_: 0 availablememorykb_: 49148 islowmemory_: false qpassedfractional_: 13445 qpassedinteger_: 16636 reportedsoc_: 95 batterytemperaturemillicentigrade_: 28100 batterystateofchargeuah_: 380000 totalmemorykb_: 596116 unknownfielddata: null cachedsize: -1}] 06-25 21:54:36.028: w/activitymanager(3122): activity pause timeout activityrecord{20ede224 u0 com.kjacksonmusic.glassrta/.rtaactivity t1} 06-25 21:54:39.560: i/gcm(3573): gcm message com.google.glass.home 0:1403747679192023%0#71a16bb9f9fd7ecd 06-25 21:54:39.622: d/connectivityservice(3122): reportnetworkcondition(1, 100) 06-25 21:54:39.630: d/connectivityservice(3122): handleinetconditionchange: net=1, condition=100,mactivedefaultnetwork=1 06-25 21:54:39.630: d/connectivityservice(3122): handleinetconditionchange: starting change hold 06-25 21:54:39.669: v/gcmbroadcastreceiver(3321): onreceive: com.google.android.c2dm.intent.receive 06-25 21:54:39.669: v/gcmbroadcastreceiver(3321): gcm intentservice class: com.google.glass.home.gcmintentservice 06-25 21:54:39.669: v/gcmbaseintentservice(3321): acquiring wakelock 06-25 21:54:39.677: v/gcmbaseintentservice(3321): intent service name: gcmintentservice-229668747847-712 06-25 21:54:39.685: i/gcmintentservice(3321): received message: bundle[{p=timeline_sync, from=229668747847, collapse_key=do_not_collapse}] 06-25 21:54:39.763: v/gcmbaseintentservice(3321): releasing wakelock 06-25 21:54:39.778: i/synchelper(3321): requesting sync [authority=com.google.glass.sync.timeline, syncsource=gcm, extras=bundle[{ignore_backoff=true}]]. 06-25 21:54:39.872: i/timelinesyncadapter(3377): entering onperformsync [authority= com.google.glass.sync.timeline, extras=bundle[{sync_priority=0, ignore_backoff=true}]]. 06-25 21:54:39.911: i/downloadsynchelper(3377): fetching unsynced items server. 06-25 21:54:39.950: i/downloadsynchelper(3377): requesting sync [window=timelinesyncwindow{starttime=1403747642036001, continuationtoken=null}]. 06-25 21:54:39.989: i/timelineitemdatabasehelper(3377): getting unsynced items sync protocols: 0, 1 06-25 21:54:40.067: w/glsuser(3573): googleaccountdataservice.gettoken() 06-25 21:54:40.130: d/connectivityservice(3122): handleinetconditionholdend: net=1, condition=100, published condition=100 06-25 21:54:40.130: d/connectivityservice(3122): sendstickybroadcast: action=android.net.conn.inet_condition_action 06-25 21:54:40.317: i/eyegestureservice(3122): retrieving persistent state of detection gesture don. 06-25 21:54:40.325: i/downloadsynchelper(3377): received items server [count=1]. 06-25 21:54:40.380: i/timelineprovider(3377): overwrote timeline item 8a3d2908-437e-47b7- b985-5c07a99f3e32 because existing modified time (1403742250447) < new modified time (1403747648937) 06-25 21:54:40.505: i/syncstats(3377): download api:582969681302 [numfiles=1, totalbytes=2kb, totalduration=375ms]. 06-25 21:54:40.528: w/activitymanager(3122): sleep timeout! sleeping now. 06-25 21:54:40.911: i/maintimelineview(3321): new timeline database content loaded; updating views. 06-25 21:54:40.942: i/maintimelineview(3321): new timeline database content loaded; updating views. 06-25 21:54:46.028: w/activitymanager(3122): activity stop timeout activityrecord{20ede224 u0 com.kjacksonmusic.glassrta/.rtaactivity t1}
Comments
Post a Comment