My specific practices: an activity and a key thread thread. In the activity start thread. I think the thread is a child thread, so thread update adds the operation of the interface, and finally there is no error. Do not thread the child thread?
------ Solution ---------------------------------------- ----
you use thread.start () method yet? Inside or directly implemented runable interface, use the former certainly played another thread, the latter may still be inside the main thread, use this method Thread.currentThread (). GetId (), look at the log inside print thread id and the main thread id is the same, you know you have to create a new thread, the other, which is not in the child thread to operate on the ui. You may not create a new thread, so there is no error.
------ Solution ---------------------------------------- ----
first floor of positive solutions, thread tag is ID
------ Solution ------------------------ --------------------
in the main thread calls Thread.currentThread (), the main thread handle preserved.
the need to compare the place, the same call Thread.currentThread (), to get the current thread handle, and then can be compared.
------ Solution ---------------------------------------- ----
should be said that the main thread calls onCreat () method!
------ Solution ---------------------------------------- ----
handle mechanism is to avoid sub-thread operation ui and design. Because the handle inside the ui action is in the main thread, the landlord if you need a simple thread operations can be considered AsyncTask. This relatively simple to achieve.
------ Solution ---------------------------------------- ----
My understanding is that the main thread calls oncreate () function creates activity and a series of interfaces
------ Solution -------------- ------------------------------
can put your test code stickers under discussion easier to describe the problem < br> ------ Solution ----------------------------------------- ---
studied. . mark
------ reference ---------------------------------------
start threads, and then I started two threads in the thread which set up a delay waiting, waiting for the end and then change the UI, this time will be given, followed by a handler for UI interface update on not being given, and this time there should communicate. But there are two threads to wait, then there is no delay in the update operation directly inside the thread, AP start generating interface, UI was later updated threads started operations, there is no error, that it could not understand it.
------ reference --------------------------------------- < br> can look at the Activity class, runOnUIThread () method implementation.
------ reference --------------------------------------- < br>
view the ID, implementation ruanable and thread.start () has a different thread ID. I would like to ask activity of oncreat () method is not activated the main UI thread? Thank you, ha ha.
------ reference --------------------------------------- < br>
I just want to experience the next update UI thread operations, wanted him to be an error, and now has error, but the new problem is that in the main thread on the child thread, the child thread has right UI operations, but there is no delay to wait, the system is operating normally, there is no error prompted. Do not know how it is. . .
------ reference --------------------------------------- < br>
I looked at the life cycle of activity, activity thread is onCreat () method calls the beginning, but in the onCreat () method before the main thread exist yet?
------ reference --------------------------------------- < br>
public class DrawPicturesActivity extends Activity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
.
.
.
new Thread(pthread).start();
nThread.start();
.
.
.
}
.
.
.
}
public class PictureThread implements Runnable{
.
.
.
public PictureThread(DrawPicturesActivity context)
{
tView = (EditText)context.findViewById(R.id.editText1);
}
private Handler threadHandler = new Handler(Looper.myLooper())
{
public void handleMessage(Message msg)
{
tView.setText("I can come to this room quietly.");
}
};
public void run()
{
Message msg = threadHandler.obtainMessage();
tView.setText("I can come to this room quietly. is that right?");//该操作能成功执行
try
{
Thread.sleep(5000);
}
catch (Exception e)
{
}
//tView.setText("I can come to this room quietly. is that right?
second");该操作不能成功执行
threadHandler.sendEmptyMessage(1);
}
}
public class NewThread extends Thread{
.
.
.
public NewThread(DrawPicturesActivity context)
{
tView = (EditText)context.findViewById(R.id.editText1);
}
private Handler threadHandler = new Handler(Looper.myLooper())
{
public void handleMessage(Message msg)
{
tView.setText("I can come to this room loudly.");
}
};
public void run()
{
Message msg = threadHandler.obtainMessage();
try
{
Thread.sleep(3000);
}
catch (Exception e)
{
}
threadHandler.sendEmptyMessage(1);
//tView.setText("The second way to this room!");该操作不能成功执行
}
}
------ reference ----------------------------------- ----
I started in activity in the two threads in the thread pictureThread, in the delay waiting for the operation of the UI before the successful execution of the operation after the thread waits fails (be annotated The operation). nThread results and pictureThread almost.
------ reference ------------------------------------ ---
first knot paste it, basically solved the problem, along with in-depth study should be able to fully understand, thank you.
------ reference --------------------------------------- < br> learn
------ reference ------------------------------------ ---
------ reference ------------------------------------ ---
learning, very good, has been quite understand this
没有评论:
发表评论