Tuesday, February 18, 2014

Brother novice, please take a look at this thread I practice how to destroy it?


I want to jump in the Test inside destroyed or closed when the thread is created new Thread (), do not know how to operate, we hope the wing ah! ! !
Code is as follows:

public class Test extends Activity implements OnClickListener {

private Button btnTest;
public final static int LOAD_PROGRESS = 0;

Handler handler = new Handler () {
public void handleMessage (Message msg) {
switch (msg.what) {
case LOAD_PROGRESS:

Toast.makeText (getApplicationContext (), (String) msg.obj, Toast.LENGTH_SHORT) show ();.

break;

}
super.handleMessage (msg);
}
};
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

btnTest = (Button) findViewById (R.id.btnSysconTest);
btnTest.setOnClickListener (this);

}

@ Override
public void onClick (View v) {
switch (v.getId ()) {
case R.id.Test:

Test ();

break;

}
}
private void Test ()
{

new Thread ()
{
public void run ()
{
Message msg = new Message ();
-------- Some operations ------------
msg.what = LOAD_PROGRESS;
msg.obj = "aaaaaaa";
handler.sendMessage (msg);
}
.} Start ();
}
@ Override
protected void onDestroy () {
/ / Is here to destroy the thread off? ? ? How to do it? ? ?
super.onDestroy ();
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Originally handler.removeCallbacks (thread name); But you do not define your thread, how are you onDestroy () method which destroyed
Reply:
@ Heaimnmn I am a new Thread () this definition, if you press your method should give an example how to define it not?
Reply:
Suggested
 private void Test () 
{

new Thread ()
{
public void run ()
{
Message msg = new Message ();
-------- Some operations ------------
msg.what = LOAD_PROGRESS;
msg.obj = "aaaaaaa";
handler.sendMessage (msg);
}
.} Start ();
}
Inside this new Thread () Do not write in internal class which define a thread on the outside, you can (inside) method call removeCallbacks onDestroy
Reply:
Write on the outside
 Runnable mBackgroundRunnable = new Runnable () {

@ Override
public void run () {
{
Message msg = new Message ();
-------- Some operations ------------
msg.what = LOAD_PROGRESS;
msg.obj = "aaaaaaa";
handler.sendMessage (msg);
}
}
};

Reply:
new Thread (mBackgroundRunnable) start ().;
Reply:
boolean isStop = false;
Thread test = new Thread () {
public void run () {
while (! isStop) {
Logic processing
}
}
};


In the onDestroyisStop = true;
test.interrupt ();
Reply:
For example, in a class variable written inside pravite Handler handler; invoked where needed to write, handler.post (mBackgroundRunnable) enabled thread, handler.removeCallbacks (mBackgroundRunnable) destroy thread
Reply:
reference to the 6th floor lieri111 reply:
boolean isStop = false;
Thread test = new Thread () {
public void run () {
while (! isStop) {
Logic processing
}
}
};


In the onDestroyisStop = true;
test.interrupt ();
isStop = true; test.interrupt () will not close all the threads?
Reply:
reference to the 8th floor heaimnmn reply:
Quote: references to the 6th floor lieri111 reply:

boolean isStop = false;
Thread test = new Thread () {
public void run () {
while (! isStop) {
Logic processing
}
}
};


In the onDestroyisStop = true;
test.interrupt ();
isStop = true; test.interrupt () will not close all the threads?

Just close the test thread, if all, of the need to destroy a single
Reply:
You need to destroy this thread do? They will destroy it
Reply:
Thread can be destroyed?
This code is what you need to do ...
Reply:
Thank you to the sub knot Posts
Reply:
Set flag bit flag
while (flag) {

}
Destruction put the flag is set to false

No comments:

Post a Comment