How to stop thread in andriod

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to stop thread in andriod

Postby m.allam » Thu Jan 21, 2010 2:02 pm

Peace upon you,
How to stop thread in andriod?
m.allam
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 17, 2009 5:02 pm

Top

Re: How to stop thread in andriod

Postby m.allam » Mon Jan 25, 2010 9:11 am

I solved this problem using boolean variable set it true when I need to stop thread and false to allow this thread to work beacuase stop is deprecated and destroy doesn't work well,
Enjoy:)
m.allam
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 17, 2009 5:02 pm

Postby Draffodx » Mon Jan 25, 2010 10:55 am

could you possibly post a code snippet showing how you done it?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Postby jwei512 » Mon Jan 25, 2010 11:28 am

Here's one:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private void fireBillImportThread() {
  3.  
  4.         KEYCODE_BACK_PRESSED = false;
  5.  
  6.         bill_t = new Thread() {
  7.  
  8.             public void run() {
  9.  
  10.                 try {
  11.  
  12.                     if (!KEYCODE_BACK_PRESSED) {
  13.  
  14.                         // do work
  15.  
  16.                         while (!KEYCODE_BACK_PRESSED) {
  17.  
  18.                             // do more work
  19.  
  20.                         }
  21.  
  22.                     } else {
  23.  
  24.                         System.out.println("Thread stopped gracefully.");
  25.  
  26.                     }
  27.  
  28.                 } catch (Exception e) {
  29.  
  30.                     e.printStackTrace();
  31.  
  32.                 }
  33.  
  34.             }
  35.  
  36.         };
  37.  
  38.         bill_t.start();
  39.  
  40.  }
  41.  
  42.  
  43.  
  44. @Override
  45.  
  46. public boolean onKeyDown(int keyCode, KeyEvent event) {
  47.  
  48.      if (keyCode == KeyEvent.KEYCODE_BACK) {
  49.  
  50.          KEYCODE_BACK_PRESSED = true;
  51.  
  52.       }
  53.  
  54.       return true;
  55.  
  56. }
  57.  
  58.  
  59.  
  60.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


So you can see how pressing the back button on the phone will set KEYCODE_BACK_PRESSED to true at which point the thread will exit gracefully.
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby m.allam » Mon Jan 25, 2010 12:00 pm

yes as jwei512 worte , another solution in the calling methods that you called it in the run method apply the boolean case. you should note if your thread should stop when action fired instead of exiting program sleep the thread after reset the boolean variable to assure that the thread stop safely.
m.allam
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 17, 2009 5:02 pm

Postby Draffodx » Mon Jan 25, 2010 6:06 pm

EDIT:

I have used:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. record.start();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


instead of

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. record.run();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


And than seems to be working a lot better

------ pre Edit ------

Thanks for that.

I'm trying to run the following code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class LoopProg extends Activity {
  2.  
  3.    
  4.  
  5.    
  6.  
  7.     boolean isRecording; //currently not used
  8.  
  9.  
  10.  
  11.    /** Called when the activity is first created. */
  12.  
  13.    @Override
  14.  
  15.    public void onCreate(Bundle savedInstanceState) {
  16.  
  17.        super.onCreate(savedInstanceState);
  18.  
  19.        setContentView(R.layout.main);
  20.  
  21.        AudioManager audio_service = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  22.  
  23.        audio_service.setSpeakerphoneOn(false);
  24.  
  25.        audio_service.setMode(AudioManager.MODE_IN_CALL);
  26.  
  27.        audio_service.setRouting(AudioManager.MODE_NORMAL,
  28.  
  29.        AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
  30.  
  31.        Record record = new Record();  
  32.  
  33.        record.run();
  34.  
  35.      
  36.  
  37.    }
  38.  
  39.  
  40.  
  41.       public class Record extends Thread
  42.  
  43.       {
  44.  
  45.            
  46.  
  47.  
  48.  
  49.               static final int bufferSize = 200000;
  50.  
  51.               final short[] buffer = new short[bufferSize];
  52.  
  53.               short[] readBuffer = new short[bufferSize];
  54.  
  55.  
  56.  
  57.               public void run() {  
  58.  
  59.                 isRecording = true;
  60.  
  61.                 android.os.Process.setThreadPriority
  62.  
  63.                 (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
  64.  
  65.  
  66.  
  67.                 int buffersize = AudioRecord.getMinBufferSize(11025,
  68.  
  69.                 AudioFormat.CHANNEL_CONFIGURATION_MONO,
  70.  
  71.                 AudioFormat.ENCODING_PCM_16BIT);
  72.  
  73.  
  74.  
  75.                                AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
  76.  
  77.                                                11025,
  78.  
  79.                                                AudioFormat.CHANNEL_CONFIGURATION_MONO,
  80.  
  81.                                                AudioFormat.ENCODING_PCM_16BIT,
  82.  
  83.                                                buffersize);
  84.  
  85.  
  86.  
  87.                                AudioTrack atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
  88.  
  89.                                                11025,
  90.  
  91.                                                AudioFormat.CHANNEL_CONFIGURATION_MONO,
  92.  
  93.                                                AudioFormat.ENCODING_PCM_16BIT,
  94.  
  95.                                                buffersize,
  96.  
  97.                                                AudioTrack.MODE_STREAM);
  98.  
  99.  
  100.  
  101.                                setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
  102.  
  103.                              
  104.  
  105.                            
  106.  
  107.                                atrack.setPlaybackRate(11025);
  108.  
  109.  
  110.  
  111.                                byte[] buffer = new byte[buffersize];
  112.  
  113.                                arec.startRecording();
  114.  
  115.                                atrack.play();
  116.  
  117.  
  118.  
  119.                                while(isRecording) {
  120.  
  121.                                        arec.read(buffer, 0, buffersize);
  122.  
  123.                                        atrack.write(buffer, 0, buffer.length);
  124.  
  125.                                }
  126.  
  127.  
  128.  
  129.                                arec.stop();
  130.  
  131.                                atrack.stop();
  132.  
  133.                                isRecording = false;
  134.  
  135.               }
  136.  
  137.       }
  138.  
  139. }
  140.  
  141.          
Parsed in 0.046 seconds, using GeSHi 1.0.8.4



However the application keeps getting hung up with running a thread like this, I presume I am doing it wrong, from research I need to do it in a background thread?
Draffodx
Master Developer
Master Developer
 
Posts: 205
Joined: Wed Nov 12, 2008 2:31 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests