See android2.2
When using the SDK source code, you can see this code in memoryfile. Java: Private Final Boolean mownsregion;
// False if this is a ref to an existing ashmem region its constructor is: Public memoryfile (string name, int length) throws ioexception
{
Mlength = length;
MFD = native_open (name,
Length );
Maddress = native_mmap (MFD, length, prot_read |
Prot_write );
Mownsregion = true;
} I think this is not final. Why is it not initialized during the declaration? You may laugh at it at this time. It is natural that you write it like this. Then I declare a final variable in my own activity and initialize it in the oncreate method. In the result, eclipse immediately reports an error and asks me to remove the final keyword. I feel a little strange. Why can it be defined like this, and I am wrong? After research, we found that we do not know the loading process of the activity. We are ready to write the code to test and directly use the source code package.
Com. husttic. Phone. test;
Import Android. App. activity;
Public class
Myactivity extends activity {
Class mythread extends thread
{
Private int count3;
Public void run ()
{
While (true)
{
H. sendemptymessage (handler_test );
Log. D (tag,
"The worker thread id ="
+
Thread. currentthread (). GETID () +
"/Tcount3 ="
+
Count3 );
Count3 ++;
//
You cannot refresh the view in the UI thread here.
//
Mbutton. settext ("ID =" + thread. currentthread (). GETID () +"
Count = "+ count );
Try
{
Thread. Sleep (3000 );
} Catch
(Interruptedexception e ){
// Todo auto-generated catch
Block
E. printstacktrace ();
}
}
}
}
Class
Mythread2 extends thread {
Private int count4;
Private
Boolean flag = true;
/**
* @ Return
Flag
*/
Public synchronized final Boolean isflag ()
{
Return flag;
}
Public void run ()
{
While (this. Flag)
{
H. sendemptymessage (handler_test );
Log. D (tag,
"The worker thread2 id ="
+
Thread. currentthread (). GETID () +
"/Tcount4 ="
+
Count4 );
Count4 ++;
Try
{
Thread. Sleep (100 );
} Catch
(Interruptedexception e ){
// Todo auto-generated catch
Block
E. printstacktrace ();
}
}
Log. D (tag,
"The worker thread2 id ="
+
Thread. currentthread (). GETID () +
"/Tending ");
}
/**
* @ Param
Flag
* The flag to set
*/
Public synchronized
Final void setflag (Boolean flag ){
This. Flag =
Flag;
}
}
Static final int handler_test = 1;
//
Test static variables associated with classes
Private Static int scount = 2;
Private Static
Final string tag = "myactivity ";
Private Static final string
Trave_view = "trave_view ";
Handler H = new handler ()
{
Public void handlemessage (Message MSG ){
Switch
(Msg. What ){
Case handler_test:
Log. D (tag,
"The Handler thread id ="
+
Thread. currentthread (). GETID () + "/N ");
Mbutton. settext ("ID ="
+ Thread. currentthread (). GETID ()
+ "Count =" +
Count );
Break;
}
}
};
Private
Int count;
Private button mbutton;
//
Test the final variable associated with the object
Private Final Boolean mfinaltest;
Private
Runnable mrunnable = new runnable (){
Public void run ()
{
// Log is printed for ease of viewing.
Log. E (tag,
Thread. currentthread (). getname () + "" +
Count );
Count ++;
Settitle ("current thread id =" +
Thread. currentthread (). GETID () + "" + count );
//
Run the run method every 2 seconds.
H. postdelayed (mrunnable,
2000 );
}
};
Private runnable mrunnable2 = new
Runnable (){
Private int count2 = 0;
Public void
Run (){
Mtextview. settext ("current thread id =" +
Thread. currentthread (). GETID () + ""
+
Count2 );
Count2 ++;
H. postdelayed (mrunnable2,
300 );
}
};
Private textview
Mtextview;
Private mythread2 th;
//
Static code block test
Static {
System. Out. println ("in static
Block ");
Myactivity. scount = 3;
}
//
Constructor Test
Public myactivity ()
{
Super ();
System. Out. println ("in myactivity
Method ");
This. mfinaltest =
True;
System. Out. println (this. mfinaltest );
System. Out. println (myactivity. scount );
}
@ Override
Public
Void finish (){
Log. D (TAG, "In finish
Method ");
Super. Finish ();
}
@ Override
Public
Boolean isfinishing (){
Log. D (TAG, "In isfinishing
Method ");
Return
Super. isfinishing ();
}
@ Override
Public void
Onconfigurationchanged (configuration newconfig ){
Log. D (TAG, "in
Onconfigurationchanged method ");
//
Change the screen direction and then obtain the screen size again.
Displaymetrics dm = new
Displaymetrics ();
This. getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
//
Actively release Object Reference
Dm =
NULL;
Super. onconfigurationchanged (newconfig );
}
/**
Called when the activity is first created .*/
@ Override
Public
Void oncreate (bundle savedinstancestate ){
Log. D (TAG, "In oncreate
Method ");
// The following line of code has an error
//
This. mfinaltest = true;
Super. oncreate (savedinstancestate );
// Used for method tracking and analysis
Debug. startmethodtracing (trave_view );
Log. D (tag,
"The main thread id =" + thread. currentthread (). GETID ()
+
"/N ");
Setcontentview (R. layout. Main );
Mtextview =
(Textview) This. findviewbyid (R. Id. textview1 );
This. mbutton = (button)
This. findviewbyid (R. Id. button1 );
This. mbutton. setonclicklistener (New
Onclicklistener (){
@ Override
Public void
Onclick (view v ){
// Todo auto-generated Method
Stub
Th. setflag (false );
// Test the constructor
New
Myactivity ();
}
});
//
Start a thread through Handler
H. Post (mrunnable );
H. Post (mrunnable2 );
//
This thread can no longer be referenced (cannot be recycled)
New
Mythread (). Start ();
// Rereference
Th = new
Mythread2 ();
Th. Start ();
}
@ Override
Public
Void ondestroy (){
Log. D (TAG, "In ondestroy
Method ");
H. removecallbacks (this. mrunnable );
H. removecallbacks (this. mrunnable2 );
Super. ondestroy ();
}
@ Override
Public
Void onpause (){
Log. D (TAG, "In onpause
Method ");
Super. onpause ();
}
@ Override
Public
Void onpostcreate (bundle savedinstancestate ){
Log. D (TAG, "in
Onpostcreate
Method ");
Super. onpostcreate (savedinstancestate );
}
@ Override
Public
Void onpostresume (){
Log. D (TAG, "in
Onpostresume ");
Super. onpostresume ();
}
@ Override
Public
Void onrestart (){
Log. D (TAG, "In onrestart
Method ");
H. Post (mrunnable );
H. Post (mrunnable2 );
Super. onrestart ();
}
@ Override
Public
Void onrestoreinstancestate (bundle savedinstancestate ){
Log. D (tag,
"In onrestoreinstancestate
Method ");
Super. onrestoreinstancestate (savedinstancestate );
}
@ Override
Public
Void onresume (){
Log. D (TAG, "In onresume
Method ");
Super. onresume ();
}
@ Override
Public
Void onsaveinstancestate (bundle outstate ){
Log. D (TAG, "in
Onsaveinstancestate
Method ");
Super. onsaveinstancestate (outstate );
}
@ Override
Public
Void onstart (){
Log. D (TAG, "In onstart
Method ");
Super. onstart ();
}
@ Override
Public
Void onstop (){
Log. D (TAG, "In onstop
Method ");
H. removecallbacks (this. mrunnable );
H. removecallbacks (this. mrunnable2 );
Debug. stopmethodtracing ();
Super. onstop ();
}
} This tests the status of the activity and the handler class, a bridge between the UI thread and other threads for information exchange. After exiting the activity, other threads are still running ~ It's strange. You can open logcat and check it. It keeps printing information. Therefore, it is better to pay attention to the usage of threads in Android ~