tag:blogger.com,1999:blog-32794531160182444702023-06-20T19:08:11.557+05:30Android ramblingsThis is underlined by the recurring evidence of man's aimlessness, his constant running hither and yon, his grasping at a way of life which constantly eludes him, possibly because he never knows exactly what he wants.
- Clifford D. Simak.Anonymoushttp://www.blogger.com/profile/04700670850345654296noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-3279453116018244470.post-3807496336795739132012-08-23T16:07:00.001+05:302012-08-23T16:07:20.054+05:30IceCream Contact Provider with SNS SupportAnonymoushttp://www.blogger.com/profile/04700670850345654296noreply@blogger.com0tag:blogger.com,1999:blog-3279453116018244470.post-3341799580296396192012-05-10T12:16:00.000+05:302012-05-11T15:46:03.137+05:30Binary Shift operation<div dir="ltr" style="text-align: left;" trbidi="on">
<b style="mso-bidi-font-weight: normal;"><span style="font-size: 15pt; mso-bidi-font-size: 12.0pt;"></span></b>Binary manipulation is one of the vital part in programming. We can do some manipulation through this in very efficient way. Bitwise operations are necessary for much low-level programming.<br />
It will be frequently used in device driver development ,protocol developemnt and low-level graphics.<br />
<br />
There are three types of shift operations present in the binary world <br />
1)<span style="mso-spacerun: yes;"> </span>Arithmetic (Signed Shift) or >> / <<<br />
2)<span style="mso-spacerun: yes;"> </span>Logical<span style="mso-spacerun: yes;"> </span>( Unsigned Shift) or >>> / <<<<br />
3)<span style="mso-spacerun: yes;"> </span>Rotational<br />
<br />
The general logic behind shifting is <br />
<br />
<div style="margin-left: 0.5in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -0.25in;">
<span lang="EN" style="font-family: Symbol; mso-ansi-language: EN; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;">·<span style="font-family: "Times New Roman";"> </span></span></span><span lang="EN" style="mso-ansi-language: EN;">Shifting left by <i>n</i> bits on a signed or unsigned binary number "Y" has the effect of multiplying it by 2<i><sup>n</sup></i>.</span></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;"><span style="mso-tab-count: 2;"> </span><b style="mso-bidi-font-weight: normal;">result = (Y x 2<i><sup>n</sup></i> )</b></span></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">For example </span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">Consider a binary number 139<span style="mso-spacerun: yes;"> </span>( 1000 1011)</span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">139 << 1 =<span style="mso-spacerun: yes;"> </span>278 (1 0001 0110) : Here 1 is overflow.</span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">If you get any confusion here check the same with the value 8( 0000 1000)</span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">8 << 1 =<span style="mso-spacerun: yes;"> </span>8 x 2<i><sup>1 </sup></i>=<span style="mso-spacerun: yes;"> </span>16 ( 0001 0000)</span><br />
</div>
<div style="margin-left: 0.25in;">
<a href="http://1.bp.blogspot.com/-M7kYjO96PSI/T6zknz8gPsI/AAAAAAAAAIA/VRw0VPDmsFo/s1600/img1.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" dba="true" src="http://1.bp.blogspot.com/-M7kYjO96PSI/T6zknz8gPsI/AAAAAAAAAIA/VRw0VPDmsFo/s1600/img1.bmp" /></a></div>
<div style="margin-left: 0.5in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -0.25in;">
<span lang="EN" style="font-family: Symbol; mso-ansi-language: EN; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;">·<span style="font-family: "Times New Roman";"> </span></span></span><span lang="EN" style="mso-ansi-language: EN;">Shifting right by <i>n</i> bits on a signed or unsigned binary number has the effect of dividing it by<span style="mso-spacerun: yes;"> </span>2<i><sup>n</sup></i>.</span></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<b style="mso-bidi-font-weight: normal;"><span lang="EN" style="mso-ansi-language: EN;"><span style="mso-tab-count: 2;"> </span>result = (Y / 2<i><sup>n</sup></i> )</span></b></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">For example </span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">Consider a binary number 139<span style="mso-spacerun: yes;"> </span>( 1000 1011)</span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">139 >> 1 =<span style="mso-spacerun: yes;"> </span>69</span></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">Apply the formula </span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;">result<span style="mso-spacerun: yes;"> </span>= ( 139 / 2<sup>1</sup>) =<span style="mso-spacerun: yes;"> </span>69.5 </span></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<b style="mso-bidi-font-weight: normal;"><span lang="EN" style="mso-ansi-language: EN;">(which rounds towards 0 for arithmetic shift and towards negative infinity for Logical shift This is one of the main different between<span style="mso-spacerun: yes;"> </span>arithmetic and logical shift)</span></b></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;"><span style="mso-spacerun: yes;"> </span></span><a href="http://4.bp.blogspot.com/-Sh8h8eFhxGc/T6zkyGE2F3I/AAAAAAAAAIM/BTxH0a3ahL0/s1600/img2.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" dba="true" src="http://4.bp.blogspot.com/-Sh8h8eFhxGc/T6zkyGE2F3I/AAAAAAAAAIM/BTxH0a3ahL0/s1600/img2.bmp" /></a></div>
<b style="mso-bidi-font-weight: normal;"><u><span style="font-size: 13pt; mso-bidi-font-size: 12.0pt;">Logical shift (Un signed):</span></u></b><br />
<div style="margin-left: 0.5in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -0.25in;">
<span lang="EN" style="font-family: Symbol; mso-ansi-language: EN; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;">·<span style="font-family: "Times New Roman";"> </span></span></span><span lang="EN" style="mso-ansi-language: EN;">It always rounds down (<b style="mso-bidi-font-weight: normal;">towards negative infinity</b>). Refer the above written example for the sample Logical shift.</span></div>
<br />
<b style="mso-bidi-font-weight: normal;"><u><span style="font-size: 13pt; mso-bidi-font-size: 12.0pt;">Arithmetic shift (Signed):</span></u></b><br />
<div style="margin-left: 0.5in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -0.25in;">
<span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;">·<span style="font-family: "Times New Roman";"> </span></span></span>It is similar to Logical shift except that left most bits will be filled with the sign bit of the original number instead of<span style="mso-spacerun: yes;"> </span>0's</div>
<div style="margin-left: 0.5in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -0.25in;">
<span lang="EN" style="font-family: Symbol; mso-ansi-language: EN; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;">·<span style="font-family: "Times New Roman";"> </span></span></span><span lang="EN" style="mso-ansi-language: EN;">Consider a binary number 139<span style="mso-spacerun: yes;"> </span>( 1000 1011). In arithmetic shift this 1st bit represent the sign of the number.<span style="mso-spacerun: yes;"> </span></span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;"><span style="mso-tab-count: 1;"> </span>1 -- represent negative </span></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;"><span style="mso-tab-count: 1;"> </span>0 -- represent positive .</span></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<span lang="EN" style="mso-ansi-language: EN;"><span style="mso-tab-count: 1;"> </span>139 >> 1 =<span style="mso-spacerun: yes;"> </span>69</span></div>
<a href="http://2.bp.blogspot.com/-9Ol7oP5dgEY/T6zlVDpjP-I/AAAAAAAAAIY/bTYignW7GRc/s1600/img3.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" dba="true" src="http://2.bp.blogspot.com/-9Ol7oP5dgEY/T6zlVDpjP-I/AAAAAAAAAIY/bTYignW7GRc/s1600/img3.bmp" /></a><br />
<br />
<span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;"> ·<span style="font-family: "Times New Roman";"> </span></span></span><b style="mso-bidi-font-weight: normal;"><span lang="EN" style="mso-ansi-language: EN;">It always rounds towards 0</span></b><br />
<div style="margin-left: 0.5in; mso-list: l1 level1 lfo1; tab-stops: list .5in; text-indent: -0.25in;">
<span lang="EN" style="font-family: Symbol; mso-ansi-language: EN; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;"><span style="font-family: "Times New Roman";"> </span></span></span><span lang="EN" style="mso-ansi-language: EN;">Arithmetic shifts can be useful as efficient ways of performing multiplication or division of signed integers</span></div>
<br />
<b style="mso-bidi-font-weight: normal;"><span lang="EN" style="mso-ansi-language: EN;">Example 1: </span></b><br />
<div style="margin-left: 0.25in;">
Calculate the result of<span style="mso-spacerun: yes;"> </span>-16 divide by 4 </div>
<div style="margin-left: 0.25in;">
<span style="mso-tab-count: 1;"> </span>-16 / 2<span style="mso-spacerun: yes;"> </span>= - 8 </div>
<div style="margin-left: 0.25in;">
<b style="mso-bidi-font-weight: normal;">(Divide a number by 2 is equivalent to right shift a number by 1)</b></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
Binary equivalent of<span style="mso-spacerun: yes;"> </span>16 = ( 0001 0000). </div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
To represent the negative number we need to calculate the 2's complement of the original number</div>
<div style="margin-left: 0.25in;">
<span style="mso-spacerun: yes;"> </span>16<span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 2;"> </span>-----><span style="mso-spacerun: yes;"> </span>0001 0000</div>
<div style="margin-left: 0.25in;">
1's Complement<span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>-----><span style="mso-spacerun: yes;"> </span>1110 1111</div>
<div style="margin-left: 0.25in;">
2's Complement<span style="mso-spacerun: yes;"> </span><span style="mso-tab-count: 1;"> </span>-----><span style="mso-spacerun: yes;"> </span>1111 0000</div>
<div style="margin-left: 0.25in;">
-16 <span style="mso-tab-count: 2;"> </span>-----><span style="mso-spacerun: yes;"> </span>1111 0000</div>
<div style="margin-left: 0.25in;">
<br /></div>
<div style="margin-left: 0.25in;">
now -16 >> 1 =<span style="mso-spacerun: yes;"> </span>1111 1000 ( This is equivalent to -8)</div>
<div style="margin-left: 0.25in;">
<br /></div>
<br />
<b style="mso-bidi-font-weight: normal;"><u><span style="font-size: 13pt; mso-bidi-font-size: 12.0pt;">Rotational/ Circular <span style="mso-spacerun: yes;"> </span>Shift :</span></u></b><br />
<div style="margin-left: 0.5in; mso-list: l0 level1 lfo2; tab-stops: list .5in; text-indent: -0.25in;">
<span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"><span style="mso-list: Ignore;">·<span style="font-family: "Times New Roman";"> </span></span></span>Rotation shift is a circular shift. Instead of pushing<span style="mso-spacerun: yes;"> </span>"0" it will push discarded bit.</div>
<br />
For example if we do normal right shift operation , LSB at the right end will be discarded and a '0' will be pushed into the MSB from the left end.<br />
<br />
But in rotational shift LSB at the right end will be removed and it will be pushed as the<span style="mso-spacerun: yes;"> </span>MSB from the left end.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-0FoLJH97BOE/T6zlXTPdfyI/AAAAAAAAAIk/vdev-wGXMt8/s1600/img4.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" dba="true" src="http://3.bp.blogspot.com/-0FoLJH97BOE/T6zlXTPdfyI/AAAAAAAAAIk/vdev-wGXMt8/s1600/img4.bmp" /></a></div>
Here we need do convert a integer number into binary. Then we are going to break the binary number into smaller pieces to get the certain range of values<br />
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<strong>Sample program in C : </strong></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
Consider a<span style="mso-spacerun: yes;"> </span>integer 92 .Binary equivalent of 91 is "0101 1100"</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
In this binary number bits from 0 to 2 representing a particular value. And 4 to 6 representing some other value. </div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
Bits from 0 to 2 is - "100"<span style="mso-spacerun: yes;"> </span>Bits from 4 to 6 is - "101" .So here the requirement is break the binary number into above mentioned<span style="mso-spacerun: yes;"> </span>range. And return the result. </div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
#include<Stdio.h></div>
#include<math.h><br />
<br />
const int BIT_LENGTH = 31; //(0 to 31)bits<br />
int aBArray[32];<br />
void toBinary(long,int);<br />
void print();<br />
<br />
long getSensor() {<br />
return 2543133;<br />
}<br />
<br />
int getRainfall(long sensorReading) {<br />
int RAINFALL_START = 7;<br />
int RAINFALL_END = 0;<br />
toBinary(sensorReading, BIT_LENGTH);<br />
print();<br />
return getResult(RAINFALL_START, RAINFALL_END);<br />
}<br />
<br />
int getHumidity(long sensorReading) {<br />
int HUMIDITY_START = 15;<br />
int HUMIDITY_END = 11;<br />
return getResult(HUMIDITY_START, HUMIDITY_END);<br />
}<br />
<br />
int getTemperature(long sensorReading) {<br />
int TEMP_START = 23;<br />
int TEMP_END = 18;<br />
return getResult(RAINFALL_START, RAINFALL_END);<br />
}<br />
void print( ){<br />
int i;<br />
printf("\n******************RESULT: BINARY************************\n");<br />
<br />
for(i = 0; i <= BIT_LENGTH; i++) {<br />
printf( " %d",aBArray[i]);<br />
if((i + 1) % 4 ==0){<br />
printf(" ")<br />
}<br />
}<br />
printf("\n**************************************************************\n");<br />
}<br />
<br />
int getResult(long aStart, int aEnd) {<br />
<br />
int initPower = (aStart - aEnd), bit, result = 0;<br />
printf("\n");<br />
for(bit = (BIT_LENGTH - aStart);bit <= (BIT_LENGTH - aEnd); bit++) {<br />
result = result + (aBArray[bit] * pow(2,initPower--) );<br />
}<br />
return result;<br />
}<br />
void toBinary(long number, int index) {<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"></span> int remainder;<br />
<span class="Apple-tab-span" style="white-space: pre;"></span>if(number <= 1) {<br />
<span class="Apple-tab-span" style="white-space: pre;"></span><br />
<span class="Apple-tab-span" style="white-space: pre;"></span> aBArray[index] = number;<br />
<span class="Apple-tab-span" style="white-space: pre;"></span> return;<br />
<span class="Apple-tab-span" style="white-space: pre;"></span> }<br />
<span class="Apple-tab-span" style="white-space: pre;"></span> remainder = number % 2;<br />
<span class="Apple-tab-span" style="white-space: pre;"></span> aBArray[index] = remainder;<br />
<span class="Apple-tab-span" style="white-space: pre;"></span> toBinary(number >> 1, --index); <br />
}<br />
<br />
int main() {<br />
long sensorReading = getSensor();<br />
clrscr();<br />
printf("\nRainfall : %d\n", getRainfall(sensorReading));<br />
printf("\nHumidity : %d\n", getHumidity(sensorReading));<br />
printf("\nTemperature : %d\n", getTemperature(sensorReading));<br />
return 0;<br />
}<br />
<br />
<br />
<br />
<br />
<br />
<br /></div>Anonymoushttp://www.blogger.com/profile/04700670850345654296noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.594562712.724026199999999 77.2787057 13.2191712 77.910419699999991tag:blogger.com,1999:blog-3279453116018244470.post-75923419634686361512012-05-07T23:37:00.000+05:302012-08-14T17:07:15.116+05:30Android Handler vs Asynctask<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<b><u><span style="color: blue;">Android UI thread</span> </u></b></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
If you freshly launch your application android will create a thread called "main".(Also called UI Thread).</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
UI thread will take care of following tasks</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
1) Dispatching events (Like touch, click, fling...etc) to the appropriate widgets </div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
2) Interacts with running components of the Android UI toolkit.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
For example , if you touch the a button on screen, the UI thread dispatches the touch event to the widget, which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread de-queues the request and notifies the widget to redraw itself.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
If android follows single threaded model it will do all the operation in the UI thread will lead to poor performance.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<b><i><span style="color: blue;">For example</span></i></b></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
Let say the user is trying to do two operations like accessing the network and pressing a button both at same time.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
First it will try to finish the network operation. After finishing the network operation it will respond to the button click. Because of this applications will be appeared like hanged.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
If the UI thread is blocked for more than a few seconds (Currently About 5 sec) android will show a dialog called "<b><a href="http://developer.android.com/guide/practices/design/responsiveness.html"><span style="color: windowtext; text-decoration: none; text-underline: none;">application not responding</span></a></b>" (<b>ANR</b>) dialog.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
General solution to avoid such a problem is create separate thread to do the network operation</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<b><span style="color: blue;">Solution 1:</span> </b></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<span style="color: #666666;">public void onClick(View aView) { </span><br />
<div>
new Thread(new Runnable() {</div>
<div>
public void run() {</div>
<div>
Bitmap b = loadImageFromNetwork(); //Network operation</div>
<div>
mImageView.setImageBitmap(b);</div>
<div>
}</div>
<div>
}).start();</div>
<div>
}</div>
</div>
<div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
It will be looking like a solution to our problem.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
But it violates the single-threaded model for the UI: The Android UI toolkit is <i>not thread-safe</i> and must always be manipulated on the UI thread.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
Android OS will not allow threads to touch the UI element other than UI thread or main thread.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
So you will get the famous exception "<b>Called from Wrong Thread Exception</b>"</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<b><u><span style="color: blue;">Sample error message:</span> </u></b></div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
ERROR/AndroidRuntime(315): FATAL EXCEPTION: Thread-8<br />
12-07 16:24:29.089: ERROR/AndroidRuntime(315): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
<b><u><span style="color: blue;">Ways to interact with the UI thread :</span></u></b></div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
Following mechanisms are provided by the android through which we can interact with UI thread from any other thread.</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br />
<div>
</div>
</div>
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<ul style="margin-top: 0in;" type="disc">
<li class="MsoNormal"><a href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)"><span style="color: #444444;">Activity.runOnUiThread(Runnable)</span></a></li>
<li class="MsoNormal"><a href="http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)"><span style="color: #444444;">View.post(Runnable)</span></a></li>
<li class="MsoNormal"><a href="http://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable, long)"><span style="color: #444444;">View.postDelayed(Runnable, long)</span></a></li>
<li class="MsoNormal"><span style="color: #444444;"><span style="color: #444444;"><a href="http://developer.android.com/reference/android/os/Handler.html">Handler</a> </span>(If the Handler is created from the UI thread)</span></li>
</ul>
</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<b><u>Solution 2:</u></b></div>
<div class="MsoNormal">
<span style="color: #666666;">public void onClick(View v) { </span><br />
<div>
new Thread(new Runnable() {</div>
<div>
public void run() {</div>
<div>
final Bitmap b = loadImageFromNetwork();</div>
<div>
<b> mImageView.post(new Runnable() {</b></div>
<b><div>
public void run() {</div>
<div>
mImageView.setImageBitmap(b);</div>
<div>
}</div>
<div>
});</div>
<div>
</div>
</b><br />
<div>
}).start();</div>
<div>
}</div>
</div>
} <br />
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
While implementing complex operation the code will be more complicated and difficult to read. To solve this problem android provides a new technique called Asynctask & Handler.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<b><u><span style="color: blue;">Threading through Handler :</span></u></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Generally Handler will be associated with the application's <b>main thread/ UI thread</b>.</div>
<div class="MsoNormal">
By default UI thread will have a Handler called <b>UI handler</b> associated with it.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
It will handle and schedule messages and runnable send from the background threads to the app UIthead /main thread.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<b><u><span style="color: blue;">Looper:</span></u></b></div>
<div class="MsoNormal" style="margin-left: 30pt;">
Looper is nothing but a <b>MessageQueue</b>. It will have the list of messages posted by the handler</div>
<div class="MsoNormal">
<b><u><span style="background-color: #f3f3f3; color: blue;">Handler :</span></u></b></div>
<div class="MsoNormal" style="margin-left: 30pt;">
Assume handler as a man who is taking care of sending messages into the Loopers message queue.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<b><u><span style="color: blue;">Pipeline thread :</span></u></b></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
By default thread will not have any message queue associated with it. If we want to change a thread into the pipeline thread we need to attach a Looper with it.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
Every thread in java will be in one of the following states in its life cycle.</div>
<div class="MsoNormal">
<br />
<span style="text-indent: 0.5in;"> </span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-SZJ28xFX6wg/T6gNiOCmNFI/AAAAAAAAAHo/RMNWj36pxhU/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="155" src="http://1.bp.blogspot.com/-SZJ28xFX6wg/T6gNiOCmNFI/AAAAAAAAAHo/RMNWj36pxhU/s400/Untitled.png" width="400" /></a></div>
<div class="MsoNormal" style="margin-left: 1in; text-indent: 0.5in;">
<b> </b></div>
<div class="MsoNormal" style="margin-left: 1in; text-indent: 0.5in;">
<b style="text-indent: 0.5in;"> <span style="color: blue;"><u>Normal Thread Life Cycle</u></span></b></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
Once it finished its run() method it will go to the dead state. We can't start a dead thread once again. If we try to do it will give the <b>IllegalThreadState</b> exception.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
But through Looper we are trying to reuse the same thread again and again until finish all the messages or jobs posted in the message queue or until call the <b>looper.quit()</b>.</div>
<div class="MsoNormal">
<strong>So if we want to change a Thread into a pipeline thread we need to attach a Looper with it. </strong></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<strong><u><span style="color: blue;">Relation between Looper, Handler and Thread :</span></u></strong></div>
<ul>
<li><div class="MsoNormal">
<b>One important character of the Looper is that it will be associated with only one thread in which the Looper is created.And it will be associated with a single thread through out the life time of a thread.</b>(This association is kept for ever and can't be broken nor changed)</div>
</li>
<li><div class="MsoNormal">
<b>One thread can't be associated with more than one Looper.</b></div>
</li>
<li><div class="MsoNormal">
<b>But one looper can be associated with more than one Handler.</b></div>
</li>
</ul>
<div class="MsoNormal" style="text-align: left;">
</div>
<div class="MsoNormal" style="text-align: left;">
If you see the Looper source code we can come to know that this Looper object is stored in a thread-local storage.<br />
</div>
<div class="MsoNormal">
<b><span style="color: blue;"><u>Sample code snippet from Looper class:</u></span></b></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<span style="color: #666666;">public class Looper {</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> ....</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> private static final ThreadLocal sThreadLocal = new ThreadLocal();</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> ....</span></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<span style="color: #666666;"> public static final void prepare() {</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> if(sThreadLocal.get() != null) {</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> throw new RunTimeException("....");</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> }</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> <b>sThreadLocal.set(new Looper());</b></span></div>
<div class="MsoNormal">
<span style="color: #666666;"> }</span></div>
<div class="MsoNormal">
<span style="color: #666666;">}</span></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<strong><span style="color: blue;"><u>This code will ensure following things.</u></span></strong></div>
<div class="MsoNormal">
1) Looper can't be created via its constructor directly</div>
<div class="MsoNormal">
2) The one and only way to create a Looper object for a thread is to call the static method</div>
<div class="MsoNormal">
<span class="Apple-tab-span" style="white-space: pre;"></span> prepare() present in the Looper class.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<br />
<div>
</div>
<div class="MsoNormal">
- prepare method first examines <a href="http://developer.android.com/reference/java/lang/ThreadLocal.html" target="_blank">ThreadLocal</a> of current thread to make sure that there isn't already a Looper associated with the thread. After the examination, a new Looper is created and saved in ThreadLocal variable "sThreadLocal".</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
After creating the Looper for a thread we can call the static method loop() to check for new messages. If any message is present in the queue it will be dispatched to the appropriate Handlers .</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<b><span style="color: blue;"><u>Sample code snippet from Looper class:</u></span></b></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<span style="color: #666666;">public static final void loop() {</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> </span></div>
<div class="MsoNormal">
<span style="color: #666666;"> .....</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> Message msg = queue.next()</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> if(null != msg) {</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> msg.target.dispatchMessage(msg);</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> }</span></div>
<div class="MsoNormal">
<span style="color: #666666;"> ........</span></div>
<div class="MsoNormal">
<span style="color: #666666;">}</span></div>
<div class="MsoNormal">
msg.target - is the Handler from which the message is created.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
So this message is dispatched to the Handlers in which the message is created and handleMessage() method will be called on this Handler and the tasks given in the handleMessage() will be executed in the Thread.</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<b><span style="color: blue;"><u>Sample code snippet from Handler:</u></span></b></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
public void handleMessage(Message aMsg) {</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
switch (msg.what) {</div>
<div class="MsoNormal">
cause DO_MOVIE_DOWNLOAD:</div>
<div class="MsoNormal">
// Our task</div>
<div class="MsoNormal">
break;</div>
<div class="MsoNormal">
cause SING_THE_SONG:</div>
<div class="MsoNormal">
// Our task</div>
<div class="MsoNormal">
break;</div>
<div class="MsoNormal">
cause DO_BREAK_DANCE:</div>
<div class="MsoNormal">
// Our task</div>
<div class="MsoNormal">
break;</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
}</div>
<div class="MsoNormal">
<br />
<div>
</div>
<b>Handler class is mainly responsible for handling (adding, removing, dispatching) messages of current thread's MessageQueue.</b></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
A Handler instance is also bound to a thread. Looper act as a intermediate between Handler and Thread. <br />
<b>The binding between Handler and Thread is achieved via Looper and MessageQueue</b>.</div>
<div class="MsoNormal">
<br />
<div>
<strong><span style="color: blue;"><u>The relationships between Looper, Handler and MessageQueue is shown below:</u></span></strong><br />
</div>
</div>
<div class="MsoNormal">
<span style="color: #cc0000;"><strong> Handler (*) ---------------> (1) Looper (MessageQueue) (1) <--------------- (1) Thread</strong></span></div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
Unlike Looper, multiple Handler instances can be bound to the same thread. Whenever we call <a href="http://developer.android.com/reference/android/os/Handler.html#post%28java.lang.Runnable%29" target="_blank">post</a> or any methods alike on the Handler, a new message is added to the associated MessageQueue.</div>
<div class="MsoNormal">
The target field of the message is set to current Handler instance. When the Looper received this message, it invokes <a href="http://developer.android.com/reference/android/os/Handler.html#dispatchMessage%28android.os.Message%29" target="_blank">dispatchMessage</a> on message's target field, so that the message routes back to the Handler instance to be handled, but on the correct thread. </div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
<div class="MsoNormal">
<br />
<div>
</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/04700670850345654296noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.594562712.724026199999999 77.2787057 13.2191712 77.910419699999991tag:blogger.com,1999:blog-3279453116018244470.post-49324079022653247402012-03-24T14:15:00.003+05:302012-05-09T11:40:24.151+05:30Marker Interface<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
About Marker interface in Java :<br />
<br />
Marker interface is an interface with no method declaration.(Interface with no method definition)<br />
<strong>Serializable</strong> is a one of the commonly used marker interface in java.<br />
<br />
Some of the commonly used Marker interfaces <br />
- java.lang.Cloneable<br />
- java.util.EventListener<br />
<br />
According to Object Oriented point of view Interface means Some behavior. So ,If a class implement one interface, then that class becomes the instance of that interface. That means it implements that behavior.(This implementation generally through give codes to the method declared in the interface).<br />
<br />
"But Marker interfaces will not have any method declaration. (That means it will not have any behavior to Implement)"<br />
<strong>Then what is the purpose of implementing this useless interface?</strong><br />
A class implements a marker interface JVM will automatically understand that some special behavior is going to be implemented by the object of that class. So JVM create one mark on that object. So internally we force that object to execute some behavior.<br />
Example :<br />
<strong><u>clone()</u></strong> : This method is in class <strong>Object</strong>. If a class implement the " <strong>Cloneable</strong> " interface then JVM internally understand that some job has to be done by the object of that class . So JVM expect clone() method to execute.<br />
So internally we force the clone() method to execute.<br />
<br />
<strong>So ,<br />1) If a class implement Cloneable interface it will tell the JVM that object of this class can be cloned (by implementing the method clone)<br />2) If a class implement Serializable interface it will tell the JVM that non-transient variables of this object can be stored</strong><br />
<strong></strong><br />
<strong><br />Exception : <strong>Marker interface can contain method declaration</strong>. </strong>Marker interface may or may not have method declaration....<br />
Runnable is a marker interface. But it is having a mthod run().<br />
<br />
<div class="MsoNormal" style="margin: 0in 0in 0pt;">
<br /></div>
<br /></div>Anonymoushttp://www.blogger.com/profile/04700670850345654296noreply@blogger.com0