tag:blogger.com,1999:blog-29715587316724047372024-03-12T19:04:49.507-07:00GreenManAndroid app Development Tutorials . .RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.comBlogger48125tag:blogger.com,1999:blog-2971558731672404737.post-60808579955462895882013-11-28T06:07:00.000-08:002013-11-28T22:29:09.813-08:00Getting Started with Android Push Notification : Latest Google Cloud Messaging (GCM) - step by step complete tutorial<div dir="ltr" style="text-align: left;" trbidi="on">
<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->
<b>GCM </b>is a free service that helps developers send data from servers to their Android applications on Android devices, and upstream messages from the user's device back to the cloud.<br />
<div>
<br /></div>
<div>
1. Create/Register App on Google Cloud</div>
<div>
2. Setup Cloud SDK with Development</div>
<div>
3. Configure project for GCM</div>
<div>
4. Get Device Registration ID</div>
<div>
5. Send Push Notifications</div>
<div>
6. Receive Push Notifications</div>
<div>
7. Handle Further step as per Notification type. </div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-IT4tknAuM50/UpdN3-53EZI/AAAAAAAABRs/X9Q2mR2WPK8/s1600/device-2013-11-28-193525.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://2.bp.blogspot.com/-IT4tknAuM50/UpdN3-53EZI/AAAAAAAABRs/X9Q2mR2WPK8/s320/device-2013-11-28-193525.png" width="200" /></a></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<b>1. Create and Register Application on Google Cloud</b></div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">Go to the Google Cloud Console - </span><a href="https://cloud.google.com/console" style="font-family: Courier;">https://cloud.google.com/console</a></div>
<div>
Login with your gmail account</div>
<div>
<br /></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-G91g1QmUZ5M/UpdJBL8Yx0I/AAAAAAAABQM/XKOgzr9BcRc/s1600/Screen+Shot+2013-11-28+at+3.21.21+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="208" src="http://1.bp.blogspot.com/-G91g1QmUZ5M/UpdJBL8Yx0I/AAAAAAAABQM/XKOgzr9BcRc/s320/Screen+Shot+2013-11-28+at+3.21.21+pm.png" width="320" /></a></div>
<div>
<div>
# Create New Project</div>
<div>
Create New Project</div>
</div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-yLlvyJ5RV28/UpdJO_It1_I/AAAAAAAABQU/5FL15hCLdfo/s1600/Screen+Shot+2013-11-28+at+3.22.11+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="178" src="http://1.bp.blogspot.com/-yLlvyJ5RV28/UpdJO_It1_I/AAAAAAAABQU/5FL15hCLdfo/s320/Screen+Shot+2013-11-28+at+3.22.11+pm.png" width="320" /></a></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<span style="font-family: Times;">Complet the Mobile number verification for security</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-7vKFq70Y554/UpdJUAqdy-I/AAAAAAAABQc/wjvgW5phNo8/s1600/Screen+Shot+2013-11-28+at+3.23.26+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="235" src="http://3.bp.blogspot.com/-7vKFq70Y554/UpdJUAqdy-I/AAAAAAAABQc/wjvgW5phNo8/s320/Screen+Shot+2013-11-28+at+3.23.26+pm.png" width="320" /></a></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
# Get SENDER_ID<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">Once you create Project, you will get landed on google cloud’s dash board</span></div>
<div>
<span style="font-family: Times;">Here you can see SENDER_ID</span></div>
<div>
<span style="color: #ff2600; font-family: Times;">Note : please note down <b>SENDER_ID</b> it is important to get RegID from GCM</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-1U0Ned7yBgE/UpdJc-7vuYI/AAAAAAAABQk/mAwcZkxfR-g/s1600/Screen+Shot+2013-11-28+at+4.04.23+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="153" src="http://1.bp.blogspot.com/-1U0Ned7yBgE/UpdJc-7vuYI/AAAAAAAABQk/mAwcZkxfR-g/s320/Screen+Shot+2013-11-28+at+4.04.23+pm.png" width="320" /></a></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;"># </span>Enable Google cloud messaging service</div>
<div>
<span style="font-family: Times;">From Dashboard go to —> APIs & auth</span></div>
<div>
<span style="font-family: Times;">Switch ON - <b>Google Cloud Messaging for Android</b> option</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-t48jBql4J9o/UpdJqoIkEYI/AAAAAAAABQs/ta7drhN0s-g/s1600/Screen+Shot+2013-11-28+at+4.26.58+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="268" src="http://2.bp.blogspot.com/-t48jBql4J9o/UpdJqoIkEYI/AAAAAAAABQs/ta7drhN0s-g/s320/Screen+Shot+2013-11-28+at+4.26.58+pm.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-w8Ohu88lGWA/UpdKQR-pTVI/AAAAAAAABRE/qh3krJcDKOc/s1600/Screen+Shot+2013-11-28+at+4.39.27+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="156" src="http://3.bp.blogspot.com/-w8Ohu88lGWA/UpdKQR-pTVI/AAAAAAAABRE/qh3krJcDKOc/s320/Screen+Shot+2013-11-28+at+4.39.27+pm.png" width="320" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;"># Get API key</span></div>
<div>
<span style="font-family: Times;">select API & auth —> Registered apps</span></div>
<div>
<span style="font-family: Times;">click Register App button</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">Give the details this way</span></div>
<div>
<br /></div>
<div>
<span style="color: #6aa84f; font-family: Times;"><b>If you are Sending Push Notification From your Local Web Server</b></span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">Name - MyCloud App</span></div>
<div>
<span style="font-family: Times;">Platform - [ Android : Accessing APIs via a web server ]</span></div>
<div>
<span style="font-family: Times;">Click Register.</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-SOOkLHE9yJk/UpdJ7H1_ExI/AAAAAAAABQ0/0cwXinrXOro/s1600/Screen+Shot+2013-11-28+at+4.33.49+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="http://1.bp.blogspot.com/-SOOkLHE9yJk/UpdJ7H1_ExI/AAAAAAAABQ0/0cwXinrXOro/s320/Screen+Shot+2013-11-28+at+4.33.49+pm.png" width="320" /></a></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="color: #6aa84f;"><b>If you are Sending Push Notification From your Android App itself</b></span><span style="font-family: Times;"><br /></span></div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">Name - MyCloud App</span></div>
<div>
<span style="font-family: Times;">Platform - [ Android : Accessing APIs directly from Android ]</span></div>
<div>
<span style="font-family: Times;">Package Name - <your-application-package-name></your-application-package-name></span></div>
<div>
<span style="font-family: Times;">SHA1 fingerprint - <your-sha-key></your-sha-key></span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">Check here How to Generate SHA key for your app</span></div>
<div>
<a href="https://developers.google.com/console/help/new/">https://developers.google.com/console/help/new/</a><span style="font-family: Times;"><br /></span></div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">Hint : Just search for the string “</span><span style="font-family: Times;"><span style="background-color: white; color: #222222; line-height: 21px;">To find your SHA-1 fingerprint:"</span> and follow the instructions</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-kG-QQNJ9NPU/UpdKqseDHLI/AAAAAAAABRU/uQr8oToD0Ac/s1600/Screen+Shot+2013-11-28+at+6.47.36+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="151" src="http://4.bp.blogspot.com/-kG-QQNJ9NPU/UpdKqseDHLI/AAAAAAAABRU/uQr8oToD0Ac/s320/Screen+Shot+2013-11-28+at+6.47.36+pm.png" width="320" /></a></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
Click Register.</div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">Once it get registered —> click on Server Key —> copy API key</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="color: #ff2600;"><span style="font-family: Times;">Note : </span>please note down<b> API_KEY</b> , it is important for sending PN Message to GCM</span></div>
<div>
<span style="color: #ff2600;"><span style="font-size: x-small;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-814O_TR54UY/UpdKDhNhCJI/AAAAAAAABQ8/CBbyqKkBZxc/s1600/Screen+Shot+2013-11-28+at+4.34.36+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="196" src="http://1.bp.blogspot.com/-814O_TR54UY/UpdKDhNhCJI/AAAAAAAABQ8/CBbyqKkBZxc/s320/Screen+Shot+2013-11-28+at+4.34.36+pm.png" width="320" /></a></div>
<div>
<span style="color: #ff2600;"><span style="font-size: x-small;"><br /></span></span></div>
<div>
<span style="font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;">Done!!</span></div>
<div>
<br /></div>
<div>
<b>2. Setup Cloud SDK with Development</b></div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">Google Cloud Messaging require Google Play Service SDK installed in our Development</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;"># Install </span>Google Play Service SDK</div>
<div>
<span style="font-family: Times;">Go to Android SDK manager —> Extras </span></div>
<div>
<span style="font-family: Times;">Install </span>Google Play Service SDK</div>
<div>
<span style="font-family: Times;">Install </span>Google Play Service for Froyo SDK (If your app supports android 2.2)</div>
<div>
<br /></div>
<div>
# Update Android Support Library</div>
<div>
Go to Android SDK manager —> Extras</div>
<div>
<span style="font-family: Times;">Install or Update Android Support Library</span> </div>
<div>
<span style="font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-qXO86uiPlEU/UpdLiAyb17I/AAAAAAAABRg/bAqGVt6mNSk/s1600/Screen+Shot+2013-11-28+at+7.24.07+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="247" src="http://4.bp.blogspot.com/-qXO86uiPlEU/UpdLiAyb17I/AAAAAAAABRg/bAqGVt6mNSk/s320/Screen+Shot+2013-11-28+at+7.24.07+pm.png" width="320" /></a></div>
<div>
<span style="font-size: x-small;"><br /></span></div>
<div>
<span style="font-size: x-small;"><br /></span></div>
<div>
# Import Google Play Service SDK into our Project</div>
<div>
<span style="font-family: Times;">Go to path where Android SDK is installed in your system</span></div>
<div>
<span style="font-family: Times;">copy the library project named “google-play-services_lib” from location</span></div>
<div>
<span style="font-family: Times;"><android_sdk>/extras/google/google_play_servies/libproject</android_sdk></span></div>
<div>
<span style="font-family: Times;">and paste where you maintain your lib projects</span></div>
<div>
Now come to project —> Import —>Android —> Existing Android code in Workspace</div>
<div>
<br /></div>
<div>
# Add Google Play Service Lib to build path</div>
<div>
<span style="font-family: Times;">Right click on project —> Build Path —> Configure Build Path</span></div>
<div>
<span style="font-family: Times;">Clic</span>k on Android —> Is Library —> Add —>Apply.</div>
<div>
<span style="font-size: x-small;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-9ZwXbaP3D78/UpdKfY0u8HI/AAAAAAAABRM/ukMeSgatLJQ/s1600/Screen+Shot+2013-11-28+at+5.05.20+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="203" src="http://3.bp.blogspot.com/-9ZwXbaP3D78/UpdKfY0u8HI/AAAAAAAABRM/ukMeSgatLJQ/s320/Screen+Shot+2013-11-28+at+5.05.20+pm.png" width="320" /></a></div>
<div>
<span style="font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;">Done!!</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<b>3. Configure project for GCM </b></div>
<div>
<br /></div>
<div>
<u># Update AndroidManifest file</u></div>
<div>
<span style="font-family: Times;">For enable GCM in our project we need to add few permission in our manifest file</span></div>
<div>
<span style="font-family: Times;">Go to <project_directory>AndroidManifest.xml and add below code</project_directory></span></div>
<div>
<span style="font-family: Times;"># Add Permission</span></div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">uses-permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"android.permission.INTERNET</span><span style="color: #3933ff; font-family: Monaco;"><span style="font-size: 11px;">”</span></span><span style="color: #009193; font-family: Monaco;"><span style="font-size: 11px;">/></span></span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">uses-permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"android.permission.GET_ACCOUNTS"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">uses-permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"android.permission.WAKE_LOCK"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span><span style="font-family: Monaco; font-size: 11px;"><br /></span><br />
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">uses-permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"android.permission.VIBRATE"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><br /></span></div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">uses-permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco;"><span style="font-size: 11px;">“<your_package_name_here>.permission.RECEIVE"</your_package_name_here></span></span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span><br />
<div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">uses-permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco;"><span style="font-size: 11px;">“<</span></span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">your_package_name_here</span><span style="color: #3933ff; font-family: Monaco;"><span style="font-size: 11px;">>.permission.C2D_MESSAGE"</span></span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span></div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">permission</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco;"><span style="font-size: 11px;">“<</span></span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">your_package_name_here</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">>.permission.C2D_MESSAGE"</span></div>
<div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:protectionLevel</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"signature"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;"># Add GCM Broadcast Receiver declaration</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">add GCM Broadcast Receiver declaration in your application tag</span></div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">application</span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"> <</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">receiver</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;"><your_package_name_here></your_package_name_here></span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">.GcmBroadcastReceiver"</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:permission</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"com.google.android.c2dm.permission.SEND"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">]]></span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">intent-filter</span><span style="color: #009193; font-family: Monaco; font-size: 11px;">]]></span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">action</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"com.google.android.c2dm.intent.RECEIVE"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">category</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;"><your_package_name_here></your_package_name_here></span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;"></</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">intent-filter</span><span style="color: #009193; font-family: Monaco; font-size: 11px;">]]></span><span style="font-family: Monaco; font-size: 11px;"><br /></span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;"></</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">receiver</span><span style="color: #009193; font-family: Monaco; font-size: 11px;">]]></span></div>
<div>
<span style="color: #009193; font-family: Monaco;"><span style="font-size: 11px;"> </span></span></div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">application/></span><span style="color: #009193; font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;"># Add GCM Servie </span>declaration</div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">application</span><span style="font-size: x-small;"><br /></span></div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"> <</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">service</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #932192; font-family: Monaco; font-size: 11px;">android:name</span><span style="font-family: Monaco; font-size: 11px;">=</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;"><your_package_name_here></your_package_name_here></span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">.GcmIntentService"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #009193; font-family: Monaco; font-size: 11px;">/></span></div>
<div>
<span style="color: #009193; font-family: Monaco; font-size: 11px;"><</span><span style="color: #4e9192; font-family: Monaco; font-size: 11px;">application/></span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<u># Update Proguard Config File</u></div>
<div>
<span style="font-family: Times;">If you are using Proguard in your application then you must configure </span></div>
<div>
<span style="font-family: Times;">Go to </span><project_directory>proguard-</project_directory>project.txt File and add below code</div>
<div>
<u><span style="font-size: x-small;"><br /></span></u></div>
<div>
<span style="font-family: Times; font-size: x-small;">———————————————————————</span></div>
<div>
<span style="font-size: x-small;">-keep class * extends java.util.ListResourceBundle {</span></div>
<div>
<pre><span style="font-family: Times; font-size: x-small;"> protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@ccom.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}</span></pre>
<pre><span style="font-family: Times; font-size: x-small;">———————————————————————</span></pre>
</div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<b>4. Get Device Registration ID</b><span style="font-family: Times;"><br /></span></div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">Now When device launch the app we need to Get the Registration ID to Send Message</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">So Go to your Launch/Splash Activity </span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">#Add Constants and Class Variables</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">PLAY_SERVICES_RESOLUTION_REQUEST</span><span style="font-family: Monaco; font-size: 11px;"> = 9000;</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">EXTRA_MESSAGE</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"message"</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">PROPERTY_REG_ID</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"registration_id"</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">PROPERTY_APP_VERSION</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"appVersion"</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"LaunchActivity"</span><span style="font-family: Monaco; font-size: 11px;">;<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">protected</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">SENDER_ID</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"your_sender_id"</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> GoogleCloudMessaging </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">gcm</span><span style="font-family: Monaco; font-size: 11px;"> =</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">regid</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">;</span><br />
<div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> Context </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">context</span><span style="font-family: Monaco; font-size: 11px;">= </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;"># Update OnCreate and OnResume methods</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<span style="color: #777777; font-family: Monaco; font-size: 11px;">@Override</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">protected</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onCreate(Bundle savedInstanceState)<br />{</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">super</span><span style="font-family: Monaco; font-size: 11px;">.onCreate(savedInstanceState);</span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> setContentView(R.layout.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">activity_launch</span><span style="font-family: Monaco; font-size: 11px;">);</span><span style="font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">context</span><span style="font-family: Monaco; font-size: 11px;"> = getApplicationContext();</span></div>
<div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (checkPlayServices()) </span>
</div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">gcm</span><span style="font-family: Monaco; font-size: 11px;"> = GoogleCloudMessaging.getInstance(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;">);<br /> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">regid</span><span style="font-family: Monaco; font-size: 11px;"> = getRegistrationId(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">context</span><span style="font-family: Monaco; font-size: 11px;">);<br /><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">regid</span><span style="font-family: Monaco; font-size: 11px;">.isEmpty())<br /> {<br /> registerInBackground();<br /> }<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">else</span><span style="font-family: Monaco; font-size: 11px;"><br /> {<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"No valid Google Play Services APK found."</span><span style="font-family: Monaco; font-size: 11px;">);<br /> }<br /> }</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;"> }</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"><br /></span></div>
<span style="color: #777777; font-family: Monaco; font-size: 11px;">@Override</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">protected</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onResume()<br />{<br /> <span style="white-space: pre;"> </span></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">super</span><span style="font-family: Monaco; font-size: 11px;">.onResume();</span><span style="font-family: Monaco; font-size: 11px;"> <span style="white-space: pre;"> </span>checkPlayServices();</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span><span style="font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;"># Implement GCM Required methods (Add below methods in LaunchActivity)</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">boolean</span><span style="font-family: Monaco; font-size: 11px;"> checkPlayServices() {<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;">);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (resultCode != ConnectionResult.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">SUCCESS</span><span style="font-family: Monaco; font-size: 11px;">) {<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {<br /> GooglePlayServicesUtil.getErrorDialog(resultCode, </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;">,<br /> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">PLAY_SERVICES_RESOLUTION_REQUEST</span><span style="font-family: Monaco; font-size: 11px;">).show();<br /> } </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">else</span><span style="font-family: Monaco; font-size: 11px;"> {<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"This device is not supported - Google Play Services."</span><span style="font-family: Monaco; font-size: 11px;">);<br /> finish();<br /> }<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">false</span><span style="font-family: Monaco; font-size: 11px;">;<br /> }<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">true</span><span style="font-family: Monaco; font-size: 11px;">;</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;"> }</span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"><br /></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> String getRegistrationId(Context context) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">{<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> SharedPreferences prefs = getGCMPreferences(context);<br /> String registrationId = prefs.getString(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">PROPERTY_REG_ID</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">""</span><span style="font-family: Monaco; font-size: 11px;">);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (registrationId.isEmpty()) {<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Registration ID not found."</span><span style="font-family: Monaco; font-size: 11px;">);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">""</span><span style="font-family: Monaco; font-size: 11px;">;<br /> }</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> registeredVersion = prefs.getInt(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">PROPERTY_APP_VERSION</span><span style="font-family: Monaco; font-size: 11px;">, Integer.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">MIN_VALUE</span><span style="font-family: Monaco; font-size: 11px;">);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> currentVersion = getAppVersion(context);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (registeredVersion != currentVersion) {<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"App version changed."</span><span style="font-family: Monaco; font-size: 11px;">);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">""</span><span style="font-family: Monaco; font-size: 11px;">;<br /> }<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> registrationId;</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span><span style="font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<br /></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> SharedPreferences getGCMPreferences(Context context) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">{</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> getSharedPreferences(LaunchActivity.</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">class</span><span style="font-family: Monaco; font-size: 11px;">.getSimpleName(),<br /> Context.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">MODE_PRIVATE</span><span style="font-family: Monaco; font-size: 11px;">);</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span></div>
<div>
<span style="font-family: Monaco;"><span style="font-size: 11px;"><br /></span></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> getAppVersion(Context context) </span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;">{<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">try</span><span style="font-family: Monaco; font-size: 11px;"> </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> PackageInfo packageInfo = context.getPackageManager()<br /> .getPackageInfo(context.getPackageName(), 0);<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> packageInfo.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">versionCode</span><span style="font-family: Monaco; font-size: 11px;">;<br /> } </span></div>
<div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;"> catch</span><span style="font-family: Monaco; font-size: 11px;"> (NameNotFoundException e) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">throw</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">new</span><span style="font-family: Monaco; font-size: 11px;"> RuntimeException(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Could not get package name: "</span><span style="font-family: Monaco; font-size: 11px;"> + e);<br /> }</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span></div>
<div>
<span style="font-family: Monaco;"><span style="font-size: 11px;"><br /></span></span></div>
<div>
<span style="font-family: Monaco;"><span style="font-size: 11px;"><br /></span></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> registerInBackground() </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">{</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> new</span><span style="font-family: Monaco; font-size: 11px;"> AsyncTask() {<br /> </span><span style="color: #777777; font-family: Monaco; font-size: 11px;">Override</span><span style="font-family: Monaco; font-size: 11px;"><br /><span style="white-space: pre;"> </span></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">protected</span><span style="font-family: Monaco; font-size: 11px;"> Object doInBackground(Object... params) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> String msg = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">""</span><span style="font-family: Monaco; font-size: 11px;">;<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">try</span><span style="font-family: Monaco; font-size: 11px;"> </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">gcm</span><span style="font-family: Monaco; font-size: 11px;"> == </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">gcm</span><span style="font-family: Monaco; font-size: 11px;"> = GoogleCloudMessaging.getInstance(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">context</span><span style="font-family: Monaco; font-size: 11px;">);<br /> }<br /> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">regid</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">gcm</span><span style="font-family: Monaco; font-size: 11px;">.register(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">SENDER_ID</span><span style="font-family: Monaco; font-size: 11px;">);</span><span style="font-family: Monaco; font-size: 11px;"> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"########################################"</span><span style="font-family: Monaco; font-size: 11px;">);<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Current Device's Registration ID is: "</span><span style="font-family: Monaco; font-size: 11px;">+msg); </span><span style="font-family: Monaco; font-size: 11px;"><br /> } </span></div>
<div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;"> catch</span><span style="font-family: Monaco; font-size: 11px;"> (IOException ex) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> msg = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Error :"</span><span style="font-family: Monaco; font-size: 11px;"> + ex.getMessage();<br /> </span><span style="font-family: Monaco; font-size: 11px;">}</span></div>
<div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;"> return</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">;<br /> }</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> protected</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onPostExecute(Object result) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> { //to do here };</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> }.execute(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">);</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span><br />
<div>
<div>
<div>
<br /></div>
<div>
<span style="color: #ff2600; font-family: Times; font-size: x-small;">Note : </span><span style="color: #ff2600; font-size: x-small;">please store<b> REGISTRATION_KEY</b>, it is important for sending PN Message to GCM</span></div>
<div>
<span style="color: #ff2600; font-family: Times; font-size: x-small;">also keep in mine this will be unique for all device, by using this only GCM will send Push Notification.</span></div>
<div>
<br /></div>
<div>
<b>5. Send Push Notifications</b></div>
<div>
<br /></div>
<div>
<u># If you are sending message from your local web server</u></div>
<div>
<br /></div>
<div>
<span style="font-family: Times;">While launching/login app you need to send REGISTRATION_ID to your web server.</span></div>
<div>
<span style="font-family: Times;">Actually Web server needs below three variable to send Message to GCM</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
REGISTRATION ID<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">API Key</span></div>
<div>
<span style="font-family: Times;">Message Text</span></div>
<div>
<br /></div>
<div>
<u># If you are sending message from your Android app itself </u></div>
<div>
<u><br /></u></div>
<div>
here you need to add below code in your activity to send Message </div>
<div>
<span style="font-size: 12px;"><br /></span></div>
<div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onClick(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> View view) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">{</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">new</span><span style="font-family: Monaco; font-size: 11px;"> AsyncTask() {</span><span style="color: #777777; font-family: Monaco; font-size: 11px;"> @Override</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">protected</span><span style="font-family: Monaco; font-size: 11px;"> String doInBackground(Void... params) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {<br /> String msg = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">""</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">try</span><span style="font-family: Monaco; font-size: 11px;"> {<br /> Bundle data = </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">new</span><span style="font-family: Monaco; font-size: 11px;"> Bundle();<br /> data.putString(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"my_message"</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Hello World"</span><span style="font-family: Monaco; font-size: 11px;">);<br /> data.putString(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"my_action"</span><span style="font-family: Monaco; font-size: 11px;">,<br /> </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"com.google.android.gcm.demo.app.ECHO_NOW"</span><span style="font-family: Monaco; font-size: 11px;">);<br /> String id = Integer.toString(msgId.incrementAndGet());</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">gcm</span><span style="font-family: Monaco; font-size: 11px;">.send(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">SENDER_ID</span><span style="font-family: Monaco; font-size: 11px;"> + </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"@<a href="http://gcm.googleapis.com/">gcm.googleapis.com</a>"</span><span style="font-family: Monaco; font-size: 11px;">, id, data);<br /> msg = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Sent message"</span><span style="font-family: Monaco; font-size: 11px;">;<br /> } </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">catch</span><span style="font-family: Monaco; font-size: 11px;"> (IOException ex) {<br /> msg = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Error :"</span><span style="font-family: Monaco; font-size: 11px;"> + ex.getMessage();<br /> }<br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">return</span><span style="font-family: Monaco; font-size: 11px;"> msg;<br /> }</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #777777; font-family: Monaco; font-size: 11px;">@Override</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">protected</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onPostExecute(String msg) {<br /> mDisplay.append(msg + </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"\n"</span><span style="font-family: Monaco; font-size: 11px;">);<br /> }<br /> }.execute(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">null</span><span style="font-family: Monaco; font-size: 11px;">);</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span></div>
</div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<b>6. Receive Push Notifications</b></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">#Add GCM Broadcast Receiver Class</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">As we have already declared “GcmBroadcastReceiver.java” in our Manifest file, So lets create this class</span></div>
<div>
<span style="font-family: Times;">update receiver class code this way</span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">class</span><span style="font-family: Monaco; font-size: 11px;"> GcmBroadcastReceiver </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">extends</span><span style="font-family: Monaco; font-size: 11px;"> WakefulBroadcastReceiver {<br /> </span><span style="color: #777777; font-family: Monaco; font-size: 11px;">@Override</span><span style="font-family: Monaco; font-size: 11px;"><br /> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onReceive(Context context, Intent intent) </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> {</span><span style="font-family: Monaco; font-size: 11px;"> ComponentName comp = </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">new</span><span style="font-family: Monaco; font-size: 11px;"> ComponentName(context.getPackageName(),<br /> GcmIntentService.</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">class</span><span style="font-family: Monaco; font-size: 11px;">.getName());</span><span style="font-family: Monaco; font-size: 11px;"> startWakefulService(context, (intent.setComponent(comp)));<br /> setResultCode(Activity.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">RESULT_OK</span><span style="font-family: Monaco; font-size: 11px;">);<br /> Toast.makeText(context, </span><span style="color: #3933ff; font-family: Monaco;"><span style="font-size: 11px;">“wow!! received new push notification"</span></span><span style="font-family: Monaco; font-size: 11px;">, Toast.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">LENGTH_LONG</span><span style="font-family: Monaco; font-size: 11px;">).show();<br /> }</span><br />
<div>
<span style="font-family: Monaco; font-size: 11px;">}</span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times;">#Add GCM Service Class</span></div>
<div>
<span style="font-family: Times;"><br /></span></div>
<div>
<span style="font-family: Times;">As we have already declared “GcmBroadcastReceiver.java” in our Manifest file, So lets create this class</span></div>
<div>
update receiver class code this way<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">class</span><span style="font-family: Monaco; font-size: 11px;"> GcmIntentService </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">extends</span><span style="font-family: Monaco; font-size: 11px;"> IntentService<br />{</span><span style="font-family: Monaco; font-size: 11px; white-space: pre;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">NOTIFICATION_ID</span><span style="font-family: Monaco; font-size: 11px;"> = 1;</span><span style="font-family: Monaco; font-size: 11px; white-space: pre;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> NotificationManager </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">mNotificationManager</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="font-family: Monaco; font-size: 11px; white-space: pre;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">final</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">static</span><span style="font-family: Monaco; font-size: 11px;"> String </span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;"> = </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"GcmIntentService"</span><span style="font-family: Monaco; font-size: 11px;">;</span><span style="font-family: Monaco; font-size: 11px; white-space: pre;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">public</span><span style="font-family: Monaco; font-size: 11px;"> GcmIntentService() {<br /><span style="white-space: pre;"> </span></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">super</span><span style="font-family: Monaco; font-size: 11px;">(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"GcmIntentService"</span><span style="font-family: Monaco; font-size: 11px;">); </span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> }</span><span style="color: #777777; font-family: Monaco; font-size: 11px;"> @Override</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> protected</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> onHandleIntent(Intent intent) {<br /> Bundle extras = intent.getExtras();<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Notification Data Json :"</span><span style="font-family: Monaco; font-size: 11px;"> + extras.getString(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"message"</span><span style="font-family: Monaco; font-size: 11px;">));<br /><br /> GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;">);<br /> String messageType = gcm.getMessageType(intent);</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> if</span><span style="font-family: Monaco; font-size: 11px;"> (!extras.isEmpty()) {</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> if</span><span style="font-family: Monaco; font-size: 11px;"> (GoogleCloudMessaging.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">MESSAGE_TYPE_SEND_ERROR</span><span style="font-family: Monaco; font-size: 11px;"><br /> .equals(messageType)) {<br /> sendNotification(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Send error: "</span><span style="font-family: Monaco; font-size: 11px;"> + extras.toString());<br /> } </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">else</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (GoogleCloudMessaging.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">MESSAGE_TYPE_DELETED</span><span style="font-family: Monaco; font-size: 11px;"><br /> .equals(messageType)) {<br /> sendNotification(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Deleted messages on server: "</span><span style="font-family: Monaco; font-size: 11px;"><br /> + extras.toString());</span><span style="color: #4e9072; font-family: Monaco; font-size: 11px;"> // If it's a regular GCM message, do some work.</span><span style="font-family: Monaco; font-size: 11px;"><br /> } </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">else</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">if</span><span style="font-family: Monaco; font-size: 11px;"> (GoogleCloudMessaging.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">MESSAGE_TYPE_MESSAGE</span><span style="font-family: Monaco; font-size: 11px;"><br /> .equals(messageType)) {</span><span style="color: #4e9072; font-family: Monaco; font-size: 11px;"><br /></span></div>
<div>
<span style="color: #4e9072; font-family: Monaco; font-size: 11px;"> // This loop represents the service doing some work.</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> for</span><span style="font-family: Monaco; font-size: 11px;"> (</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">int</span><span style="font-family: Monaco; font-size: 11px;"> i = 0; i < 5; i++) {<br /> Log.d(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">,</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">Working... "</span><span style="font-family: Monaco; font-size: 11px;"> + (i + 1) + </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"/5 @ "</span></div>
<div>
<span style="font-family: Monaco; font-size: 11px;"> + SystemClock.elapsedRealtime());</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> try</span><span style="font-family: Monaco; font-size: 11px;"> {<br /> Thread.sleep(5000);<br /> } </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">catch</span><span style="font-family: Monaco; font-size: 11px;"> (InterruptedException e) {<br /> }<br /> }<br /> Log.i(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">TAG</span><span style="font-family: Monaco; font-size: 11px;">, </span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Completed work @ "</span><span style="font-family: Monaco; font-size: 11px;"> + SystemClock.elapsedRealtime());<br /> sendNotification(extras.getString(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"message"</span><span style="font-family: Monaco; font-size: 11px;">));<br /> }<br /> }</span><span style="color: #4e9072; font-family: Monaco; font-size: 11px;"> // Release the wake lock provided by the WakefulBroadcastReceiver.</span><span style="font-family: Monaco; font-size: 11px;"><br /> GcmBroadcastReceiver.completeWakefulIntent(intent);<br /> }</span><span style="color: #4e9072; font-family: Monaco; font-size: 11px;"> // Put the message into a notification and post it.</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #4e9072; font-family: Monaco; font-size: 11px;"> // This is just one simple example of what you might choose to do with</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #4e9072; font-family: Monaco; font-size: 11px;"> // a GCM message.</span><span style="font-family: Monaco; font-size: 11px;"><br /></span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> private</span><span style="font-family: Monaco; font-size: 11px;"> </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">void</span><span style="font-family: Monaco; font-size: 11px;"> sendNotification(String msg) {</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;"> mNotificationManager</span><span style="font-family: Monaco; font-size: 11px;"> = (NotificationManager) </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;"><br /> .getSystemService(Context.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">NOTIFICATION_SERVICE</span><span style="font-family: Monaco; font-size: 11px;">);<br /> PendingIntent contentIntent = PendingIntent.getActivity(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;">, 0,</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> new</span><span style="font-family: Monaco; font-size: 11px;"> Intent(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">this</span><span style="font-family: Monaco; font-size: 11px;">, LaunchActivity.</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">class</span><span style="font-family: Monaco; font-size: 11px;">), 0);<br /><br /> NotificationCompat.Builder mBuilder = </span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">new</span><span style="font-family: Monaco; font-size: 11px;"> NotificationCompat.Builder(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;"> this</span><span style="font-family: Monaco; font-size: 11px;">)<br /> .setSmallIcon(R.drawable.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">icon</span><span style="font-family: Monaco; font-size: 11px;">)<br /> .setContentTitle(</span><span style="color: #3933ff; font-family: Monaco; font-size: 11px;">"Ocutag Snap"</span><span style="font-family: Monaco; font-size: 11px;">)<br /> .setStyle(</span><span style="color: #931a68; font-family: Monaco; font-size: 11px;">new</span><span style="font-family: Monaco; font-size: 11px;"> NotificationCompat.BigTextStyle().bigText(msg))<br /> .setContentText(msg)<br /> .setDefaults(Notification.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">DEFAULT_SOUND </span><span style="font-family: Monaco; font-size: 11px;">| Notification.</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">DEFAULT_VIBRATE</span><span style="font-family: Monaco; font-size: 11px;">);<br /><br /> mBuilder.setContentIntent(contentIntent);</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;"> mNotificationManager</span><span style="font-family: Monaco; font-size: 11px;">.notify(</span><span style="color: #0326cc; font-family: Monaco; font-size: 11px;">NOTIFICATION_ID</span><span style="font-family: Monaco; font-size: 11px;">, mBuilder.build());<br /> }</span><br />
<div style="font-family: Arial;">
<span style="font-family: Monaco; font-size: 11px;">}</span><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div style="font-family: Arial;">
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div style="font-family: Arial;">
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div style="font-family: Arial;">
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div style="font-family: Arial;">
<span style="font-family: Times;">That’s It We have Done!! no more coding. Just Build the App</span></div>
<div style="font-family: Arial;">
<span style="font-family: Times;">If you sending Message from your own Web server tell them to send Message</span></div>
<div style="font-family: Arial;">
<span style="font-family: Times;">or else you can send message from your own device.</span></div>
<div style="font-family: Arial;">
<span style="font-family: Times;"><br /></span></div>
<div style="font-family: Arial;">
<span style="font-family: Times;">Run the app. And you should Receive the Push Notification on Your Device with Sound + Vibration alert.</span></div>
<div style="font-family: Arial;">
<br />
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times;">Cheers!! We Did It.</span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times;"><br /></span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times;">For more info please! Check below references : </span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times;"><br /></span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times;">Google Documentation</span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<a href="http://developer.android.com/google/gcm/gs.html">http://developer.android.com/google/gcm/gs.html</a><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<a href="http://developer.android.com/google/gcm/client.html">http://developer.android.com/google/gcm/client.html</a></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<span style="font-family: Times;">Google Open Source Code</span></div>
<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->
<br />
<div>
<a href="http://code.google.com/p/gcm/source/checkout">http://code.google.com/p/gcm/source/checkout</a><span style="font-family: Times; font-size: x-small;"><br /></span></div>
<div>
<br /></div>
</div>
</div>
</div>
</div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com2Bangalore, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-2971558731672404737.post-25023396934969483482013-11-18T04:58:00.003-08:002013-11-21T21:15:13.028-08:00LG- Google Nexus 5 - Released : October 31, 2013<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-size: 12px;">
<b>Google Nexus 5</b> :<span style="color: #669c35;"><b> <i>If you are a nexus fan</i></b></span>,<span style="font-family: Arial;"> </span>Google is all set to launch its much awaited Nexus 5 Smartphone in India October 31, 2013.</div>
<div style="font-size: 12px;">
With several retailers across metro cities confirming the arrival of stocks by Monday, 18th November,</div>
<div style="font-size: 12px;">
Price for 16GB Nexus 5 price quoted to be Rs. 28,900 ($349) and 32GB price to be Rs. 32,999 ( $399).</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
<b>Tech Specs:</b></div>
<div style="font-size: 12px;">
<br /></div>
<div style="font-size: 12px;">
Model<b> - LG- Google Nexus 5</b></div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Camera front - 8.0-megapixel , back - 1.3-megapixel</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Screen 4.95 inch, 1920x1080 display (445 ppi)</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Qualcomm quad-core 2.26GHz processor</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Network -3G And 4G/LTE and Dualband Wi-Fi</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Android™ 4.4, KitKat®</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Wireless charging</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>•<span class="Apple-tab-span" style="white-space: pre;"> </span>Battery -Talk time: up to 17 hours</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span> Standby mode: 300 hours.</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
<b>Images:</b></div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-family: Helvetica; font-size: 12px;">
</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="font-size: 12px; min-height: 14px;">
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.androidcentral.com/sites/androidcentral.com/files/imagecache/w680h550/postimages/108579/n18_0.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="225" src="http://www.androidcentral.com/sites/androidcentral.com/files/imagecache/w680h550/postimages/108579/n18_0.jpg" width="400" /></a></div>
<br /></div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-mMOLuVSqbys/UooOFQbfs0I/AAAAAAAABOs/B22PMgBDtIw/s1600/nexus5-2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://1.bp.blogspot.com/-mMOLuVSqbys/UooOFQbfs0I/AAAAAAAABOs/B22PMgBDtIw/s400/nexus5-2.jpg" width="351" /></a></div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
<b>Reviews:</b></div>
<div style="font-size: 12px;">
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.youtube.com/embed/1dg2UIzIt4s?feature=player_embedded' frameborder='0'></iframe></div>
<div style="font-size: 12px;">
<b><br /></b></div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="color: #5e30eb; font-size: 12px;">
<span style="color: #0061ff;">More Details: </span>http://www.ibtimes.co.in/articles/522715/20131116/google-nexus-5-release-indian-store-29990.htm</div>
<div style="color: #5e30eb; font-size: 12px;">
<br /></div>
<div style="font-size: 12px;">
Cheers!!</div>
<div style="font-size: 12px;">
-RDC</div>
</div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0Bangalore, Karnataka, India12.9715987 77.59456269999998312.4764182 76.949115699999979 13.4667792 78.240009699999987tag:blogger.com,1999:blog-2971558731672404737.post-3245522419288068722013-11-14T05:26:00.000-08:002013-11-14T05:27:32.848-08:00How to check Memory Leaks using Eclipse Memory Analyzer Tool (MAT) in Android App<div dir="ltr" style="text-align: left;" trbidi="on">
Eclipse Memory Analyzer Tool (MAT)<br />
Java handles its memory in two areas. The heap and the stack.<br />
<br />
<b>Java Heap</b><br />
In the heap the Java Virtual Machine (JVM) stores all objects created by the Java application, e.g. by using the "new" operator. The Java garbage collector (gc) can logically separate the heap into different areas, so that the gc can faster identify objects which can get removed<br />
The memory for new objects is allocated on the heap at run time. Instance variables live inside the object in which they are declared.<br />
<br />
<b>Java Stack</b><br />
Stack is where the method invocations and the local variables are stored. If a method is called then its stack frame is put onto the top of the call stack. The stack frame holds the state of the method including which line of code is executing and the values of all local variables. The method at the top of the stack is always the current running method for that stack. Threads have their own call stack.<br />
<br />
<b>Install MAT on Eclipse</b><br />
We need to install two eclipse plugins<br />
1. BIRT Chart Engine <i>//For Getting Memory Info with Pie Chart diagrams</i><br />
2. MAT <i>//For Getting Memory Info</i><br />
——————————————————————————————<br />
Install BIRT Chart Engine plugins<br />
1. Eclipse —> Help Menu —> Install New Software —> Add<br />
2. Enter below information<br />
----------------------------------------------------------------------------<br />
| Name : BIRT Chart Engine |<br />
| Location: http://download.eclipse.org/birt/update-site/4.3 |<br />
----------------------------------------------------------------------------<br />
<br />
Source: http://wiki.eclipse.org/BIRT_Update_Site_URL<br />
Now select only Birt Chart Engine and Go for Install.<br />
<br />
——————————————————————————————<br />
Install MAT plugins<br />
1. Eclipse —> Help Menu —> Install New Software —> Add<br />
2. Enter below information<br />
----------------------------------------------------------------------------<br />
| Name : MAT |<br />
| Location: http://download.eclipse.org/mat/1.3/update-site/ |<br />
----------------------------------------------------------------------------<br />
<br />
Source:http://www.eclipse.org/mat/downloads.php<br />
Now select MAT and Birt Chart Plugin for MAT and Go for Install.<br />
——————————————————————————————<br />
<b>Get Heap Dump of Android Project</b><br />
Once you installed .. <br />
1. Connect Android Device for Debugging/Testing App<br />
2. Now Go the The project and Build/Run<br />
3. Open DDMS —> Devices —> Select Process (Project’s Package Name in List)<br />
4. Click the "Dump HPROF File" icon in the Devices view's toolbar (looks like a half-filled can with a downward-pointing arrow)<br />
<br />
5. Select the option Leak Suspects Report and click Finish.<br />
6. That’s it!!.. You Got the App’s Memory Heap Report<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-WN0Ctf-gCmI/UoTPu6rRUTI/AAAAAAAABM8/qt1jHV0_A_g/s1600/Screen+Shot+2013-11-14+at+6.31.16+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-WN0Ctf-gCmI/UoTPu6rRUTI/AAAAAAAABM8/qt1jHV0_A_g/s320/Screen+Shot+2013-11-14+at+6.31.16+pm.png" width="297" /></a></div>
<br />
<br />
——————————————————————————————<br />
<b>Analyse Memory Leaks, Allocation & GC etc.</b><br />
<br />
Heap Overview<br />
<br />
Few Important Links<br />
http://android-developers.blogspot.in/2011/03/memory-analysis-for-android.html<br />
http://2min2code.com/articles/eclipse_memory_analyzer/overview<br />
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasictutorial.html<br />
http://eclipsesource.com/blogs/2013/01/21/10-tips-for-using-the-eclipse-memory-analyzer/</div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0Southern Asia12.897489183755891 78.75-17.354874316244111 37.441406 43.149852683755896 120.058594tag:blogger.com,1999:blog-2971558731672404737.post-41504594418386810552013-08-06T05:24:00.000-07:002013-11-15T05:37:24.387-08:00How to pick the Image thumb-nail from gallery in android<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: Helvetica; font-size: 12px;">
We can pick the Image thumb-nail from gallery using Intent.</div>
<div style="font-family: Helvetica; font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-family: Helvetica; font-size: 12px; min-height: 14px;">
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-EVs16euEn8E/UoYf0RAVkYI/AAAAAAAABNY/RNir1rsMcF4/s1600/0.jpg.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://2.bp.blogspot.com/-EVs16euEn8E/UoYf0RAVkYI/AAAAAAAABNY/RNir1rsMcF4/s320/0.jpg.png" width="192" /></a></div>
<br /></div>
<div style="font-family: Helvetica; font-size: 12px; min-height: 14px;">
<br /></div>
<div style="color: #cc1c00; font-size: 12px;">
<i>Note : at the end of this Tutorial , you can find zip source code of this example .</i></div>
<div style="font-family: Helvetica; font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
There are two types of thumbnails available:</div>
<div style="font-size: 12px;">
MINI_KIND: 512 x 384 thumbnail</div>
<div style="font-size: 12px;">
MICRO_KIND: 96 x 96 thumbnail (Size is 18 or 36 kb depends on Devices)</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
1. We need to Create an Intent for pick Image from Gallery.</div>
<div style="font-family: Monaco; font-size: 11px;">
Intent i = <span style="color: #931a68;">new</span> Intent(Intent.<span style="color: #0326cc;">ACTION_PICK</span>,</div>
<div style="font-family: Monaco; font-size: 11px;">
android.provider.MediaStore.Images.Media.<span style="color: #0326cc;">EXTERNAL_CONTENT_URI</span>);</div>
<div style="font-family: Monaco; font-size: 11px;">
startActivityForResult(i, <span style="color: #0326cc;">RESULT_LOAD_IMAGE</span>);</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<br /></div>
<div style="font-size: 12px;">
2. Override the method to get the result Image thumb-nail picked from Android Device.</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="color: #777777; font-family: Monaco; font-size: 11px;">
@Override</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #931a68;">protected</span> <span style="color: #931a68;">void</span> onActivityResult(<span style="color: #931a68;">int</span> requestCode, <span style="color: #931a68;">int</span> resultCode, Intent data) {</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #931a68;">super</span>.onActivityResult(requestCode, resultCode, data);</div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>//Here is the place we got picked thumb-nail data</div>
<div style="font-size: 12px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
3. Get thumb-nail from Data result.</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px;">
ContentResolver resolver = getContentResolver();</div>
<div style="font-family: Monaco; font-size: 11px;">
Uri actualUri = data.getData();</div>
<div style="font-family: Monaco; font-size: 11px;">
List<string> uriPath = actualUri.getPathSegments();</string></div>
<div style="font-family: Monaco; font-size: 11px;">
<span style="color: #931a68;">long</span> imageId = Long.parseLong(uriPath.get(uriPath.size() - 1));</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px;">
Bitmap thumb = Thumbnails.getThumbnail(resolver, imageId,Thumbnails.<span style="color: #0326cc;">MICRO_KIND</span>, <span style="color: #931a68;">null</span>);</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
4. Get thumb-nail Path.</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px;">
<span style="color: #931a68;">private</span> String getFilePath(Uri data){</div>
<div style="font-family: Monaco; font-size: 11px;">
String path = <span style="color: #3933ff;">""</span>;</div>
<div style="font-family: Monaco; font-size: 11px;">
path = data.getPath();</div>
<div style="font-family: Monaco; font-size: 11px;">
String[] filePathColumn = { MediaStore.Images.Media.<span style="color: #0326cc;">DATA</span> };</div>
<div style="font-family: Monaco; font-size: 11px;">
Cursor cursor = getContentResolver().query(data,filePathColumn, <span style="color: #931a68;">null</span>, <span style="color: #931a68;">null</span>, <span style="color: #931a68;">null</span>);</div>
<div style="font-family: Monaco; font-size: 11px;">
<span style="color: #931a68;">if</span>(cursor!=<span style="color: #931a68;">null</span>){</div>
<div style="font-family: Monaco; font-size: 11px;">
cursor.moveToFirst();</div>
<div style="font-family: Monaco; font-size: 11px;">
<span style="color: #931a68;">int</span> columnIndex = cursor.getColumnIndex(filePathColumn[0]);</div>
<div style="font-family: Monaco; font-size: 11px;">
path = cursor.getString(columnIndex);</div>
<div style="font-family: Monaco; font-size: 11px;">
cursor.close();</div>
<div style="font-family: Monaco; font-size: 11px;">
}</div>
<div style="font-family: Monaco; font-size: 11px;">
<span style="color: #931a68;">return</span> path;</div>
<div style="font-family: Monaco; font-size: 11px;">
}</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<br /></div>
<div style="font-size: 12px;">
4. Rotate Thumb-nail if it’s required.</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-family: Monaco; font-size: 11px;">
String thumbPath = getFilePath(actualUri);<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="font-family: Monaco; font-size: 11px;">
ExifInterface exif;</div>
<div style="color: #931a68; font-family: Monaco; font-size: 11px;">
try<span style="color: black;"> </span></div>
<div style="font-family: Monaco; font-size: 11px;">
{</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>exif = <span style="color: #931a68;">new</span> ExifInterface(thumbPath);</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #931a68;">int</span> orientation = exif.getAttributeInt(</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>ExifInterface.<span style="color: #0326cc;">TAG_ORIENTATION</span>, 0);</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Matrix matrix = <span style="color: #931a68;">new</span> Matrix();</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #931a68;">if</span> (orientation == 6) {</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>matrix.postRotate(90);<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>} <span style="color: #931a68;">else</span> <span style="color: #931a68;">if</span> (orientation == 3) {</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>matrix.postRotate(180);</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>} <span style="color: #931a68;">else</span> <span style="color: #931a68;">if</span> (orientation == 8) {</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>matrix.postRotate(270);</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>/*</div>
<div style="font-family: Monaco; font-size: 11px;">
<span style="color: #e32400;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Note:</span> resultBitmap (I have declared in Class)</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>[<span style="color: #931a68;">private</span> Bitmap <span style="color: #0326cc;">resultBitmap</span> = <span style="color: #931a68;">null</span>;]</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>*/</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="color: #0326cc; font-family: Monaco; font-size: 11px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span><span style="color: #931a68;">if</span><span style="color: black;">(</span>resultBitmap<span style="color: black;"> != </span><span style="color: #931a68;">null</span><span style="color: black;">)</span></div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div style="color: #3933ff; font-family: Monaco; font-size: 11px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span>Log.d(</span>""<span style="color: black;">, </span>"Recycling:resultBitmap"<span style="color: black;">);</span></div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #0326cc;">resultBitmap</span>.recycle();</div>
<div style="color: #0326cc; font-family: Monaco; font-size: 11px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>resultBitmap<span style="color: black;"> =</span><span style="color: #931a68;">null</span><span style="color: black;">;</span></div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="color: #4e9072; font-family: Monaco; font-size: 11px;">
<span style="color: black;"><span class="Apple-tab-span" style="white-space: pre;"> </span></span>//Create bitmap thumb-nail with perfect orientation</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #0326cc;">resultBitmap</span> = Bitmap.createBitmap(thumb, 0, 0, thumb.getWidth(),thumb.getHeight(), matrix, <span style="color: #931a68;">true</span>);</div>
<div style="font-family: Monaco; font-size: 11px;">
} <span style="color: #931a68;">catch</span> (Exception e) {</div>
<div style="font-family: Monaco; font-size: 11px;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();</div>
<div style="font-family: Monaco; font-size: 11px;">
}</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="font-size: 12px;">
5. Set the Result Image Thumb-nail Bitmap on ImageView</div>
<div style="font-size: 12px; min-height: 14px;">
<br /></div>
<div style="color: #0326cc; font-family: Monaco; font-size: 11px;">
imageView<span style="color: black;">.setImageBitmap(</span>resultBitmap<span style="color: black;">);</span></div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<br /></div>
<div style="color: #e32400; font-family: Monaco; font-size: 11px;">
Note: What if Image doesn’t have Thumbnail.</div>
<div style="color: #e32400; font-family: Monaco; font-size: 11px;">
I have handled it in sample code, we will get the image and create thumbnail.</div>
<div style="font-family: Monaco; font-size: 11px; min-height: 15px;">
<br /></div>
<div style="font-size: 12px;">
We will create very simple app to do this work</div>
<div style="font-size: 12px;">
-------------------------------------------</div>
<div style="font-size: 12px;">
App Name: <b>PickImageThumbnailFromGallery</b></div>
<div style="font-size: 12px;">
Package Name: <b>com.rdc. PickImageThumbnailFromGallery</b></div>
<div style="font-size: 12px;">
Android SDK: <b>Android SDK 2.2 / API 8</b></div>
<div style="font-size: 12px;">
Default Activity Name: <b>MainActivity.java</b></div>
<br />
<div style="font-size: 12px;">
-------------------------------------------</div>
<br />
<b>MainActivity.java</b><br />
<br />
<pre class="java" name="code">package com.rdc.pickimagethumbnailfromgallery;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ImageView imageView = null;
private Button button = null;
private Bitmap bitmap = null;
private static final int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
ContentResolver resolver = getContentResolver();
Uri actualUri = data.getData();
List<string> uriPath = actualUri.getPathSegments();
long imageId = Long.parseLong(uriPath.get(uriPath.size() - 1));
Bitmap thumb = Thumbnails.getThumbnail(resolver, imageId,
Thumbnails.MICRO_KIND, null);
//There is no thumb-nail with this Image
if (thumb == null) {
// Log.d(TAG, "Failed to get thumb-nail for this image.");
Toast.makeText(getApplicationContext(),
"Failed to get thumbnail for our image.",
Toast.LENGTH_SHORT).show();
//so create thumb-nail from image itself
Cursor cursor = resolver
.query(actualUri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
cursor.close();
imageView.setImageBitmap(this.createImageThumbnail(imageFilePath,
100, 100));
}
//We got the thumb-nail from gallery, rotate if needed else use on ImageView
else {
String thumbPath = getFilePath(actualUri);
ExifInterface exif;
try {
exif = new ExifInterface(thumbPath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = Bitmap.createBitmap(thumb, 0, 0, thumb.getWidth(),
thumb.getHeight(), matrix, true);
if (thumb != bitmap) {
thumb.recycle();
thumb = null;
}
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Bitmap createImageThumbnail(String imagePath, int width, int height) {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
/ (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
/ (float) width);
if (heightRatio > 1 || widthRatio > 1) {
if (heightRatio > widthRatio) {
bmpFactoryOptions.inSampleSize = heightRatio;
} else {
bmpFactoryOptions.inSampleSize = widthRatio;
}
}
bmpFactoryOptions.inJustDecodeBounds = false;
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = BitmapFactory.decodeFile(imagePath, bmpFactoryOptions);
return bitmap;
}
private String getFilePath(Uri data) {
String path = "";
path = data.getPath();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(data, filePathColumn, null,
null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex);
cursor.close();
}
return path;
}
}
</string></pre>
<br /></div>
<b>activity_main.xml</b><br />
<pre class="xml" name="code"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PickImage" />
</LinearLayout>
</pre>
<br />
<b>ApplicatoinManifest.xml</b><br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc.pickimagethumbnailfromgallery"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.rdc.pickimagethumbnailfromgallery.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<br />
The output Screen will be like this..<br />
<br />
<span style="color: red;">Note: before click on upload image button make sure your Android Device is not connected with Computer.</span><br />
<br />
Click to get thumbnail<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-sG_Nbv93gEs/UoYgXmk2NUI/AAAAAAAABNg/0agzHIYj8Ko/s1600/1.jpg.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-sG_Nbv93gEs/UoYgXmk2NUI/AAAAAAAABNg/0agzHIYj8Ko/s320/1.jpg.png" width="192" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
You can download the complete source code zip file here : <a href="https://www.opendrive.com/files?M18yNDI2ODU0MV9MZWJRUA" target="_blank"><span style="color: #6aa84f;"><b>PickImageThumbnailFromGallery.Zip</b></span></a><span style="color: #0b5394;"><b></b></span></div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-3816742232416679722013-01-03T00:08:00.000-08:002013-01-03T00:08:57.559-08:00ListView with CheckBox using ArrayAdatpter in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Happy New Year to you All Android Developer :)<br />
<br />
Today I am going to create a small Android application for<br />
showing Check Box in List View using Array Adapter in Android,<br />
So lets start it now..<br />
<br />
<i><span style="color: #cc0000;">Note : at the end of this Tutorial , you can find zip source code of this example .</span></i><br />
<br />
We will create very simple app to do this work<br />
-------------------------------------------<br />
App Name: <b>CheckBoxListArrayAdapter</b><br />
Package Name: <b>com.rdc.activity</b><br />
Android SDK: <b>Android SDK 2.2 / API 8</b><br />
Default Activity Name: <b>MainActivity.java</b><br />
-------------------------------------------<br />
<br />
<b>MainActivity</b><b>.java</b><br />
<b><br /></b>
<b><br /></b>
<br />
<pre class="java" name="code">package com.rdc.activity;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.rdc.adapter.PlanetArrayAdapter;
import com.rdc.model.Planet;
import com.rdc.model.PlanetViewHolder;
public class MainActivity extends Activity {
private ListView mainListView = null;
private Planet[] planets = null;
private ArrayAdapter<Planet> listAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainListView = (ListView) findViewById(R.id.mainListView);
// When item is tapped, toggle checked properties of CheckBox and
// Planet.
mainListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
Planet planet = listAdapter.getItem(position);
planet.toggleChecked();
PlanetViewHolder viewHolder = (PlanetViewHolder) item
.getTag();
viewHolder.getCheckBox().setChecked(planet.isChecked());
}
});
// Create and populate planets.
planets = (Planet[]) getLastNonConfigurationInstance();
if (planets == null) {
planets = new Planet[] { new Planet("Mercury"),
new Planet("Venus"), new Planet("Earth"),
new Planet("Mars"), new Planet("Jupiter"),
new Planet("Saturn"), new Planet("Uranus"),
new Planet("Neptune"), new Planet("Ceres"),
new Planet("Pluto"), new Planet("Haumea"),
new Planet("Makemake"), new Planet("Eris") };
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll(Arrays.asList(planets));
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter(listAdapter);
}
public Object onRetainNonConfigurationInstance() {
return planets;
}
}
</pre>
<br />
PlanetArrayAdapter.java<br />
<pre class="java" name="code">package com.rdc.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.rdc.activity.R;
import com.rdc.model.Planet;
import com.rdc.model.PlanetViewHolder;
/** Custom adapter for displaying an array of Planet objects. */
public class PlanetArrayAdapter extends ArrayAdapter<Planet> {
private LayoutInflater inflater;
public PlanetArrayAdapter(Context context, List<Planet> planetList) {
super(context, R.layout.simplerow, R.id.rowTextView, planetList);
//Cache the LayoutInflate to avoid asking for a new one each time.
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Planet planet = (Planet) this.getItem(position);
CheckBox checkBox;
TextView textView;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.simplerow, null);
textView = (TextView) convertView.findViewById(R.id.rowTextView);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
convertView.setTag(new PlanetViewHolder(textView, checkBox));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Planet planet = (Planet) cb.getTag();
planet.setChecked(cb.isChecked());
}
});
}
// Re-use existing row view
else {
PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
.getTag();
checkBox = viewHolder.getCheckBox();
textView = viewHolder.getTextView();
}
checkBox.setTag(planet);
// Display planet data
checkBox.setChecked(planet.isChecked());
textView.setText(planet.getName());
return convertView;
}
}
</pre>
<br />
Planet.java<br />
<pre class="java" name="code">package com.rdc.model;
/** Holds planet data. */
public class Planet {
private String name = "";
private boolean checked = false;
public Planet() {
}
public Planet(String name) {
this.name = name;
}
public Planet(String name, boolean checked) {
this.name = name;
this.checked = checked;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String toString() {
return name;
}
public void toggleChecked() {
checked = !checked;
}
}
</pre>
<br />
PlanetViewHolder.java<br />
<pre class="java" name="code">package com.rdc.model;
import android.widget.CheckBox;
import android.widget.TextView;
/** Holds child views for one row. */
public class PlanetViewHolder {
private CheckBox checkBox;
private TextView textView;
public PlanetViewHolder() {
}
public PlanetViewHolder(TextView textView, CheckBox checkBox) {
this.checkBox = checkBox;
this.textView = textView;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
</pre>
<br />
main.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainListView">
</ListView>
</LinearLayout>
</pre>
<br />
simplerow.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>
<CheckBox
android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
</pre>
<br />
AndroidManifest.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<br />
Here is the Result screen<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-0KEXkv0TlHM/UOU6mgwzPDI/AAAAAAAAArU/mFWx8aiAB4I/s1600/listview_checkBox_with+ArrayAdapter.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-0KEXkv0TlHM/UOU6mgwzPDI/AAAAAAAAArU/mFWx8aiAB4I/s320/listview_checkBox_with+ArrayAdapter.jpg" width="214" /></a></div>
<br />
<br />
<span style="background-color: white; color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">You can download the complete source code zip file here : </span><span style="background-color: white;"><b><span style="color: #4c7c00; font-family: Times New Roman, Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><a href="https://www.opendrive.com/files?M180NjI1MTAxX0FCRVo2" target="_blank">CheckBoxListArrayAdapter.zip</a></span></span></b></span></div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com3Bangalore, Karnataka, India12.9715987 77.59456269999998312.4764147 76.949115699999979 13.4667827 78.240009699999987tag:blogger.com,1999:blog-2971558731672404737.post-24157565373749820882012-08-12T03:01:00.000-07:002012-12-07T03:11:51.446-08:00How to Pick Image from Gallery in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
Today I just learn and going to share with you,<br />
how we can get an image From Gallery in our Android app<br />
so below is the step by step description also at last<br />
you can find zip source code of this example.<br />
<br />
We will create very simple app to do this work<br />
-------------------------------------------<br />
App Name: <b>PickGalleryImage</b><br />
Package Name: <b>com.rdc.activity</b><br />
Android SDK: <b>Android SDK 2.3.3 / API 10</b><br />
Default Activity Name: <b>PickGalleryImageActivity.java</b><br />
-------------------------------------------<br />
<br />
<b>PickGalleryImageActivity.java</b><br />
<br />
<pre class="java" name="code">package com.rdc.activity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class PickGalleryImageActivity extends Activity implements
OnClickListener {
private static int RESULT_LOAD_IMAGE = 1;
private Button btnLoadImage = null;
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLoadImage = (Button) findViewById(R.id.button_LoadPicture);
btnLoadImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore
.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE &&
resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
}
}
}
</pre>
<b>main.xml</b><br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/imageView"></ImageView>
<Button
android:layout_height="wrap_content"
android:text="Load Picture"
android:layout_width="wrap_content"
android:layout_weight="0"
android:layout_gravity="center"
android:id="@+id/button_LoadPicture"></Button>
</LinearLayout>
</pre>
<b>AndroidManifest.xml</b><br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc.activity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".PickGalleryImageActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<br />
The output Screen will be like this..<br />
<br />
<span style="color: red;">Note: before click on upload image button make sure your Android Device is not connected with Computer.</span><br />
<br />
Click to upload image<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-gOGCd5zSqI8/UMHLLJZW1HI/AAAAAAAAAqw/UbpTvoFcLuU/s1600/browIMage.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-gOGCd5zSqI8/UMHLLJZW1HI/AAAAAAAAAqw/UbpTvoFcLuU/s320/browIMage.jpg" width="213" /></a></div>
<br />
<br />
Just select any one image and see you got it here<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-qFtPW7ICj_A/UMHLVbulMzI/AAAAAAAAAq4/I-kvOTghzEk/s1600/loadedImageFromgallery.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-qFtPW7ICj_A/UMHLVbulMzI/AAAAAAAAAq4/I-kvOTghzEk/s320/loadedImageFromgallery.jpg" width="213" /></a></div>
<br />
You can download the complete source code zip file here : <span style="color: #0b5394;"><b><a href="https://www.opendrive.com/files?M180MzgzMjkyX0pLOENW" target="_blank">PickGalleryimage.zip</a></b></span></div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com2 Bangalore, Karnataka, India12.9715987 77.594562712.7238042 77.2787057 13.219393199999999 77.910419699999991tag:blogger.com,1999:blog-2971558731672404737.post-81444646537690725902012-07-31T04:16:00.000-07:002012-07-31T04:16:00.966-07:00Google Map Tutorial Android (Advance)<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Hello Android Guys...hope you doing great!!<br />
<br />
We know already how to get Google map in our Android App see my earlier tutorial post<br />
<a href="http://rdcworld-android.blogspot.in/2012/07/google-map-tutorial-android-basic.html" target="_blank"><b><span style="color: #38761d;">Google Map Tutorial Android (Basic)</span></b></a><br />
<br />
<b><span style="color: red;">Note:</span></b> Because we need to Get Google map API key to Get map in our app<br />
I have wrote step by step simple tutorial see and put in you main.xml file see below<br />
<br />
<div style="text-align: left;">
<a href="http://rdcworld-android.blogspot.in/2012/07/google-map-tutorial-android-basic.html" target="_blank"><b><span style="color: #38761d;">Obtain Google Maps Android API Key</span></b></a></div>
<br />
Okay so today we are going to add some more new things to our Google Maps<br />
<br />
1. Enter location name and Go to particular location<br />
2. Bookmark location (POI) place of interest on map<br />
3. Get the location name/coordinates where you touched on map<br />
4. Zoom-in and Zoom-out map location<br />
<br />
Let's start coding stuff..create an Android app with<br />
<br />
<br />
<br />
-------------------------------------------<br />
App Name: GoogleMapAdvance<br />
Package Name: com.rdc<br />
Android SDK: Android SDK 2.3.3 / Google API 10<br />
Default MapActivity Name: ActivityGoogleMap<br />
-------------------------------------------<br />
<div>
<br /></div>
ActivityGoogleMap<br />
<br />
<pre class="java" name="code">package com.rdc.gmap;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class ActivityGoogleMap extends MapActivity
implements OnClickListener {
private MapView mapView = null;
private Button btnGo = null;
private EditText editLocation = null;
private MapController mController = null;
private GeoPoint gPoint = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// to add zoom in and zoom out default buttons
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
btnGo = (Button) findViewById(R.id.buttonGo);
btnGo.setOnClickListener(this);
editLocation = (EditText) findViewById(R.id.editText1);
// load bangalore as default location
browseLocation();
// ---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
public void onClick(View v) {
if (btnGo == v) {
String location = editLocation.getText().toString();
if (location.equalsIgnoreCase("")) {
Toast.makeText(getBaseContext(),"Please Enter location!!",
Toast.LENGTH_SHORT).show();
} else {
String map_location = "geo:0,0?q=" + location;
Uri geoUri = Uri.parse(map_location);
Intent mapCall = new Intent(Intent.ACTION_VIEW, geoUri);
startActivity(mapCall);
// clear old location string
location = null;
}
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
// to load particular location on g-map
public void browseLocation() {
mController = mapView.getController();
String coordinates[] = { "12.917233", "77.620811" };
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
gPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mController.animateTo(gPoint);
mController.setZoom(17);
}
// create inner class to add marker at any place on g-map
class MapOverlay extends com.google.android.maps.Overlay {
// add bookmark on map
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
// ---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(gPoint, screenPts);
// ---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.red_pushpin2);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
return true;
}
// get the location where you touched on map
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
// ---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(), (int) event.getY());
Geocoder geoCoder = new Geocoder(getBaseContext(),
Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
p.getLatitudeE6()/ 1E6, p.getLongitudeE6()/ 1E6,
1);
String add = "";
if (addresses.size() > 0) {
for (int i = 0; i < addresses.get(0)
.getMaxAddressLineIndex(); i++)
add += addresses.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add,
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
return true;
} else
return false;
}
}
}
</pre>
main.xml
<br />
<br />
<span style="color: red;"><b>Note:</b></span> make sure you must have Google Map API key to put in Map View in main.xml file<br />
(This is the mistake often made by newbie)<br />
<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:weightSum="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/linearLayout1"
android:layout_weight="0.02"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight=".10"
android:id="@+id/buttonGo"
android:text=" Go "></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:lines="1"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_marginRight="2dp"
android:layout_weight=".90"></EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/laymap"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_weight="0.98">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0iuCzAK4N1AoTya_fr62sB7NXXPqkWqF-OCNMEg" />
</LinearLayout>
<!-- you need to replace your Google map API key here see apiKey="" -->
</LinearLayout>
</pre>
<br />
and Manifest file is<br />
<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc.gmap"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".ActivityGoogleMap"
android:theme="@android:style/Theme.NoTitleBar"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<br />
<br />
Default Output screen will load as Bangalore and Mark it<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-taIRUrQVJlQ/UBe7BR3HNxI/AAAAAAAAAoI/vbxU3ov4BNI/s1600/gmapop1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-taIRUrQVJlQ/UBe7BR3HNxI/AAAAAAAAAoI/vbxU3ov4BNI/s1600/gmapop1.png" /></a></div>
<br />
<br />
if you touch any place on map it shows details like this<br />
<br />
when you enter any location and click Go...it loads new location...<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-QryKkXi8c9g/UBe79-iRkTI/AAAAAAAAAoQ/TAQosG54kak/s1600/gmapop3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-QryKkXi8c9g/UBe79-iRkTI/AAAAAAAAAoQ/TAQosG54kak/s1600/gmapop3.png" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-3HpHDCmOjAE/UBe8J9n5ukI/AAAAAAAAAoY/tCV2jWwG5c4/s1600/gmapop4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-3HpHDCmOjAE/UBe8J9n5ukI/AAAAAAAAAoY/tCV2jWwG5c4/s1600/gmapop4.png" /></a></div>
<br />
<br />
You can download the complete source code zip file here : <a href="https://www.opendrive.com/files?M18yMTM4NTI3XzJUbFZ4" target="_blank"><b><span style="color: #3d85c6;">GoogleMapAdvance</span></b></a><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!
<br />
<br /></div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com3tag:blogger.com,1999:blog-2971558731672404737.post-41779326661763996872012-07-30T07:13:00.000-07:002012-07-30T22:47:17.129-07:00Google Map Tutorial Android (Basic)<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
In this tutorial we will learn how can we add google map into our android app in a very easy & simple way.<br />
<br />
Note: Because the Maps library is not a part of the standard Android library, you must remember four things at-least to get G-Map in our app .<br />
<br />
1. Obtaining a Google Maps Android API Key to get G-Map view in our app.<br />
2.Select Android SDK with Google APIs (when create new app)<br />
3. We need to extends MapActivity<br />
4. Your emulator also created with Google API SDK to run Google map apps (before run app).<br />
<br />
so let's try this small app<br />
<br />
First is first: <b><span style="color: #38761d; font-size: large;">Obtain Google Maps Android API</span></b><br />
***************************************************************************<br />
1. Create a folder in C drive called "<span class="geshifilter"><code class="geshifilter-text">C:\Android</code></span>"<br />
<br />
2. Go to this path "<span class="geshifilter"><code class="geshifilter-text">C:\Program Files\Java\<JDK_version_number>\bin</code></span>"<br />
or where you installed java, copy "keytool.exe" file and put in our created folder "
<span style="font-family: monospace;">Android</span> ".<br />
<br />
3. Now we need to look for "debug.keystore" for probably you can find here<br />
<br />
<ul style="text-align: left;">
<li> Windows Vista: C:\Users\\.android\debug.keystore</li>
<li> Windows XP: C:\Documents and Settings\\.android\debug.keystore</li>
<li> OS X and Linux: ~/.android/debug.keystore </li>
</ul>
<br />
In my system i got here "C:\Users\RDC\.android\debug.keystore"<br />
<br />
Now copy this file and put into our folder "
<span style="font-family: monospace;">Android</span> "<br />
<br />
4. open command prompt and go to bin folder in prompt.<br />
then hit command<br />
--------------------------------------------------------------------------------------------------------<br />
<pre class="geshifilter-text"><b>keytool.exe -list -alias androiddebugkey -keystore "C:\android\debug.keystore" -storepass android -keypass android</b></pre>
--------------------------------------------------------------------------------------------------------<br />
Now we need to copy MD5 key so right click on command prompt and "mark" then copy and save in Text file. see below image...<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-vApPAv9H2j8/UBaLXQfi7fI/AAAAAAAAAm8/_bjWCFlbPds/s1600/gmapcmd2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-vApPAv9H2j8/UBaLXQfi7fI/AAAAAAAAAm8/_bjWCFlbPds/s1600/gmapcmd2.jpg" /></a></div>
<br />
<br />
Copy MD5 key and open this <a href="http://code.google.com/android/maps-api-signup.html" target="_blank"><b><span style="color: #3d85c6;">developer-page</span></b></a> and sign in.<br />
then paste here MD5 fingerprint and Generate API key this way<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-24tsNcKBc5c/UBaPFV7c2QI/AAAAAAAAAnQ/4GfIvKY0jkA/s1600/gmapkey1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="222" src="http://3.bp.blogspot.com/-24tsNcKBc5c/UBaPFV7c2QI/AAAAAAAAAnQ/4GfIvKY0jkA/s400/gmapkey1.jpg" width="400" /></a></div>
<br />
after generating key the result screen will be<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-WJm1eTnaQZg/UBaPV8Qwi-I/AAAAAAAAAnY/nLkKK4iVUgo/s1600/gmapkey2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="178" src="http://3.bp.blogspot.com/-WJm1eTnaQZg/UBaPV8Qwi-I/AAAAAAAAAnY/nLkKK4iVUgo/s400/gmapkey2.jpg" width="400" /></a></div>
<br />
you can use this key whenever wants to develop Google map app in this development environment only.<br />
<br />
Okay Great!! now we are ready to go...make sure don't use mine key its valid only for my development environment so use your own key :D<br />
<br />
*****************************************************************************<br />
<br />
<br />
Let's create new android app this way..<br />
<br />
-------------------------------------------<br />
App Name: AlertBoxBasic<br />
Package Name: com.rdc<br />
Android SDK: Android SDK 2.3.3 / API 10<br />
Default MapActivity Name: ActivityGoogleMap<br />
-------------------------------------------<br />
<br />
ActivityGoogleMap.java
<br />
<pre class="java" name="code">package com.rdc.gmap;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class ActivityGoogleMap extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//to add zoom in and zoom out default buttons
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
</pre>
<br />
look important Google API key in main.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0iuCzAK4N1AoTya_fr62sB7NXXPqkWqF-OCNMEg" />
</LinearLayout>
</pre>
<br />
and Manifest file must be like<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc.gmap"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".ActivityGoogleMap"
android:theme="@android:style/Theme.NoTitleBar"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<br />
Now create Emulator using Google API SDK see below image<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-SK1xcrIfVLI/UBaUwSKkhpI/AAAAAAAAAns/knzBktjHWB8/s1600/gmapemu.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-SK1xcrIfVLI/UBaUwSKkhpI/AAAAAAAAAns/knzBktjHWB8/s1600/gmapemu.jpg" /></a></div>
<br />
The output Screen will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-ykUlc7FbK3o/UBaVfTPufqI/AAAAAAAAAn0/Zl5hR9pSBrk/s1600/gmapbasicop.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-ykUlc7FbK3o/UBaVfTPufqI/AAAAAAAAAn0/Zl5hR9pSBrk/s1600/gmapbasicop.jpg" /></a></div>
<br />
<br />
You can download the complete source code zip file here : <a href="https://www.opendrive.com/files?M18yMTM3MjE4XzB1WFRQ" target="_blank"><b><span style="color: #3d85c6;">GoogleMapBasic</span></b></a><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!
<br />
<br /></div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com2tag:blogger.com,1999:blog-2971558731672404737.post-26567670247253317312012-07-04T05:25:00.000-07:002012-07-26T03:28:38.409-07:00Android Tricks<div dir="ltr" style="text-align: left;" trbidi="on">
<b><span style="background-color: white;">How to </span>Disable Home Button</b><br />
<pre class="java" name="code"> @Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.
LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
</pre>
<br />
<b><span style="background-color: white;">How to </span>Disable Back Button</b><br />
<pre class="java" name="code"> @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return false;
}
</pre>
<br />
<b><span style="background-color: white;">How to </span>Disable Soft Keypad</b><br />
<pre class="java" name="code">final EditText txtName = (EditText) findViewById(R.id.txtName);
txtName.setInputType(InputType.TYPE_NULL);
</pre>
<br />
<b>How to Make Static Rotation/orientation in Android</b><span style="background-color: white;"></span><br />
<pre class="java" name="code"> //if you want to lock screen for always Portrait mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
or
//if you want to lock screen for always Landscape mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
</pre>
<br />
<b><br /></b><br />
<b>How to Disable screen Rotation/orientation in Android (better way is java code)</b><span style="background-color: white;"></span><br />
<pre class="xml" name="code">
//put this code in Manifest file, activity tag
android:screenOrientation="nosensor"
android:configChanges="keyboardHidden|orientation|screenSize"
</pre>
<pre class="java" name="code">
/* or even you can do it by programmatically -- just put
Configuration code in onResume method before calling super
like this */
@Override
protected void onResume() {
int currentOrientation = getResources().getConfiguration()
.orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo
.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo
.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
super.onResume();
}
</pre>
<b>How to Disable Title Bar and Make Full Screen View</b><br />
<pre class="java" name="code">
//1. put this line to manifest file in Application tag
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
//2. put below code in your activity onCreate method
//to disable notification bar (Top Bar)
requestWindowFeature(Window.FEATURE_NO_TITLE);
//to set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//make sure code should be before calling below method
setContentView(R.layout.main);
</pre>
<br />
<b>How to Create Alert Dialog Box in Android</b><br />
<pre class="java" name="code"> AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("App has been started..")
.setCancelable(false)
.setTitle("Alert Box")
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
</pre>
<br />
<br />
<b>How to Create Toast message in Android</b><br />
<pre class="java" name="code"> Toast.makeText(getApplicationContext(), "I am splash message..",
Toast.LENGTH_LONG).show();
</pre>
<br />
<b>How to create Progress Dialog in Android</b><br />
<pre class="java" name="code"> ProgressDialog dialog = ProgressDialog.show(this, "",
"Loading. Please wait...", true);
</pre>
<br />
<b>Load Home Screen Programmatically in Android</b><br />
<pre class="java" name="code"> //to load home screen put this code in onClick method of desired button
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain)
</pre>
<br />
<b>Start/Load Activity from Activity
</b><br />
<pre class="java" name="code">//put this code where you want to load another Activity
Intent intent = new Intent(FirstActivty.this, SecondActivity.class);
//below 2 lines (Flags) are optional
// 1. if set, it will clear the back stack
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 2. If set, this activity will become the start of a new task
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
</pre>
<br />
<b>Make a Phone Call</b><br />
<pre class="java" name="code">String mobileNo = "+919741817902";
String uri = "tel:" + mobileNo.trim() ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
//add permission to Manifest file
<uses-permission android:name="android.permission.CALL_PHONE">
</uses-permission></pre>
<b>How to check WiFi is Connected or Not</b><br />
<pre class="java" name="code">public void chekcWifiConnectDisconnect() {
ConnectivityManager connManager = (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
Log.v("Debug", "Wifi is connectd..");
} else {
Log.v("Debug", "Wifi is not connectd..");
}
}
//dont forget to put Wifi permission in manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
</pre>
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com2tag:blogger.com,1999:blog-2971558731672404737.post-1828417583558147362012-07-02T00:44:00.000-07:002012-07-04T00:25:34.239-07:00Read & Store Log-cat Programmatically in Android<div dir="ltr" style="text-align: left;" trbidi="on">
Android provide Logcat option with ADB to Debugg the Application.<br />
<br />
So we can test app on Emulator and with the help of Logcat we can easily trace the Error details.<br />
<br />
But if We are testing on Real Android Device and that is not connected with system then its hard know where we are getting exception, error etc.<br />
<br />
so for that i wrote this Tutorial<br />
<br />
We can read the Logcat details programmatically and also at run-time we can store in Text file and later we can see the error details.<br />
<br />
so what we will do in this code:<br />
<br />
<ul style="text-align: left;">
<li>Read the logcat</li>
<li>Showing the logcat on TextView</li>
<li>Create a Text file</li>
<li>store logcat into Text file</li>
<li>and finally save file into SDCard.</li>
</ul>
<br />
see below code snap..<br />
<br />
<pre class="java" name="code">//to read the logcat programmatically
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
{
log.append(line);
}
}
catch (IOException e) {
}
</pre>
<br />
<pre class="java" name="code">//to create a Text file name "logcat.txt" in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");
</pre>
<br />
<br />
don't forget to add permission for read log-cat in manifest file<br />
<br />
<pre class="xml" name="code"><uses-permission android:name="android.permission.READ_LOGS">
</uses-permission></pre>
<br />
add permission for writing text file to SDCard file<br />
<br />
<pre class="xml" name="code"><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission></pre>
<br />
So lets create a small App to do this things
<br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: <b>LogCollect</b></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: <b>LogTest</b></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<br />
LogTest.java<br />
<pre class="java" name="code">package com.rdc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
public class LogTest extends Activity {
private StringBuilder log;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
} catch (IOException e) {
}
//convert log to string
final String logString = new String(log.toString());
//create text file in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");
try {
//to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(logString);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
</pre>
<br />
main.xml
<br />
<pre class="xml" name="code"><scrollview android:fillviewport="true" android:id="@+id/scroller" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello">
</textview></linearlayout>
</scrollview>
</pre>
<br />
and the manifest file: make sure you need to add permission
<br />
<pre class="xml" name="code"><manifest android:versioncode="1" android:versionname="1.0" package="com.rdc" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="10">
<uses-permission android:name="android.permission.READ_LOGS">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".LogTest">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-permission></uses-permission></uses-sdk></manifest>
</pre>
<br />
you can check the stored file
open DDMS in eclipse and go to this path (in my cash)<br />
<br />
File Explorer<br />
<br />
data/mnt/sdcard/myLogcat/logcat.txt<br />
<br />
now you can pull out this "logfile.txt" and store in local system. see below snap shot<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-8rZ1J8Dce0E/T_KtwvOA6II/AAAAAAAAAhg/hes4WOwnSIA/s1600/logfileSDcard.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="348" src="http://3.bp.blogspot.com/-8rZ1J8Dce0E/T_KtwvOA6II/AAAAAAAAAhg/hes4WOwnSIA/s400/logfileSDcard.jpg" width="400" /></a></div>
<br />
<br />
also if you execute this app it will show you the log-cat on TextView with Scrolling
like this<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-nw-_DlsK6-Q/T_KirAuV3CI/AAAAAAAAAhU/VnRSQfaNqY0/s1600/logcatread.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://2.bp.blogspot.com/-nw-_DlsK6-Q/T_KirAuV3CI/AAAAAAAAAhU/VnRSQfaNqY0/s320/logcatread.jpg" width="216" /></a></div>
<br />
<span style="color: #6aa84f; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">You can download the complete source code zip file here :</span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="background-color: white; font-size: 15px; line-height: 20px;"><span style="color: #3d85c6; font-family: 'Times New Roman', Times, FreeSerif, serif;"><b><u><a href="https://www.opendrive.com/files?M18xOTIyNDYxX0dXYlJt" target="_blank">LogCollect</a></u></b></span></span><br />
<b style="color: #6ccd11; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></b><br />
cheers!!<br />
<br />
I'd love to hear your thoughts!
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com3tag:blogger.com,1999:blog-2971558731672404737.post-46699367441242246382012-06-28T08:47:00.000-07:002012-07-02T08:49:28.319-07:00Disable Title Bar or Full Screen View in Android<div dir="ltr" style="text-align: left;" trbidi="on">
Today i learn how to disable Notification bar (Battery, Network Status info Bar ) Top of the Screen.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-vpBAdJNIm9g/T_HBjw_LK1I/AAAAAAAAAhI/0KhmMCS0BaE/s1600/topbar.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="56" src="http://4.bp.blogspot.com/-vpBAdJNIm9g/T_HBjw_LK1I/AAAAAAAAAhI/0KhmMCS0BaE/s320/topbar.jpg" width="320" /></a></div>
<br />
and how can we set Full Screen View to Our Application Screen.<br />
<br />
Just a pretty simple code see below..<br />
<br />
<span style="color: #6aa84f;">//to disable notification bar (Top Bar)</span><br />
<span style="color: #674ea7;">requestWindowFeature(Window.FEATURE_NO_TITLE);</span><br />
<br />
<span style="color: #6aa84f;">//to set full screen view</span><br />
<span style="color: #674ea7;">getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,</span><br />
<span style="color: #674ea7;"> WindowManager.LayoutParams.FLAG_FULLSCREEN);</span><br />
<br />
<span style="color: red;"><b>Note:</b></span> Above code put before loading the main view or we can say before this line<br />
<span style="color: #674ea7;">setContentView(R.layout.main);</span><br />
<br />
<span style="background-color: white; color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">also i made simple app to achieve this if need then have look on it.</span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>DisableTitleBar</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>MyActivity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">so here is the code</span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>MyActivity.java</b></span></span>
<br />
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//to disable notification bar (Top Bar)
requestWindowFeature(Window.FEATURE_NO_TITLE);
//to set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//make sure code should be before calling below method
setContentView(R.layout.main);
}
}
</pre>
<br />
<b>main.xml
</b><br />
<pre class="xml" name="code"><linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<textview android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="I Don't have Title Bar" android:textsize="18sp">
<linearlayout android:gravity="center_vertical|center_horizontal" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="This is Full Screen View"></textview>
</linearlayout>
</textview></linearlayout>
</pre>
<br />
<b>AndroidManifest.xml
</b><br />
<pre class="xml" name="code"><manifest android:versioncode="1" android:versionname="1.0" package="com.rdc" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="10">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-sdk></manifest>
</pre>
<br />
The output will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-r-fLuxH1VwM/T_HBdbGEu2I/AAAAAAAAAhA/3p4gRBFe0Ms/s1600/notitle.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://3.bp.blogspot.com/-r-fLuxH1VwM/T_HBdbGEu2I/AAAAAAAAAhA/3p4gRBFe0Ms/s320/notitle.jpg" width="214" /></a></div>
<br />
<br />
<br />
I'd love to hear your thoughts!
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-10931944531448516242012-06-15T08:05:00.000-07:002012-07-02T08:08:14.266-07:00Disable Soft KeyPad in Android<div dir="ltr" style="text-align: left;" trbidi="on">
Today we will learn another small trick to disable the softkeypad in Android<br />
<br />
Sometime you need to disable the Soft Keypad / Virtual Keypad and want to input from either Physical Keypad or Design your own keypad.<br />
<br />
<span style="background-color: white;">Pretty</span> simple and only single line code i have written below<br />
<br />
<span style="color: #6aa84f;"> //disable soft keypad</span><br />
<span style="color: #674ea7;">txtName.setInputType(InputType.TYPE_NULL);</span><br />
<span style="color: #674ea7;"><br /></span><br />
<span style="background-color: white;">also i made simple app to achieve this if need then have look on it.</span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>DisableKeypad</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>DisableKeypadActivity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">so here is the code</span>
<br />
<br />
DisableKeypadActivity.java<br />
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
public class DisableKeypadActivity extends Activity {
private EditText txtName=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the id of edit text
txtName =(EditText) findViewById(R.id.txtName);
//disable soft keypad
txtName.setInputType(InputType.TYPE_NULL);
}
}
</pre>
<br />
main.xml
<br />
<pre class="xml" name="code"><linearlayout android:gravity="center_vertical|center_horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<textview android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="SoftKeypad is Disabled.. ">
<edittext android:hint="Enter your name.." android:id="@+id/txtName" android:layout_height="wrap_content" android:layout_margintop="40dp" android:layout_width="match_parent">
<requestfocus></requestfocus>
</edittext>
</textview></linearlayout>
</pre>
<br />
AndroidManifest.xml
<br />
<pre class="xml" name="code"><manifest android:versioncode="1" android:versionname="1.0" package="com.rdc" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="10">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".DisableKeypadActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-sdk></manifest>
</pre>
<br />
Tested on Real Androd Device [Sony Ericsson (XPERIA)] the output will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-2COKvAWp0FQ/T_G5A9DgNII/AAAAAAAAAg0/l5uedRZKyyk/s1600/disablekeypad.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-2COKvAWp0FQ/T_G5A9DgNII/AAAAAAAAAg0/l5uedRZKyyk/s320/disablekeypad.jpg" width="214" /></a></div>
<br />
<br />
I'd love to hear your thoughts!
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-50148994495803375022012-06-10T05:53:00.000-07:002012-07-31T05:54:45.400-07:00How to add Google ads in Android apps (Google Admob Ads)<div dir="ltr" style="text-align: left;" trbidi="on">
These day you can find many Android app are having great features you know what still they are FREE available in Android Market/Google Play why?<br />
<br />
Because many of those earning money through Google Ad-mob Ads showing in the apps.<br />
<br />
so the question is how we can integrated these Google or Any ads into our Android Apps.<br />
<br />
I am going to write this page to achieve this....<br />
<br />
Before starting codding stuff we need Google Advertisement Publisher ID.<br />
<br />
<b>Get Ads Publisher ID</b><br />
<br />
First of all you need to sign-up account at <a href="https://www.google.com/adsense" target="_blank"><b><span style="color: #38761d;">Google AdSense</span></b></a><br />
if you don't have then fill-up some bank and transaction stuff you will get you client ID<br />
<br />
eg. Mine is "a1500db2724b9b8"<br />
<br />
okay now let's create a small app with...<br />
-------------------------------------------<br />
App Name: GoogleAdMob<br />
Package Name: com.rdc<br />
Android SDK: Android SDK 2.3.3 / API 10<br />
Default Activity Name: ActivityGoogleAdMob<br />
-------------------------------------------<br />
<br />
<br />
<b>Download and Add Google AdMob SDK</b><br />
<br />
You can download latest <a href="https://developers.google.com/mobile-ads-sdk/download" target="_blank"><b><span style="color: #38761d;">Google AdMob Ads SDK</span></b></a> here<br />
now we need to add this sdk into our GoogleAdMob app<br />
<br />
So right click on project --> properties--><br />
java build path -->Libraries-->Add External JARs see below image<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-d0l6yD-NyeM/UBfKpc0fkRI/AAAAAAAAAow/M79k6JYL2B0/s1600/GoogleAdsSdk.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="273" src="http://3.bp.blogspot.com/-d0l6yD-NyeM/UBfKpc0fkRI/AAAAAAAAAow/M79k6JYL2B0/s400/GoogleAdsSdk.jpg" width="400" /></a></div>
<br />
<br />
<br />
Now start some coding stuff this way... your main activity should be..<br />
<br />
ActivityGoogleAdMob.java<br />
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
public class ActivityGoogleAdMob extends Activity {
private AdView adView = null;
private static final String ADMOB_ID = "a1500db2724b9b8";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adView
adView = new AdView(this, AdSize.BANNER, ADMOB_ID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="@+id/mainLayout"
LinearLayout lay = (LinearLayout) findViewById(R.id.mainLayout);
// Add the adView to it
lay.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
</pre>
<br />
main.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
</pre>
<br />
Manifest file will be
<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ActivityGoogleAdMob"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
</pre>
<br />
Note: you must have app target is API 13 or newer<br />
see the file "default.properties"<br />
<pre class="java" name="code"># This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-13
</pre>
<br />
The output Screen will be like this..<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-xi72ivL7pAE/UBfTlwqCisI/AAAAAAAAApI/LpJRfNPtiR4/s1600/GoogleAdsOP.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-xi72ivL7pAE/UBfTlwqCisI/AAAAAAAAApI/LpJRfNPtiR4/s1600/GoogleAdsOP.jpg" /></a></div>
<br />
<br />
You can download the Google AdMobSDK 6.0 and complete source code zip file here : <a href="https://www.opendrive.com/files?M18yMTM4Nzg2XzhKMWl5" target="_blank"><b><span style="color: #3d85c6;">GoogleAdMob</span></b></a><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!
</div>
RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-65139097296702836352012-06-02T07:04:00.000-07:002012-07-02T07:06:43.895-07:00Disable Android Screen Rotation/Orientation<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Some time you need to Disable or Lock the screen orientation.<br />
<br />
just need to add this code in your activity which you want to lock<br />
<br />
<br />
<span style="color: #6aa84f;">//for Portrait Mode only</span><br />
<b><span style="color: #674ea7;">setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</span></b><br />
<b><span style="color: #674ea7;"> or</span></b><br />
<span style="color: #6aa84f;">//for Landscape Mode only</span>
<br />
<b><span style="color: #674ea7;">setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);</span></b><br />
<br />
well its pretty simple to achieve this, still i have created a small app<br />
-------------------------------------------<br />
App Name: <b>DisableScreenOrientation</b><br />
Package Name: <b>com.rdc</b><br />
Android SDK: <b>Android SDK 2.3.3 / API 10</b><br />
Default Activity Name: <b>MyActivity</b><br />
-------------------------------------------<br />
so here is the code<br />
<br />
<b>MyActivity.java</b><br />
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//if you want to lock screen for always Portrait mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//if you want to lock screen for always Landscape mode
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
</pre>
<br />
<b>main.xml</b></div>
<pre class="xml" name="code"><linearlayout android:gravity="center_vertical|center_horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<textview android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="I will never Rotate with Screen" android:textsize="33sp">
</textview></linearlayout>
</pre>
and the manifest file is<br />
<b> AndroidManifest.xml
</b><br />
<pre class="xml" name="code"><manifest android:versioncode="1" android:versionname="1.0" package="com.rdc" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="10">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-sdk></manifest>
</pre>
<br />
Tested on Real Androd Device [Sony Ericsson (XPERIA)] the output will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-68R7WyoDFk8/T_GpxaM_GTI/AAAAAAAAAgo/3Mgqew57cmo/s1600/disablescreenorintation.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://3.bp.blogspot.com/-68R7WyoDFk8/T_GpxaM_GTI/AAAAAAAAAgo/3Mgqew57cmo/s320/disablescreenorintation.jpg" width="213" /></a></div>
<br />
<br />
I'd love to hear your thoughts! </div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-14016984016443466212012-05-14T12:31:00.000-07:002012-06-29T12:41:23.060-07:00Android Threading (Basic)<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Thread</b><br />
A thread is a sequential path of code execution
within a program. And each thread has its own local variables, program counter
and lifetime.<br />
<br />
in this example we create simplest thread by implementing Runnable interface.<br />
<b><br /></b><br />
<b>Start Coding..</b> so lets move ahead with some coding..<br />
<br />
create a Simple Android App Name "AndroidThreadingBasic" with 2.3.3 SDK,<br />
<br />
your Activity should be like this.<br />
<br />
<pre class="java" name="code">
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity implements OnClickListener {
//Declear the button and Textview instance variable
private Button btnStart=null;
private TextView tv=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the id of Button from xml file
btnStart = (Button) findViewById(R.id.button1);
btnStart.setOnClickListener(this);
//get the id of TextView from xml file
tv = (TextView) findViewById(R.id.textView1);
tv.setVisibility(View.INVISIBLE);
}
public void onClick(View v) {
if(v==btnStart){
tv.setVisibility(View.VISIBLE);
// create thread by implementing Runnable interface
new Thread(new Runnable() {
public void run() {
System.out.println("Thread is running now..");
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}).start();
}
}
}
</pre>
<br />
Now put this code in your main.xml file
<br />
<pre class="java" name="code"><linearlayout android:gravity="center_vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" android="http://schemas.android.com/apk/res/android" xmlns:="">
<linearlayout android:gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="match_parent">
<button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Start Thread">
</button>
</linearlayout>
<textview android:gravity="center_horizontal" android:id="@+id/textView1" android:layout_gravity="bottom" android:layout_height="wrap_content" android:layout_margintop="40dp" android:layout_width="fill_parent" android:text="Thread is running now..">
</textview>
</linearlayout>
</pre>
<br />
The output Screen will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-1VChtu-UsFI/T-4CkdEQ44I/AAAAAAAAAgQ/RVbzD40U8bI/s1600/AndroidThreadingBasicop.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="http://2.bp.blogspot.com/-1VChtu-UsFI/T-4CkdEQ44I/AAAAAAAAAgQ/RVbzD40U8bI/s400/AndroidThreadingBasicop.jpg" width="268" /></a></div>
<br />
and check your logcat, it will print 1 to 10 integers with message.<br />
<br />
<span style="color: red;">Note</span> : if you want to update UI then you should use Handler or AsyncTask Thread Check my Blogs below<br />
<br />
<b>Android Threading with Handler</b> and <b> </b><br />
<br />
<b>Android Threading With AsyncTask</b><br />
<br />
<br />
<br />
I'd love to hear your thoughts! </div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-79589160092560178802012-05-03T09:51:00.000-07:002012-05-16T10:02:41.260-07:00Calculate Device Moving Speed Programmatically<div dir="ltr" style="text-align: left;" trbidi="on">
so this evening i would like to share with you new thing what i have learn..<br />
<br />
some time we need to calculate moving speed of our android mobile device.<br />
<br />
this example will calculate the location coordinates latitude-longitude after every 5 seconds gap.<br />
and then calculate the speed of device in m/s.<br />
all work done in background by the service.<br />
<br />
so i we need to do is install this app into mobile and close now it started a background service<br />
and calculate the speed forever..<br />
[you can create without any activity also, but i have created an activity ans started the service from activity]<br />
<br />
let's begin the coding, first of all we need to create a simple project having single activity like this<br />
<b>DeviceMovingSpeed.java</b><br />
<pre class="java" name="code">
package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class DeviceMovingSpeed extends Activity// implements Runnable
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("Debug", "Activity started..");
Intent myIntent=new Intent(this,MyService.class);
startService(myIntent);
}
}
</pre>
<br />
Now we need to create a service where we will handle all the stuff
creating a thread
Getting current location
calculating coordinates latitude-longitude
calculating distance
and device moving speed
<br />
<b>MyService.java</b><br />
<pre class="java" name="code">
package com.rdc;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service
{
private LocationManager locManager;
private LocationListener locListener = new myLocationListener();
static final Double EARTH_RADIUS = 6371.00;
private boolean gps_enabled = false;
private boolean network_enabled = false;
private Handler handler = new Handler();
Thread t;
@Override
public IBinder onBind(Intent intent) {return null;}
@Override
public void onCreate() {}
@Override
public void onDestroy() {}
@Override
public void onStart(Intent intent, int startid) {}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
final Runnable r = new Runnable()
{ public void run()
{
Log.v("Debug", "Hello");
location();
handler.postDelayed(this, 5000);
}
};
handler.postDelayed(r, 5000);
return START_STICKY;
}
public void location(){
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try{
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch(Exception ex){}
try{
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
catch(Exception ex){}
Log.v("Debug", "in on create.. 2");
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locListener);
Log.v("Debug", "Enabled..");
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locListener);
Log.v("Debug", "Disabled..");
}
Log.v("Debug", "in on create..3");
}
private class myLocationListener implements LocationListener
{
double lat_old=0.0;
double lon_old=0.0;
double lat_new;
double lon_new;
double time=10;
double speed=0.0;
@Override
public void onLocationChanged(Location location) {
Log.v("Debug", "in onLocation changed..");
if(location!=null){
locManager.removeUpdates(locListener);
//String Speed = "Device Speed: " +location.getSpeed();
lat_new=location.getLongitude();
lon_new =location.getLatitude();
String longitude = "Longitude: " +location.getLongitude();
String latitude = "Latitude: " +location.getLatitude();
double distance =CalculationByDistance(lat_new, lon_new, lat_old, lon_old);
speed = distance/time;
Toast.makeText(getApplicationContext(), longitude+"\n"+latitude+"\nDistance is: "
+distance+"\nSpeed is: "+speed , Toast.LENGTH_SHORT).show();
lat_old=lat_new;
lon_old=lon_new;
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
public double CalculationByDistance(double lat1, double lon1, double lat2, double lon2) {
double Radius = EARTH_RADIUS;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}
}
</pre>
<br />
now come to <b>AndroidManifest.xml</b><br />
<pre class="java" name="code">
<manifest android:versioncode="1" android:versionname="1.0" package="com.kns" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="8">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name" debuggable="true">
<activity android:label="@string/app_name" android:name=".DeviceMovingSpeed">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
<service android:enabled="true" android:name=".MyService">
<intent-filter>
<action android:name="com.kns.MyService">
</action>
</intent-filter>
</service>
</application>
</uses-sdk></manifest>
</pre>
<br />
okay!! that's it.. now run the application toast will appear to say :-<br />
"current coordinates"<br />
"distance if device is moving"<br />
"and speed of device in m/s (meter/seconds)"<br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts! </div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com4tag:blogger.com,1999:blog-2971558731672404737.post-22320972802291853822012-04-27T00:16:00.000-07:002012-07-06T00:18:54.162-07:00How to Make a Phone Call Programmatically in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>MakeACall</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>MyActivity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span>
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;">MyActivity.java</span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></span>
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyActivity extends Activity implements OnClickListener {
private Button btncall = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btncall = (Button) findViewById(R.id.btnCall);
btncall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String mobileNo = "+919741817902";
String uri = "tel:" + mobileNo.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
}
</pre>
<br />
main.xml<br />
<pre class="xml" name="code"><linearlayout android:gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<button android:id="@+id/btnCall" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Make a Call"></button>
</linearlayout>
</pre>
<br />
AndroidManifest.xml<br />
<pre class="xml" name="code"><manifest android:versioncode="1" android:versionname="1.0" package="com.rdc" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="10">
<uses-permission android:name="android.permission.CALL_PHONE">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-permission></uses-sdk></manifest>
</pre>
<br />
The output Screen will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-mbBGvCqYU7Y/T_aQz4hB9FI/AAAAAAAAAiw/GaTs25o6eIY/s1600/makeacall.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-mbBGvCqYU7Y/T_aQz4hB9FI/AAAAAAAAAiw/GaTs25o6eIY/s320/makeacall.jpg" width="215" /></a></div>
<br />
<br />
You can download the complete source code zip file here : <a href="https://www.opendrive.com/files?M18xOTMxMDY4XzM2cUNz" target="_blank"><span style="color: #3d85c6;">MakeACall</span></a><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-70907619133511905932012-04-15T02:48:00.000-07:002012-07-17T05:59:05.944-07:00Start Service from Activity<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Some time we need to start a service from Android Activity..<br />
how can we achieve this i am going to write step by step.<br />
<br />
First of all we need to Create a simple application with an Activity<br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>Activity2Service</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>MyActivity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span>
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></span><br />
"MyActivity.java"<br />
<pre class="java" name="code">
package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("Debug", "Activity has been Started..");
Toast.makeText(getBaseContext(),
"Activity Started", Toast.LENGTH_SHORT)
.show();
Intent myIntent = new Intent(getBaseContext(), MyService.class);
startService(myIntent);
}
}
</pre>
<br />
Then Create a Service<br />
<pre class="java" name="code">
package com.rdc;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.v("Debug", "Service has been Created..");
// code to execute when the service is first created
}
@Override
public void onDestroy() {
// code to execute when the service is shutting down
}
// This method has been deprecated since API 5
/*
@Override public void onStart(Intent intent, int startid) {
code to execute when the service is starting up
}*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("Debug", "Service has been Started..");
Toast.makeText(getBaseContext(), "Service has been Started..",
Toast.LENGTH_SHORT).show();
// We want this service to continue running until it's explicitly
// stopped, so return sticky.
return START_STICKY;
}
}
</pre>
<br/>main.xml<br/>
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
</pre>
<br />
Don't forget to make entry in "Manifest" file for Service<br />
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:enabled="true"
android:name=".MyService">
<intent-filter>
<action android:name="com.rdc.MyService">
</action>
</intent-filter>
</service>
</application>
</manifest>
</pre>
<br />
Now Run Application, Toast will appear.. :)<br />
<br />
<span style="color: #6aa84f; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">You can download the complete source code zip file here :</span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="background-color: white; font-size: 15px; line-height: 20px;"><span style="color: #3d85c6; font-family: 'Times New Roman', Times, FreeSerif, serif;"><a href="https://www.opendrive.com/files?M18xOTI1MDcyX00yVjY5" target="_blank"><b>Activity2Service</b></a></span></span><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-38998412660706565642012-04-07T00:41:00.000-07:002012-07-17T05:50:51.907-07:00Start Activity from Activity<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Some time we need to start an Activity from Activity..<br />
how can we achieve this i am going to write step by step.<br />
<br />
We will create very simple app to do this work<br />
-------------------------------------------<br />
App Name: <b>Activity2Activity</b><br />
Package Name: <b>com.rdc</b><br />
Android SDK: <b>Android SDK 2.3.3 / API 10</b><br />
Default Activity Name: <b>FirstActivity</b><br />
-------------------------------------------<br />
<br />
<br />
<b>FirstActivity.java</b><br />
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btnfirst);
btnload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
}
</pre>
<br />
<b>SecondActivity.java</b><br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button btnload = (Button) findViewById(R.id.btnsecond);
btnload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
}
}
</pre>
<br />
<b>first.xml</b><br />
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:text="I am first activity.."
android:gravity="center"
android:layout_height="62dp" />
<LinearLayout
android:id="@+id/layV"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Load Second Activity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnfirst"></Button>
</LinearLayout>
</LinearLayout>
</pre>
<br />
<b>second.xml</b><br />
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:text="I am Second activity.."
android:gravity="center"
android:layout_height="62dp" />
<LinearLayout
android:id="@+id/layV"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Load First Activity"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/btnsecond"></Button>
</LinearLayout>
</LinearLayout>
</pre>
<br />
<b>AndroidManifest.xml</b><br />
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".FirstActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre>
<br />
The output Screen will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-NPeFWPnSLhw/T_aWohWpXLI/AAAAAAAAAi8/LgTZi3vO6co/s1600/activity2activity.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://2.bp.blogspot.com/-NPeFWPnSLhw/T_aWohWpXLI/AAAAAAAAAi8/LgTZi3vO6co/s320/activity2activity.jpg" width="220" /></a></div>
<br />
<br />
You can download the complete source code zip file here : <span style="background-color: white;"><u><a href="https://www.opendrive.com/files?M18xOTMxMDcyX0lxa1Fn" target="_blank"><b><span style="color: #3d85c6;">Activity2Activity</span></b></a></u></span><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-56365032196888441212012-04-01T09:56:00.000-07:002012-07-17T05:55:36.345-07:00Start BroadcastReceiver from Activity<div dir="ltr" style="text-align: left;" trbidi="on">
Some time we need to start a service from Android Activity..<br />
how can we achieve this i am going to write step by step.<br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">We will create very simple app to do this work</span>
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>Activity2BReceiver</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>MyActivity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span>
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></span><br />
<b>MyActivity.java</b><br />
<pre class="java" name="code">
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("Debug", "Activity has been started..");
}
}
</pre>
<br />
Then Create a Broadcast Receiver<br />
<br />
<b>MyReceiver.java
</b><br />
<pre class="java" name="code">
package com.rdc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("Debug", "SMS Broadcast Receiver has been started..");
Toast.makeText(context, "BReceiver is watching ur message..",
Toast.LENGTH_SHORT).show();
}
}
</pre>
<br/>main.xml<br/>
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
</pre>
<br />
Don't forget to make entry in "Manifest" file for Receiver<br />
<pre class="xml" name="code">
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.rdc.MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
</pre>
<br />
Run this app and send message from another mobile/emulator,
Receiver will notify you about new sms<br />
See below output screens tensted on Emulator and Real Device<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-SkyF6Hm8_tU/T_WA7ofsZzI/AAAAAAAAAig/2coRvKDefWU/s1600/activity2BR.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="182" src="http://1.bp.blogspot.com/-SkyF6Hm8_tU/T_WA7ofsZzI/AAAAAAAAAig/2coRvKDefWU/s400/activity2BR.jpg" width="400" /></a></div>
<br />
<br />
<span style="color: #6aa84f; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">You can download the complete source code zip file here :</span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="background-color: white; font-size: 15px; line-height: 20px;"><span style="color: #3d85c6; font-family: 'Times New Roman', Times, FreeSerif, serif;"><b><u><a href="https://www.opendrive.com/files?M18xOTI1MDUyXzNobGpk" target="_blank">Activity2BReceiver</a></u></b></span></span><br />
<span style="background-color: white; color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><span style="color: #3d85c6;"><b><br /></b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">I'd love to hear your thoughts!</span>
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-64051887469903961872012-03-31T08:39:00.000-07:002012-05-18T08:39:32.589-07:00How to Get Own Mobile Number Programmatically<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
sometime you have multiple SIM cards and you don't know which one Number you are using.Then you may be want to "Get Own Mobile Number" ?<br />
How I am going to write a small android application which will solve our puzzle.<br />
<br />
So here we go.. we need to create a Basic Android App having an Activity like this<br />
<b>GetMyPhoneNoActivity.java</b><br />
<pre class="java" name="code">
package com.kns;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class GetMyPhoneNoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String number =getMyPhoneNO();
Toast.makeText(getApplicationContext(), "My Phone No is: "
+number, Toast.LENGTH_SHORT).show();
Log.v("Debug", number);
}
private String getMyPhoneNO(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService
(Context.TELEPHONY_SERVICE);
String yourNumber = mTelephonyMgr.getLine1Number();
return yourNumber;
}
}</pre>
<br />
Then we need to add user permission into manifest file <br />
<pre class="java" name="code">
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission></pre>
so it will be look like this..<br />
<b>AndroidManifest.xml</b> <br />
<pre class="java" name="code">
<manifest android:versioncode="1" android:versionname="1.0" package="com.kns" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="8">
<uses-permission android:name="android.permission.READ_PHONE_STATE">
<application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".GetMyPhoneNoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-permission></uses-sdk></manifest>
</pre>
</div>
<br />
Now if will check this code on Emulator device, The output will be..<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-XTJ519U7qLE/T7Zr58w0IaI/AAAAAAAAAac/eNUsSJOwcCY/s1600/ownno.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="283" src="http://2.bp.blogspot.com/-XTJ519U7qLE/T7Zr58w0IaI/AAAAAAAAAac/eNUsSJOwcCY/s400/ownno.jpg" width="400" /></a></div>
<br />
Note: As i have read, so far some people have conflict about different behavior of output.<br />
<span class="comment-copy">there are reports that some SIMs cause this method to return <code>null.</code></span><br />
<br />
Because --? I think there are cases where the phone does not know its own number - it somehow depends on the network provider / SIM card.<br />
<br />
So, There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (e.g. this is how porting is supported).<br />
<br />
I'd like to know your suggestions!!<br />
<br />
Thanks! </div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com3tag:blogger.com,1999:blog-2971558731672404737.post-90138860798641210192012-03-02T08:43:00.000-08:002012-07-02T03:39:59.106-07:00Lock Phone Screen Programmtically<div dir="ltr" style="text-align: left;" trbidi="on">
How to lock Mobile Phone Screen ...<br />
first of all we need to get permission from Admin<br />
create app with default activity<br />
<br />
Project Name : LockScreen<br />
Package Name: com.kns<br />
Android SDK: 2.2 API 8<br />
<br />
the project structure is look like this<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-QUNtR5YAF8M/T_F3_rA-g5I/AAAAAAAAAgc/23ppAt3qFHk/s1600/ls-ps.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-QUNtR5YAF8M/T_F3_rA-g5I/AAAAAAAAAgc/23ppAt3qFHk/s320/ls-ps.jpg" width="212" /></a></div>
<br />
<br />
LockScreenActivity.java<br />
<pre class="java" name="code">
package com.kns;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LockScreenActivity extends Activity implements OnClickListener {
private Button lock;
private Button disable;
private Button enable;
static final int RESULT_ENABLE = 1;
DevicePolicyManager deviceManger;
ActivityManager activityManager;
ComponentName compName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
deviceManger = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager)getSystemService(
Context.ACTIVITY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);
setContentView(R.layout.main);
lock =(Button)findViewById(R.id.lock);
lock.setOnClickListener(this);
disable = (Button)findViewById(R.id.btnDisable);
enable =(Button)findViewById(R.id.btnEnable);
disable.setOnClickListener(this);
enable.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == lock){
boolean active = deviceManger.isAdminActive(compName);
if (active) {
deviceManger.lockNow();
}
}
if(v == enable){
Intent intent = new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);
}
if(v == disable){
deviceManger.removeActiveAdmin(compName);
updateButtonStates();
}
}
private void updateButtonStates() {
boolean active = deviceManger.isAdminActive(compName);
if (active) {
enable.setEnabled(false);
disable.setEnabled(true);
} else {
enable.setEnabled(true);
disable.setEnabled(false);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Admin enabled!");
} else {
Log.i("DeviceAdminSample", "Admin enable FAILED!");
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
</pre>
<br />
MyAdmin.java<br />
<pre class="java" name="code">
package com.kns;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;
public class MyAdmin extends DeviceAdminReceiver{
static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(
DeviceAdminReceiver.class.getName(), 0);
}
static String PREF_PASSWORD_QUALITY = "password_quality";
static String PREF_PASSWORD_LENGTH = "password_length";
static String PREF_MAX_FAILED_PW = "max_failed_pw";
void showToast(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onEnabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: enabled");
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "This is an optional message to warn the user about disabling.";
}
@Override
public void onDisabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: disabled");
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw changed");
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw failed");
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw succeeded");
}
}
</pre>
<br />
main.xml<br />
<pre class="java" name="code">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="To Lock Screen First Enable it.."
android:textSize="15sp"></TextView>
<Button
android:id="@+id/lock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lock The Phone" ></Button>
<Button
android:id="@+id/btnEnable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable" ></Button>
<Button
android:id="@+id/btnDisable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable" ></Button>
</LinearLayout>
</pre>
<br />
policies.xml<br />
<pre class="java" name="code">
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
</pre>
<br />
AndroidManifest.xml<br />
<pre class="java" name="code">
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kns"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LockScreenActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@layout/policies" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
</pre>
<br />
Before Lock the screen you need to Enable Admin Permission<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-W2dHkW9v8mM/T7piSAJqfyI/AAAAAAAAAbg/iiiPCOnCjpU/s1600/ls1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="218" src="http://3.bp.blogspot.com/-W2dHkW9v8mM/T7piSAJqfyI/AAAAAAAAAbg/iiiPCOnCjpU/s320/ls1.jpg" width="320" /></a></div>
<br />
Then you will get this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-89TEQj19Z-0/T7piZE6naCI/AAAAAAAAAbo/eL0XLOwO6HE/s1600/ls2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://2.bp.blogspot.com/-89TEQj19Z-0/T7piZE6naCI/AAAAAAAAAbo/eL0XLOwO6HE/s320/ls2.jpg" width="215" /></a></div>
After Enable you will lock screen..like this<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-K55qmCyXP_M/T7pijub1yuI/AAAAAAAAAbw/PHwTysxlZvI/s1600/sl3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="226" src="http://2.bp.blogspot.com/-K55qmCyXP_M/T7pijub1yuI/AAAAAAAAAbw/PHwTysxlZvI/s320/sl3.jpg" width="320" /></a></div>
<br />
we have done it!!<br />
<br />
<span style="font-family: Georgia, Utopia, 'Palatino Linotype', Palatino, serif; font-size: 15px; line-height: 20px;">I'd love to hear your thoughts!!</span>
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com21Bengaluru, India12.9715987 77.594562712.724026199999999 77.2787057 13.2191712 77.910419699999991tag:blogger.com,1999:blog-2971558731672404737.post-45436519734209368482012-02-28T09:07:00.000-08:002012-07-17T05:45:07.093-07:00Start Service from Broadcast Receiver<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Some time we need to start Service from Broadcast Receiver..</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">how can we achieve this i am going to write step by step.</span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">So lets create a small App to do this things </span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>BReceiver2Service</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.2 / API 8</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">MyReceiver.java</span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span>
<br />
<pre class="java" name="code">package com.rdc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyReceiver Started",
Toast.LENGTH_SHORT).show();
Log.v("Info Message", "in Broadcast receiver");
Intent myIntent=new Intent(context,MyService.class);
context.startService(myIntent);
}
}
</pre>
<br />
MyService
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(getBaseContext(), "Service Started",
Toast.LENGTH_SHORT).show();
/* We want this service to continue running until it is explicitly
stopped, so return sticky. */
return START_STICKY;
}
}
</pre>
<br />
main.xml
<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
</pre>
<br />
AndroidManifest.xml
<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<service
android:enabled="true"
android:name=".MyService">
<intent-filter>
<action android:name="com.rdc.MyService">
</action>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
</pre>
<br />
Now Reboot Emulator, Toast will appear.<br />
<br />
you can download zip file from here : <a href="https://www.opendrive.com/files?M18yMDA3MjQxX0lTbjd0" target="_blank"><b><span style="color: #3d85c6;">BReceiver2Service</span></b></a><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!!</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com2Bengaluru, India12.9715987 77.594562712.724026199999999 77.2787057 13.2191712 77.910419699999991tag:blogger.com,1999:blog-2971558731672404737.post-20492996308974415092012-02-14T06:43:00.000-08:002012-07-04T06:46:42.624-07:00Uninstall App programmatically in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Today we will learn How to an Android Application can Uninstall itself <span style="background-color: white;">Programmatically</span><br />
<span style="background-color: white;"><br /></span><br />
Note: here we need to pass package name in URI<br />
so we are passing package name in which main/default/startup Activity Present<br />
that we also define in manifest file at top level<br />
<br />
Okay let's try this simple app<br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>UninstallApp</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.3.3 / API 10</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Default Activity Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>UninstallAppActivity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span>
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;">UninstallAppActivity.java</span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><br /></span></span>
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class UninstallAppActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri packageUri = Uri.parse("package:com.rdc");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageUri);
startActivity(uninstallIntent);
}
});
}
}
</pre>
<br />
main.xml
<br />
<pre class="xml" name="code"><linearlayout android:gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Uninstall App"></button>
</linearlayout>
</pre>
<br />
AndroidManifest.xml<br />
<pre class="xml" name="code"><manifest android:versioncode="1" android:versionname="1.0" package="com.rdc" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minsdkversion="10">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".UninstallAppActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
</application>
</uses-sdk></manifest>
</pre>
<br />
The output Screen will be like this..<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-H7nztZu8-hY/T_RG8Q_iOCI/AAAAAAAAAiU/-MhqTJEY7hU/s1600/uninstall.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://4.bp.blogspot.com/-H7nztZu8-hY/T_RG8Q_iOCI/AAAAAAAAAiU/-MhqTJEY7hU/s320/uninstall.jpg" width="214" /></a></div>
<span style="color: #6aa84f; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"><br /></span><br />
<span style="color: #6aa84f; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">You can download the complete source code zip file here :</span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;"> </span><span style="background-color: white; font-size: 15px; line-height: 20px;"><span style="color: #3d85c6; font-family: 'Times New Roman', Times, FreeSerif, serif;"><b><u><a href="https://www.opendrive.com/files?M18xOTIzMTMzX1F4NzRn" target="_blank">UninstallApp</a></u></b></span></span><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com0tag:blogger.com,1999:blog-2971558731672404737.post-47580265775243935512012-02-06T09:17:00.000-08:002012-07-17T05:23:24.278-07:00Start Activity from Broadcast Recevier<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: #363636; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif;"><span style="font-size: 15px; line-height: 20px;"></span></span><br />
Some time we need to start an Activity from Broadcast Receiver..<br />
how can we achieve this i am going to write step by step.<br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">So lets create a small App to do this things </span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">App Name: </span><span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;"><b>BReceiver2Activity</b></span></span><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Package Name: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">com.rdc</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK: </span><b style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">Android SDK 2.2 / API 8</b><br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif; font-size: 15px; line-height: 20px;">-------------------------------------------</span><br />
<br />
<span style="color: #363636; font-family: 'Times New Roman', Times, FreeSerif, serif;"><span style="font-size: 15px; line-height: 20px;">MyReceiver.java</span></span>
<br />
<pre class="java" name="code">package com.rdc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "MyReceiver Started",
Toast.LENGTH_SHORT).show();
Log.v("Info Message", "in Broadcast receiver");
Intent myIntent=new Intent(context,MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
</pre>
<br />
MyActivity.java
<br />
<pre class="java" name="code">package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
</pre>
<br />
main.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_weight="0.14" />
</LinearLayout>
</pre>
<br />
AndroidManifest.xml<br />
<pre class="xml" name="code"><?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:enabled="true"
android:name=".MyActivity">
<intent-filter>
<action android:name="com.rdc.MyActivity">
</action>
</intent-filter>
</activity>
<receiver
android:enabled="true"
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
</pre>
<br />
Now Reboot Emulator, Activity will appear on start-up.<br />
<br />
You can download source code here: <a href="https://www.opendrive.com/files?M18yMDA3MTUyX1pqenFs" target="_blank"><b><span style="color: #3d85c6;">BReceiver2Activity</span></b></a><br />
<br />
cheers!!<br />
<br />
I'd love to hear your thoughts!!
</div>RDChttp://www.blogger.com/profile/09071971836590859058noreply@blogger.com3Bangalore, India12.9715987 77.594562712.724026199999999 77.2787057 13.2191712 77.910419699999991