java - Sending information from IntentService to Activity -


my question how can send string "messages" (rises , drops) background running intentservice main activity? want show messages in ui.

i tried use eventbus, because it's needs less code , should theoretically more simple user, unfortunatelly it's crashing app try register subscriber in oncreate method. thanks!

mainactivity.java

import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.view;  public class mainactivity extends activity {   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);  }  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  public void startservice(view view) {     intent intent = new intent(this, myservice.class);     startservice(intent); }  // method stop service public void stopservice(view view) {     stopservice(new intent(getbasecontext(), myservice.class));     system.exit(0); } } 

myservice.java edited

import android.app.intentservice; import android.app.service; import android.content.intent; import android.media.audioformat; import android.media.audiorecord; import android.media.mediarecorder; import android.util.log; import android.widget.toast;    public class myservice extends intentservice {  public myservice() {     super("myservice"); }  @override protected void onhandleintent(intent intent) {      runnable r = new runnable() {         public void run() {              final int recorder_samplerate = 8000;             final int recorder_channels = audioformat.channel_in_mono;             final int recorder_audio_encoding = audioformat.encoding_pcm_16bit;             int frequency;             audiorecord recorder;             int numcrossing, p, numsamples;             short audiodata[];             boolean recording;              int buffersize = audiorecord.getminbuffersize(recorder_samplerate,                     recorder_channels, recorder_audio_encoding);              recorder = new audiorecord(mediarecorder.audiosource.mic,                     recorder_samplerate, recorder_channels, recorder_audio_encoding, buffersize);              recorder.startrecording();             recording = true;             audiodata = new short[buffersize];             int[] values;             int k = 0, t = 0;              values = new int[2];              while (recording) {                 numcrossing = 0;                 numsamples = 0;                 recorder.read(audiodata, 0, buffersize);                 int mod = (buffersize / 4) * 4;                  (p = 0; p < mod; 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++;                     numsamples += 4;                 }                 (p = 0; p < buffersize; p++) {                     if (audiodata[p] > 0 && audiodata[p + 1] <= 0) numcrossing++;                     if (audiodata[p] < 0 && audiodata[p + 1] >= 0) numcrossing++;                     numsamples++;                 }                  frequency = (8000 / numsamples) * numcrossing;                  log.d("proov", string.valueof(frequency));                  if (frequency >= 2550 && frequency <= 2750 && (values[0] != 2)) {                     values[0] = 1;                     values[1] = 2;                     k = 1;                 }//if                  if (frequency <= 3090 && frequency >= 2900 && (values[0] == 0)) {                     values[0] = 2;                     values[1] = 1;                     k = 1;                 }//if                  if (frequency <= 3090 && frequency >= 2900 && (values[0] == 1)) {                     t = 1;                     break;                 }//if                  if (frequency >= 2550 && frequency <= 2750 && (values[0] == 2)) {                     t = 2;                     break;                 }//if                  if (k != 0) {                     k = k + 1;                 }//if                  if (k == 20) {                     values[0] = 0;                     values[1] = 0;                 }//if              }//while              if (t == 1) {                 string message = "rises";                 values[0] = 0;                 values[1] = 0;              }//if              if (t == 2) {                 string message = "drops";                 values[0] = 0;                 values[1] = 0;              }//if         }     };      thread t = new thread(r);     t.start();     //return service.start_sticky; }  @override public void ondestroy() {     super.ondestroy();     toast.maketext(this, "service destroyed", toast.length_long).show(); } 

you can use:

public class apiresultreceive extends resultreceiver {      private final static string tag = "apiresultreceive";     public final static int api_start = 1;     public final static int api_end = 2;     public final static int api_success = 3;     public final static int api_fail = 4;     private receive receive = null;      public interface receive {          public void onreceiveresult(int resultcode, bundle resultdata);     }      public apiresultreceive(handler handler) {         super(handler);     }      public void setreceive(receive receive) {         this.receive = receive;     }      @override     protected void onreceiveresult(int resultcode, bundle resultdata) {         super.onreceiveresult(resultcode, resultdata);         if (receive != null) {             receive.onreceiveresult(resultcode, resultdata);         }     } } 

in service:

@override protected void onhandleintent(intent intent) {     senddata(         intent.getintextra(extra_count, 0),         (resultreceiver) intent.getparcelableextra(extra_receive)     ); }  private void senddata(int count, resultreceiver resultreceive) {     resultreceive.send(apiresultreceive.api_start, null);      (int = 0; < count; i++) {         log.i(tag, "i = " + i);         systemclock.sleep(1000);     }      resultreceive.send(apiresultreceive.api_end, null); }  public static void senddata(context context, int count, apiresultreceive resultreceive) {     intent intent = new intent(context, apiservice.class);     intent.putextra(extra_count, count);     intent.putextra(extra_receive, resultreceive);     context.startservice(intent); } 

and on activity :

public class myactivity extends activity implements apiresultreceive.receive {      private final static string tag = "myactivity";     private apiresultreceive resultreceive = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         log.i(tag, "oncreate");          resultreceive = new apiresultreceive(new handler());         resultreceive.setreceive(this);          //  start send data         apiservice.senddata(this, 10, resultreceive);     }      @override     protected void onresume() {         super.onresume();         log.i(tag, "onresume");     }      @override     protected void ondestroy() {         super.ondestroy();         log.i(tag, "ondestory");         resultreceive.setreceive(null);     }      @override     public void onreceiveresult(int resultcode, bundle resultdata) {         switch (resultcode) {             case apiresultreceive.api_start:                 log.d(tag, "apiresultreceive.api_start");                 break;             case apiresultreceive.api_success:                 log.d(tag, "apiresultreceive.api_success");                 break;             case apiresultreceive.api_fail:                 log.d(tag, "apiresultreceive.api_fail");                 break;             case apiresultreceive.api_end:                 log.d(tag, "apiresultreceive.api_end");                 break;         }     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -