<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4976004055150276620</id><updated>2026-05-29T01:10:11.712-07:00</updated><category term="Android"/><category term="Java"/><category term="Android Development Problems"/><category term="BroadcastReceiver"/><category term="iOS"/><category term="Android Studio"/><category term="Cloud Computing"/><category term="Google Cloud Endpoints"/><category term="Objective C"/><category term="C++"/><category term="Code Snippets"/><category term="NSString"/><category term="sendBroadcast"/><category term="APIs"/><category term="AVD Error"/><category term="Android Chart Library"/><category term="Android Java Chart Library"/><category term="Android Virtual Device (AVD)"/><category term="Boost C++"/><category term="Design Patterns"/><category term="Docker"/><category term="Docker CLI"/><category term="HMAC"/><category term="HelloCharts-Android"/><category term="Inspiration"/><category term="LocalBroadcastManager"/><category term="MBaaS"/><category term="Mobile Backend as a service"/><category term="Motivation"/><category term="NSData"/><category term="NSDate"/><category term="NSDateFormatter"/><category term="Quotes"/><category term="Security"/><category term="Singleton Design Pattern"/><category term="Swift Language"/><category term="Ubuntu"/><category term="Web Services"/><category term="iOS Adaptive Layout"/><category term="iOS Auto Layout"/><category term="iOS Universal Storyboards"/><category term="iOS7"/><title type='text'>Nipun&#39;s Writings</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Nipun De Silva</name><uri>http://www.blogger.com/profile/01584925573563517469</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>21</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-5600357987425482516</id><published>2026-04-21T11:45:00.000-07:00</published><updated>2026-04-21T11:45:16.672-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Docker"/><category scheme="http://www.blogger.com/atom/ns#" term="Docker CLI"/><category scheme="http://www.blogger.com/atom/ns#" term="Ubuntu"/><title type='text'>Installing Docker CLI on Ubuntu (Step-by-Step Guide)</title><content type='html'>&lt;p&gt;If you&#39;re working with containers, &lt;b&gt;Docker&lt;/b&gt; is one of the most essential tools you’ll need. In this guide, I’ll walk you through installing the Docker CLI (along with the Docker Engine) on an Ubuntu system in a simple and practical way.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Prerequisites&lt;/h2&gt;&lt;p&gt;Before you start, make sure:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;You’re using an Ubuntu system (18.04 or later recommended)&lt;/li&gt;&lt;li&gt;You have a user account with sudo privileges&lt;/li&gt;&lt;li&gt;Your system is connected to the internet&lt;/li&gt;&lt;/ul&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Steps&lt;/h2&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 1: Update Package Index&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;697&quot; data-start=&quot;670&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;668&quot; data-start=&quot;623&quot;&gt;Start by updating your system’s package list:&lt;/p&gt;&lt;p&gt;sudo apt update&lt;/p&gt;&lt;p&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;830&quot; data-start=&quot;731&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;729&quot; data-start=&quot;699&quot;&gt;Install required dependencies:&lt;br /&gt;&lt;/p&gt;&lt;p data-end=&quot;729&quot; data-start=&quot;699&quot;&gt;sudo apt install -y apt-transport-https ca-certificates curl software-properties-common&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 2: Add Docker’s Official GPG Key&lt;/h3&gt;&lt;p data-end=&quot;729&quot; data-start=&quot;699&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;1069&quot; data-start=&quot;931&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;929&quot; data-start=&quot;882&quot;&gt;This ensures packages are verified and trusted:&lt;/p&gt;&lt;p data-end=&quot;929&quot; data-start=&quot;882&quot;&gt;curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 3: Add Docker Repository&lt;/h3&gt;&lt;p data-end=&quot;929&quot; data-start=&quot;882&quot;&gt;echo &quot;deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot; | sudo tee /etc/apt/sources.list.d/docker.list &amp;gt; /dev/null&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 4: Install Docker Engine &amp;amp; CLI&lt;/h3&gt;&lt;div&gt;&lt;p data-end=&quot;1482&quot; data-start=&quot;1456&quot;&gt;Update package list again:&lt;/p&gt;&lt;p data-end=&quot;1482&quot; data-start=&quot;1456&quot;&gt;sudo apt update&lt;/p&gt;&lt;p data-end=&quot;1482&quot; data-start=&quot;1456&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;1603&quot; data-start=&quot;1534&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;1532&quot; data-start=&quot;1513&quot;&gt;Now install Docker:&lt;/p&gt;&lt;p data-end=&quot;1532&quot; data-start=&quot;1513&quot;&gt;sudo apt install -y docker-ce docker-ce-cli containerd.io&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 5: Start and Enable Docker&lt;/h3&gt;&lt;p data-end=&quot;1532&quot; data-start=&quot;1513&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;1711&quot; data-start=&quot;1672&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;1670&quot; data-start=&quot;1649&quot;&gt;Start Docker service:&lt;/p&gt;&lt;p data-end=&quot;1670&quot; data-start=&quot;1649&quot;&gt;sudo systemctl start docker&lt;/p&gt;&lt;p data-end=&quot;1670&quot; data-start=&quot;1649&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;1786&quot; data-start=&quot;1746&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;1744&quot; data-start=&quot;1713&quot;&gt;Enable Docker to start on boot:&lt;/p&gt;&lt;p data-end=&quot;1744&quot; data-start=&quot;1713&quot;&gt;sudo systemctl enable docker&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 6: Run Docker Without sudo (Optional but Recommended)&lt;/h3&gt;&lt;p data-end=&quot;1744&quot; data-start=&quot;1713&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;1987&quot; data-start=&quot;1946&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;1944&quot; data-start=&quot;1859&quot;&gt;By default, Docker requires &lt;code data-end=&quot;1893&quot; data-start=&quot;1887&quot;&gt;sudo&lt;/code&gt;. To avoid that, add your user to the Docker group:&lt;/p&gt;&lt;p data-end=&quot;1944&quot; data-start=&quot;1859&quot;&gt;sudo usermod -aG docker $USER&lt;/p&gt;&lt;p data-end=&quot;1944&quot; data-start=&quot;1859&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;2046&quot; data-start=&quot;2021&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;2019&quot; data-start=&quot;1989&quot;&gt;Apply the changes immediately:&lt;/p&gt;&lt;p data-end=&quot;2019&quot; data-start=&quot;1989&quot;&gt;newgrp docker&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Step 7: Verify Installation&lt;/h3&gt;&lt;div&gt;&lt;p data-end=&quot;2112&quot; data-start=&quot;2087&quot;&gt;Check Docker system info:&lt;/p&gt;&lt;p data-end=&quot;2112&quot; data-start=&quot;2087&quot;&gt;docker info&lt;/p&gt;&lt;p data-end=&quot;2112&quot; data-start=&quot;2087&quot;&gt;
&lt;/p&gt;&lt;pre class=&quot;overflow-visible! px-0!&quot; data-end=&quot;2186&quot; data-start=&quot;2165&quot;&gt;&lt;div class=&quot;relative w-full mt-4 mb-1&quot;&gt;&lt;div class=&quot;&quot;&gt;&lt;div class=&quot;relative&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;h-full min-h-0 min-w-0&quot;&gt;&lt;div class=&quot;border border-token-border-light border-radius-3xl corner-superellipse/1.1 rounded-3xl&quot;&gt;&lt;div class=&quot;h-full w-full border-radius-3xl bg-token-bg-elevated-secondary corner-superellipse/1.1 overflow-clip rounded-3xl lxnfua_clipPathFallback&quot;&gt;&lt;div class=&quot;pointer-events-none absolute inset-x-4 top-12 bottom-4&quot;&gt;&lt;div class=&quot;pointer-events-none sticky z-40 shrink-0 z-1!&quot;&gt;&lt;div class=&quot;sticky bg-token-border-light&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p data-end=&quot;2163&quot; data-start=&quot;2139&quot;&gt;List running containers:&lt;/p&gt;&lt;p data-end=&quot;2163&quot; data-start=&quot;2139&quot;&gt;docker ps&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Quick Note&lt;/h2&gt;&lt;p style=&quot;text-align: left;&quot;&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;docker-ce → Docker Engine&lt;/li&gt;&lt;li&gt;docker-ce-cli → Docker CLI&lt;/li&gt;&lt;li&gt;containerd.io → Container runtime&lt;/li&gt;&lt;/ul&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusion&lt;/h2&gt;&lt;p&gt;&lt;/p&gt;&lt;div&gt;You now have Docker CLI installed and ready to use on Ubuntu. This setup is ideal for development, testing, and learning containerization.&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;From here, you can start running containers, pulling images, or even building your own.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/5600357987425482516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2026/04/installing-docker-cli-on-ubuntu-step-by.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5600357987425482516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5600357987425482516'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2026/04/installing-docker-cli-on-ubuntu-step-by.html' title='Installing Docker CLI on Ubuntu (Step-by-Step Guide)'/><author><name>Nipun De Silva</name><uri>http://www.blogger.com/profile/01584925573563517469</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-1595329175960407127</id><published>2017-12-09T21:40:00.000-08:00</published><updated>2017-12-09T21:40:23.105-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Inspiration"/><category scheme="http://www.blogger.com/atom/ns#" term="Motivation"/><category scheme="http://www.blogger.com/atom/ns#" term="Quotes"/><title type='text'></title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYfSbu09N47oX0Ph-4z7t6mRcJx-C-q2KQ5MAwTpbpE19tgTzYrJ8z3f3DbUgjSyXbbxOoORdU3X95I14JWmyS9rZVYNcri3Sov8CiHCKcxqrQ5iJw5DZG50_oooKMkIt_Xg2sWpXWf7nf/s1600/quotes-The-man-who-will-use_transparent3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1600&quot; data-original-width=&quot;1600&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYfSbu09N47oX0Ph-4z7t6mRcJx-C-q2KQ5MAwTpbpE19tgTzYrJ8z3f3DbUgjSyXbbxOoORdU3X95I14JWmyS9rZVYNcri3Sov8CiHCKcxqrQ5iJw5DZG50_oooKMkIt_Xg2sWpXWf7nf/s640/quotes-The-man-who-will-use_transparent3.png&quot; width=&quot;640&quot; /&gt;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;i&gt;&lt;b&gt;An inspirational quote by &lt;a href=&quot;https://en.wikipedia.org/wiki/Henry_Ford&quot;&gt;Henry Ford&lt;/a&gt;&lt;/b&gt;&lt;/i&gt;. &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/1595329175960407127/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2017/12/an-inspirational-quote-by-henry-ford.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1595329175960407127'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1595329175960407127'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2017/12/an-inspirational-quote-by-henry-ford.html' title=''/><author><name>Nipun De Silva</name><uri>http://www.blogger.com/profile/01584925573563517469</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYfSbu09N47oX0Ph-4z7t6mRcJx-C-q2KQ5MAwTpbpE19tgTzYrJ8z3f3DbUgjSyXbbxOoORdU3X95I14JWmyS9rZVYNcri3Sov8CiHCKcxqrQ5iJw5DZG50_oooKMkIt_Xg2sWpXWf7nf/s72-c/quotes-The-man-who-will-use_transparent3.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-8461859912522737419</id><published>2017-02-18T20:31:00.001-08:00</published><updated>2017-02-18T20:31:49.407-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><category scheme="http://www.blogger.com/atom/ns#" term="Design Patterns"/><category scheme="http://www.blogger.com/atom/ns#" term="Singleton Design Pattern"/><title type='text'>Singleton Design Pattern In C++</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
Singleton Design Pattern is used to keep one instance of a class in the system. To implement this pattern in C++, language features like access modifiers (public, protected, private), class functions and variables by using static, pointer and dynamic memory is used.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
To keep single instance of particular class, developers must be prevented from creating new objects. In order to achieve that constructors of that particular class must be private. Doing so, new object of that class cannot be created. Object creating is facilitated well control manner to keep one instance of that class in the system by a public class method. The created single instance also is kept in the class variable. So when program request an instance, if that variable has the instance that instance is returned. Otherwise new instance is created and returned. All subsequent calls to object creation method returns the single instance that is kept in the class variable.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Below implementation use &lt;b&gt;&lt;i&gt;static ThreadPool *getInstance()&lt;/i&gt;&lt;/b&gt; method to create or get single instance.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;Implementation&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
#include &amp;lt;iostream&amp;gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
using std::cout;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
using std::endl;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
class ThreadPool {&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;private:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; static ThreadPool *tPSingleton;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; ThreadPool ();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;public:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; static ThreadPool *getInstance();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; bool startThreadPool();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; bool stopThreadPool();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; ~ThreadPool ();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
};&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
ThreadPool* ThreadPool::tPSingleton = NULL;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
ThreadPool::ThreadPool (){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;Thread Pool is created&quot;&amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
ThreadPool::~ThreadPool (){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;Thread Pool is destroyed&quot;&amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
ThreadPool *ThreadPool::getInstance(){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;if(ThreadPool::tPSingleton == NULL){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; ThreadPool::tPSingleton = new ThreadPool;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;return ThreadPool::tPSingleton;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
bool ThreadPool::startThreadPool(){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;Thread pool is being started&quot;&amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
bool ThreadPool::stopThreadPool(){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;Thread pool is being stoped&quot;&amp;lt;&amp;lt;endl;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;return true;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
int main() {&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;ThreadPool *threadPool1;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;ThreadPool *threadPool2;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;/*&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;threadPool1 = new ThreadPool;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Produce below error with Error No 1&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;*/&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;threadPool1 = ThreadPool::getInstance();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;threadPool1-&amp;gt;startThreadPool();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;threadPool1-&amp;gt;stopThreadPool();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;threadPool2 = ThreadPool::getInstance();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;threadPool2-&amp;gt;startThreadPool();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;threadPool2-&amp;gt;stopThreadPool();&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp;return 0;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
}&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
/*&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Error No 1&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
singleton.cpp: In function ‘int main()’:&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
singleton.cpp:23:1: error: ‘ThreadPool::ThreadPool()’ is private&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;ThreadPool::ThreadPool (){&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp;^&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
singleton.cpp:52:22: error: within this context&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; threadPool1 = new ThreadPool; &amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ^&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
*/&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;i&gt;Output&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thread Pool is created&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thread pool is being started&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thread pool is being stoped&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thread pool is being started&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Thread pool is being stoped&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/8461859912522737419/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2017/02/singleton-design-pattern-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8461859912522737419'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8461859912522737419'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2017/02/singleton-design-pattern-in-c.html' title='Singleton Design Pattern In C++'/><author><name>Nipun De Silva</name><uri>http://www.blogger.com/profile/01584925573563517469</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-2791642870925795635</id><published>2016-10-30T02:58:00.003-07:00</published><updated>2016-10-30T02:58:59.424-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="BroadcastReceiver"/><category scheme="http://www.blogger.com/atom/ns#" term="sendBroadcast"/><title type='text'>Android  App To Understand Sticky Broadcasts</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
This blog post is about an application that can be used to understand what is the difference between sticky broadcasts and ordinary broadcasts of Android system. App name is &lt;i&gt;&lt;b&gt;StickyBCast&lt;/b&gt;&lt;/i&gt;. App has below package name.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;i&gt;&lt;b&gt; com.blogspot.nipunswritings.stickybcast&lt;/b&gt;&lt;/i&gt;.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
UI of the app is shown below.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_x42nQLb_inXLkeagZj5vjkmYeP5gINJNn3ZLzrHV1fYIiVLcvtQRV2cSioMyM6JgOYUR7f-FdHCdlglwg1UG4WIDD9Iy3HlrFdPP6ir9pfKGOQOYfaXbfaAyczFt1RZ1qP3vOuYuVL3e/s1600/Screenshot_2016-10-23-16-37-40.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_x42nQLb_inXLkeagZj5vjkmYeP5gINJNn3ZLzrHV1fYIiVLcvtQRV2cSioMyM6JgOYUR7f-FdHCdlglwg1UG4WIDD9Iy3HlrFdPP6ir9pfKGOQOYfaXbfaAyczFt1RZ1qP3vOuYuVL3e/s400/Screenshot_2016-10-23-16-37-40.png&quot; width=&quot;225&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
Above UI shows what sticky broadcast and ordinary broadcast sent. This app send broadcast at two events. One is when onStop() is called and other one is when user touch on send button. Content that is sent with sticky broadcast is displayed below &quot;&lt;i&gt;&lt;b&gt;Sticky Broadcast Content&lt;/b&gt;&lt;/i&gt;&quot; and Content that is sent with ordinary broadcast is displayed below &quot;&lt;i&gt;&lt;b&gt;Ordinary Broadcast Content&lt;/b&gt;&lt;/i&gt;&quot;. The content that is sent with broadcast are shown in TextViews.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
To show content that is sent with sticky broadcast TextView with Id &quot;sticky_content_tv&quot; is used. TextView with id ordinary_content_tv is used to show the content that is sent with normal broadcast.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
Class (MainActivity) below is used to show difference between two types of broadcasts.&amp;nbsp; &lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;MainActivity &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;AppCompatActivity {

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;TextView &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentTv&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;TextView &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentTv&lt;/span&gt;;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContent&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContent&lt;/span&gt;;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;IntentFilter &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentFilter&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;IntentFilter &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentFilter&lt;/span&gt;;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;BroadcastReceiver &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oReceiver &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;BroadcastReceiver() {
        &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
            &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContent &lt;/span&gt;= intent.getStringExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_EXTRA&lt;/span&gt;);
            setTextViews();
        }
    };

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;BroadcastReceiver &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sReceiver &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;BroadcastReceiver() {
        &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
            &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContent &lt;/span&gt;= intent.getStringExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_EXTRA&lt;/span&gt;);
            setTextViews();
        }
    };


    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onCreate(Bundle savedInstanceState) {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onCreate(savedInstanceState);
        setContentView(R.layout.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;activity_main&lt;/span&gt;);

        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentTv &lt;/span&gt;= (TextView) findViewById(R.id.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;ordinary_content_tv&lt;/span&gt;);
        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentTv &lt;/span&gt;= (TextView) findViewById(R.id.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;sticky_content_tv&lt;/span&gt;);

        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentFilter &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;IntentFilter(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_ACTION&lt;/span&gt;);
        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentFilter &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;IntentFilter(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_ACTION&lt;/span&gt;);
    }

    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onStart() {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onStart();

        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContent &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&quot;&lt;/span&gt;;
        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContent &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&quot;&lt;/span&gt;;

        Intent oIntent = registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oReceiver&lt;/span&gt;, &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentFilter&lt;/span&gt;);
        Intent sIntent = registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sReceiver&lt;/span&gt;, &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentFilter&lt;/span&gt;);

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(oIntent != &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {
            &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContent &lt;/span&gt;= oIntent.getStringExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_EXTRA&lt;/span&gt;);
        }

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(sIntent != &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {
            &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContent &lt;/span&gt;= sIntent.getStringExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_EXTRA&lt;/span&gt;);
        }

        setTextViews();
    }


    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onStop() {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onStop();
        sendBroadcasts(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Ordinay Value 2&quot;&lt;/span&gt;, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Sticky Value 2&quot;&lt;/span&gt;);
    }

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private void &lt;/span&gt;setTextViews() {
        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentTv&lt;/span&gt;.setText(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContent&lt;/span&gt;);
        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentTv&lt;/span&gt;.setText(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContent&lt;/span&gt;);
    }

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendBroadcasts(String oExtrVal, String sExtrVal) {
        Intent oIntent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_ACTION&lt;/span&gt;);
        Intent sIntent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_ACTION&lt;/span&gt;);

        oIntent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_EXTRA&lt;/span&gt;, oExtrVal);
        sIntent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_EXTRA&lt;/span&gt;, sExtrVal);


        sendBroadcast(oIntent);
        sendStickyBroadcast(sIntent);
    }

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendBCastWithClick(View view) {
        sendBroadcasts(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Ordinay Value 1&quot;&lt;/span&gt;, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Sticky Value 1&quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
Some of the constants that are needed for this application has been defined in a class called Constants. This is the definition of that class.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;Constants {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_ACTION &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.s_action&quot;&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_ACTION &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.o_action&quot;&lt;/span&gt;;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_EXTRA &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.s_extra&quot;&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_EXTRA &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.o_extra&quot;&lt;/span&gt;;
}&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
In&amp;nbsp;&lt;span style=&quot;background-color: white; font-family: &amp;quot;courier new&amp;quot;; font-size: 9pt;&quot;&gt;MainActivity&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: navy; font-family: &amp;quot;courier new&amp;quot;; font-size: 9pt; font-weight: bold;&quot;&gt;public void &lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: &amp;quot;courier new&amp;quot;; font-size: 9pt;&quot;&gt;sendBroadcasts(String oExtrVal, String sExtrVal)&lt;/span&gt;&amp;nbsp;method is called at two event. One event is in the onStop() method of the activity and other one is in the button click listener of send button with the method&amp;nbsp;&lt;span style=&quot;color: navy; font-family: &amp;quot;courier new&amp;quot;; font-size: 9pt; font-weight: bold;&quot;&gt;public void &lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: &amp;quot;courier new&amp;quot;; font-size: 9pt;&quot;&gt;sendBCastWithClick(View view)&lt;/span&gt;. The arguments that are passed to method&amp;nbsp;&lt;span style=&quot;background-color: white; font-family: &amp;quot;courier new&amp;quot;; font-size: 12px;&quot;&gt;sendBroadcasts(String, String)&lt;/span&gt;&amp;nbsp;are different in two events.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
Code segment that is useful to understand difference between sticky broadcasts and normal broadcasts is this.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; font-family: &#39;courier new&#39;; font-size: 9pt;&quot;&gt;        Intent oIntent = registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oReceiver&lt;/span&gt;, &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContentFilter&lt;/span&gt;);
        Intent sIntent = registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sReceiver&lt;/span&gt;, &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContentFilter&lt;/span&gt;);&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
here when registration is done if sticky broadcast has been sent earlier that is returned by the function registerReceiver(Intent) for sticky broadcasts. But for normal broadcasts it returns null. If you run the app and click send button both text boxes will display text values that have been sent by broadcasts. In that case there is no difference between those two types of broadcast. &amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
Then do something that cause to call onStop() and come back to app. The TextView for sticky broadcast text has been set with the &quot;&lt;b&gt;Sticky Value 2&lt;/b&gt;&quot; but other one is empty. Because system doesn&#39;t provide normal broadcast intents that was sent before registration. So for below code &lt;b&gt;oIntent &lt;/b&gt;&amp;nbsp;is null.&lt;/div&gt;
&lt;pre style=&quot;background-color: white; font-family: &#39;courier new&#39;; font-size: 9pt;&quot;&gt;        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(oIntent != &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {
            &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;oContent &lt;/span&gt;= oIntent.getStringExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_EXTRA&lt;/span&gt;);
        }

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(sIntent != &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {
            &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;sContent &lt;/span&gt;= sIntent.getStringExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_EXTRA&lt;/span&gt;);
        }&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
To send broadcasts below function is used. Method&amp;nbsp;&lt;span style=&quot;background-color: white; font-family: &#39;courier new&#39;; font-size: 9pt;&quot;&gt;sendStickyBroadcast(sIntent)&lt;/span&gt;&amp;nbsp;is deprecated even though that is used for this example.&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; font-family: &#39;courier new&#39;; font-size: 9pt;&quot;&gt;    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendBroadcasts(String oExtrVal, String sExtrVal) {
        Intent oIntent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_ACTION&lt;/span&gt;);
        Intent sIntent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_ACTION&lt;/span&gt;);

        oIntent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ORDINARY_EXTRA&lt;/span&gt;, oExtrVal);
        sIntent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;STICKY_EXTRA&lt;/span&gt;, sExtrVal);


        sendBroadcast(oIntent);
        sendStickyBroadcast(sIntent);
    }&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;b&gt;Reference:&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/Context.html#sendStickyBroadcast(android.content.Intent)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/2791642870925795635/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/10/android-app-to-understand-sticky.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/2791642870925795635'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/2791642870925795635'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/10/android-app-to-understand-sticky.html' title='Android  App To Understand Sticky Broadcasts'/><author><name>Nipun De Silva</name><uri>http://www.blogger.com/profile/01584925573563517469</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_x42nQLb_inXLkeagZj5vjkmYeP5gINJNn3ZLzrHV1fYIiVLcvtQRV2cSioMyM6JgOYUR7f-FdHCdlglwg1UG4WIDD9Iy3HlrFdPP6ir9pfKGOQOYfaXbfaAyczFt1RZ1qP3vOuYuVL3e/s72-c/Screenshot_2016-10-23-16-37-40.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-6902799926094792906</id><published>2016-08-14T06:07:00.001-07:00</published><updated>2016-08-14T06:07:20.327-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="sendBroadcast"/><title type='text'>sendBroadcast Android Example</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
This is an example of using method&amp;nbsp;&lt;a href=&quot;https://developer.android.com/reference/android/content/Context.html#sendBroadcast(android.content.Intent)&quot;&gt;sendBroadcast()&lt;/a&gt; to send broadcasts in Android. App has one BroadcastReceiver. It display broadcast action if there is one else display nothing. It also display the addition of two numbers that were sent to it.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
BroadcastReceiver has been implemented with the class &lt;i&gt;&lt;b&gt;NumberReceiver&lt;/b&gt;&lt;/i&gt;. So it must be registered by using the manifest file.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;receiver&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:name=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;.NumberReceiver&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:enabled=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:exported=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;intent-filter&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;       &amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;action &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:name=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.demosendbcast.add&quot; &lt;/span&gt;/&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &amp;lt;/&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;intent-filter&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;lt;/&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;receiver&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
It can receive intents with the below action&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;com.blogspot.nipunswritings.demosendbcast.add&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
Project has class Constants that has been defined key for intent actions and extras.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;Constants {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;    public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ACTION_ADD &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.demosendbcast.add&quot;&lt;/span&gt;;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;    public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_X &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.demosendbcast.x&quot;&lt;/span&gt;;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;    public static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_Y &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;com.blogspot.nipunswritings.demosendbcast.y&quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;}&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the activity that invoke sendBroadcast() has Two EditTexts and a Button. When user click the button numbers in two EditTexts are send to BroadcastReceiver after converting them to integers. This is the button onClick method.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendToAddition (View view) {

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;boolean &lt;/span&gt;withAction = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;false&lt;/span&gt;;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;x = Integer.&lt;span style=&quot;font-style: italic;&quot;&gt;parseInt&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;xEt&lt;/span&gt;.getText().toString());
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;y = Integer.&lt;span style=&quot;font-style: italic;&quot;&gt;parseInt&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;yEt&lt;/span&gt;.getText().toString());

    Intent intent;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(withAction) {
        intent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ACTION_ADD&lt;/span&gt;);
    } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;else &lt;/span&gt;{
        intent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;, NumberReceiver.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;class&lt;/span&gt;);
    }
    intent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_X&lt;/span&gt;, x);
    intent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_Y&lt;/span&gt;, y);

    sendBroadcast(intent);
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Important code snippet of above method is this.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    Intent intent;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(withAction) {
        intent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ACTION_ADD&lt;/span&gt;);
    } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;else &lt;/span&gt;{
        intent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;, NumberReceiver.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;class&lt;/span&gt;);
    }
    intent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_X&lt;/span&gt;, x);
    intent.putExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_Y&lt;/span&gt;, y);&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    sendBroadcast(intent); &lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If Boolean variable withAction is true Intent instance is created with the ACTION_ADD in constants file. If it is not true since both Receiver and Activity in the same class Intent instance is created with the class. Then integer values which are got by casting String that are provided by EditTexts are put into Intent instance. Finally sendBroadcast() is called.&lt;/div&gt;
&lt;br /&gt;
Method onReceive(Context, Intent) of NumberReceiver. has been written below.&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;x = intent.getIntExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_X&lt;/span&gt;, &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;);
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;y = intent.getIntExtra(Constants.&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;EXTRA_Y&lt;/span&gt;, &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;);

    String action = intent.getAction();
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(action != &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {
        Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Action = &quot;&lt;/span&gt;+action, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).
                show();
    } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;else &lt;/span&gt;{
        Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Action isn&#39;t available&quot;&lt;/span&gt;, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).
                show();
    }


    Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;x + y = &quot;&lt;/span&gt;+Calculator.&lt;span style=&quot;font-style: italic;&quot;&gt;performAddition&lt;/span&gt;(x,y), Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).
            show();
}&lt;/pre&gt;
&lt;br /&gt;
It get extras and actions then display them with a Toast.&lt;br /&gt;
&lt;br /&gt;
Calculations is done by using the Calculator class. Calculator class implementation is this.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;Calculator {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static int &lt;/span&gt;performAddition (&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;x, &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;y) {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;x+y;
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/6902799926094792906/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/08/sendbroadcast-android-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/6902799926094792906'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/6902799926094792906'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/08/sendbroadcast-android-example.html' title='sendBroadcast Android Example'/><author><name>Nipun De Silva</name><uri>http://www.blogger.com/profile/01584925573563517469</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-516297856614771439</id><published>2016-07-17T08:04:00.002-07:00</published><updated>2016-07-17T08:04:51.022-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="BroadcastReceiver"/><category scheme="http://www.blogger.com/atom/ns#" term="LocalBroadcastManager"/><title type='text'>Android LocalBroadcastManager Example</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
Using broadcasts in Android applications sometimes introduce problems as broadcasts can be received by the other applications too. To prevent these problems Android system has&amp;nbsp;&lt;a href=&quot;https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html&quot; target=&quot;_blank&quot;&gt;LocalBroadcastManager&lt;/a&gt; that can be used to implement secure communication mechanism within app components.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This example shows how to use LocalBroadcastManager to send and receive broadcasts. Though this application has only one activity it can LocalBroadcastManager is capable doing more than that. For example It can be used to send broadcasts from service to another activity, from One BroadcastReceiver to activity like use cases.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The app which is developed for demonstration has one EditText and Button. When user type something on EditText and click the Button typed content send to BroadcastReceiver that has been registered by using LocalBroadcastManager. After it received that Text it shows as a Toast.&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEincFQcMjqHSH9Tq__5rSnfydbVK0en_t4ayT7YV2FitIwMErLFF15iEnySfS8vNkYEnRSUpdjqvIANrbQbKibqejdOW8365qtl32B1iD9euDCJSgfy5TniRtHlznF4DMMNM8uUSR8ulbG_/s1600/Screenshot_20160717-195429.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEincFQcMjqHSH9Tq__5rSnfydbVK0en_t4ayT7YV2FitIwMErLFF15iEnySfS8vNkYEnRSUpdjqvIANrbQbKibqejdOW8365qtl32B1iD9euDCJSgfy5TniRtHlznF4DMMNM8uUSR8ulbG_/s640/Screenshot_20160717-195429.png&quot; width=&quot;360&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Above image shows what this application does. Implementation of this application is on MainActivity class which is shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;background-color: #e4e4ff; color: navy; font-weight: bold;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt; &lt;/span&gt;android.content.BroadcastReceiver;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.content.Context;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.content.Intent;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.content.IntentFilter;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.support.v4.content.LocalBroadcastManager;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.support.v7.app.AppCompatActivity;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.os.Bundle;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.view.View;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.widget.EditText;
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.widget.Toast;

&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;MainActivity &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;AppCompatActivity {

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private final &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_SENT_ACTION &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;       &quot;com.blogspot.nipunswritings.bcsender.action.TEXT_SENT&quot;&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private final &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_EXTRA &lt;/span&gt;=&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;       &quot;com.blogspot.nipunswritings.bcsender.extra.TEXT&quot;&lt;/span&gt;;

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;&lt;span style=&quot;background-color: #e4e4ff;&quot;&gt;BroadcastReceiver&lt;/span&gt; &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;textSentReceier &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background-color: #e4e4ff;&quot;&gt;BroadcastReceiver&lt;/span&gt;() {
        &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
            String textExtra = intent.getStringExtra(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_EXTRA&lt;/span&gt;);
            Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, textExtra, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show();
        }
    };

    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onCreate(Bundle savedInstanceState) {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onCreate(savedInstanceState);
        setContentView(R.layout.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;activity_main&lt;/span&gt;);
    }

    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onResume() {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onResume();

        IntentFilter textSentIntentFilter = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;IntentFilter(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_SENT_ACTION&lt;/span&gt;);
        LocalBroadcastManager
                .&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;)
                .registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;textSentReceier&lt;/span&gt;, textSentIntentFilter);
    }

    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onPause() {
        LocalBroadcastManager.&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;            .unregisterReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;textSentReceier&lt;/span&gt;);

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onPause();

    }

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendLocalTestBc(View view) {
        EditText sendingTextEt = (EditText) findViewById(R.id.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;activity_main_sent_text_et&lt;/span&gt;);
        String sendingText = sendingTextEt.getText().toString();

        Intent textSentIntent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_SENT_ACTION&lt;/span&gt;);
        textSentIntent.putExtra(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_EXTRA&lt;/span&gt;, sendingText);
        LocalBroadcastManager.&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;).sendBroadcast(textSentIntent);
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As Broadcasts and associated data are needed to be identified at the beginning of above class unique identifiers for above class has been defined like this.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private final &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_SENT_ACTION &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;       &quot;com.blogspot.nipunswritings.bcsender.action.TEXT_SENT&quot;&lt;/span&gt;;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private final &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_EXTRA &lt;/span&gt;=&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;       &quot;com.blogspot.nipunswritings.bcsender.extra.TEXT&quot;&lt;/span&gt;;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Receive Local Broadcasts&amp;nbsp; &lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
To receive broadcasts this activity must have a BroadcastReceiver implementation, that is shown below.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;&lt;span style=&quot;background-color: #e4e4ff;&quot;&gt;BroadcastReceiver&lt;/span&gt; &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;textSentReceier &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;&lt;span style=&quot;background-color: #e4e4ff;&quot;&gt;BroadcastReceiver&lt;/span&gt;() {
        &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
            String textExtra = intent.getStringExtra(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_EXTRA&lt;/span&gt;);
            Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, textExtra, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show();
        }
    };&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Register Receiver with LocalBroadcastManager&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In the onResume() IntentFilter is created to register receiver for designated action. IntentFilter constructor take that action as the parameter so it can receive broadcasts with this action.&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Next line is about registering receiver for the IntentFilter by using &lt;b&gt;LocalBroadcastManager&lt;/b&gt; and &lt;b&gt;registerReceiver()&lt;/b&gt;. After that receiver has ability to get broadcasts with&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #660e7a;&quot;&gt;TEXT_SENT_ACTION&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;
    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onResume() {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onResume();

        IntentFilter textSentIntentFilter = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;IntentFilter(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_SENT_ACTION&lt;/span&gt;);
        LocalBroadcastManager
                .&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;)
                .registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;textSentReceier&lt;/span&gt;, textSentIntentFilter);
    }&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Unregister Receiver with LocalBroadcastManager&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;
    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onPause() {
        LocalBroadcastManager.&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;            .unregisterReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;textSentReceier&lt;/span&gt;);

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onPause();

    }&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
All registered BroadcastReceivers must be unregistered so it has done in onPause() by using &lt;b&gt;unregisterReceiver()&lt;/b&gt; and &lt;b&gt;LocalBroadcastManager&lt;/b&gt;.&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Sending LocalBroadCasts&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendLocalTestBc(View view) {
        EditText sendingTextEt = (EditText) findViewById(R.id.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;activity_main_sent_text_et&lt;/span&gt;);
        String sendingText = sendingTextEt.getText().toString();

        Intent textSentIntent = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Intent(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_SENT_ACTION&lt;/span&gt;);
        textSentIntent.putExtra(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;TEXT_EXTRA&lt;/span&gt;, sendingText);
        LocalBroadcastManager.&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(MainActivity.&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;).sendBroadcast(textSentIntent);
    }&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Above method is onClick method of the button. When user touch that button a Intent is crated with previously defined action and data key. After that it is sent with sendBroadcast() and LocalBroadcastManager. &lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;References:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html&lt;/li&gt;
&lt;/ul&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/516297856614771439/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/07/android-localbroadcastmanager-example.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/516297856614771439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/516297856614771439'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/07/android-localbroadcastmanager-example.html' title='Android LocalBroadcastManager Example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEincFQcMjqHSH9Tq__5rSnfydbVK0en_t4ayT7YV2FitIwMErLFF15iEnySfS8vNkYEnRSUpdjqvIANrbQbKibqejdOW8365qtl32B1iD9euDCJSgfy5TniRtHlznF4DMMNM8uUSR8ulbG_/s72-c/Screenshot_20160717-195429.png" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-8609969330171712144</id><published>2016-06-26T11:11:00.001-07:00</published><updated>2016-06-26T11:11:40.939-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="BroadcastReceiver"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>registerReceiver (BroadcastReceiver, IntentFilter) Android Example</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
This example is about using &lt;a href=&quot;https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)&quot; target=&quot;_blank&quot;&gt;registerReceiver&lt;/a&gt; and&amp;nbsp;&lt;a href=&quot;https://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)&quot; target=&quot;_blank&quot;&gt;unregisterReceiver&lt;/a&gt; methods to use BroadcastReceiver programmatically. When BroadcastReceiver is registered in this way it&#39;s not declared in the AndroidManifest.xml file.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
This application project has one Activity file and it register BroadcastReceiver to obtain battery level change information. Broadcast action&amp;nbsp;&lt;a href=&quot;https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED&quot; target=&quot;_blank&quot;&gt;ACTION_BATTERY_CHANGED&lt;/a&gt; &lt;span id=&quot;goog_632464419&quot;&gt;&lt;/span&gt;&lt;span id=&quot;goog_632464420&quot;&gt;&lt;/span&gt; is used by Android system to broadcast battery level changing information. Therefore that is used in this project.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Below source code is about implementation of the class BatteryChangedReceiver that extends from BroadcastReceiver. It receive battery change information and extra data on that intent are got by using the class &lt;a href=&quot;https://developer.android.com/reference/android/os/BatteryManager.html&quot; target=&quot;_blank&quot;&gt;BatteryManager&lt;/a&gt;. BatteryManager class has definitions of extra data that are sent with aforementioned broadcast action. Once onReceive(Context, Intent) method receives that data they are shown as Toast.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;BatteryChangedReceiver Implementation&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;background-color: #e4e4ff; color: navy; font-weight: bold;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt; &lt;/span&gt;android.content.BroadcastReceiver;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.content.Context;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.content.Intent;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.os.BatteryManager;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;android.widget.Toast;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;BatteryChangedReceiver &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;&lt;span style=&quot;background-color: #e4e4ff;&quot;&gt;BroadcastReceiver&lt;/span&gt; {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;   public &lt;/span&gt;BatteryChangedReceiver() {&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;   }&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   @Override&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;      if &lt;/span&gt;(intent.getAction().equals(Intent.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;ACTION_BATTERY_CHANGED&lt;/span&gt;)) {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;         int &lt;/span&gt;level = intent.getIntExtra(BatteryManager.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;EXTRA_LEVEL&lt;/span&gt;, -&lt;span style=&quot;color: blue;&quot;&gt;1&lt;/span&gt;);&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;         int &lt;/span&gt;scale = intent.getIntExtra(BatteryManager.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;EXTRA_SCALE&lt;/span&gt;, -&lt;span style=&quot;color: blue;&quot;&gt;1&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;         Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Level = &quot;&lt;/span&gt;+level+&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;,&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;                        Scale = &quot;&lt;/span&gt;+scale, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show();&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;      }&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;   }&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Above BroadcastReceiver is registered in the class MainActivity. That source code is this.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;MainActivity Implementation&lt;/b&gt; &lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;MainActivity &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;AppCompatActivity {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;   private &lt;/span&gt;BatteryChangedReceiver &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;batteryChangedReceiver&lt;/span&gt;;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;   private &lt;/span&gt;IntentFilter &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;intentFilter&lt;/span&gt;;&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   @Override&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onCreate(Bundle savedInstanceState) {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;      super&lt;/span&gt;.onCreate(savedInstanceState);&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;      setContentView(R.layout.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;activity_main&lt;/span&gt;);&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;      batteryChangedReceiver &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;BatteryChangedReceiver();&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;      intentFilter &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;IntentFilter(Intent.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;ACTION_BATTERY_CHANGED&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;   }&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   @Override&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onResume() {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;      super&lt;/span&gt;.onResume();&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;      &lt;/span&gt;registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;batteryChangedReceiver&lt;/span&gt;, &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;intentFilter&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;   }&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   @Override&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onPause() {&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;      unregisterReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;batteryChangedReceiver&lt;/span&gt;);
      &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;.onPause();&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;   }&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
An instance of the BatteryChangedReceiver is created to be used as first argument to registerReceiver() method. This method needs IntentFilter object for second argument to register BroadcastReceiver provided with first argument for a specific Intent. So both objects for first and second arguments are created in onCreate() method.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;   batteryChangedReceiver &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;BatteryChangedReceiver();&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;   intentFilter &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;IntentFilter(Intent.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;ACTION_BATTERY_CHANGED&lt;/span&gt;);&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
The method registerReceiver() is called in onResume() to register BroadcastReceiver.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onResume() {&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;   super&lt;/span&gt;.onResume();&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;   &lt;/span&gt;registerReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;batteryChangedReceiver&lt;/span&gt;, &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;intentFilter&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;}&lt;span style=&quot;color: olive;&quot;&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
As the BroadcastReceiver is registered in the onResume() it must be unregistered onPause(). Otherwise error will be occurred for leaking BroadcastReceiver. A BroadcastReceiver must be unregistered as many as it is registered.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&amp;nbsp; &lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onPause() {&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;   unregisterReceiver(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;batteryChangedReceiver&lt;/span&gt;);&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;   super&lt;/span&gt;.onPause();&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
If onStop() is used to register BroadcastReceiver it must be unregistered in onStop().&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;b&gt;Reference&lt;/b&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver,%20android.content.IntentFilter)&lt;/li&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)&lt;/li&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED&lt;/li&gt;
&lt;li&gt;https://developer.android.com/reference/android/os/BatteryManager.html&lt;/li&gt;
&lt;/ul&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/8609969330171712144/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/registerreceiver-broadcastreceiver.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8609969330171712144'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8609969330171712144'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/registerreceiver-broadcastreceiver.html' title='registerReceiver (BroadcastReceiver, IntentFilter) Android Example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-3438787243889244169</id><published>2016-06-19T10:29:00.003-07:00</published><updated>2016-06-19T10:33:17.345-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="BroadcastReceiver"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Android BroadcastReceiver</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
Android applications need some mechanism to know when some event is occurred. For example some applications need to perform tasks when Android OS booting is completed. Android SDK provide&amp;nbsp;&lt;a href=&quot;https://developer.android.com/reference/android/content/BroadcastReceiver.html&quot; target=&quot;_blank&quot;&gt;BroadcastReceiver&lt;/a&gt; class for that purpose. This blog post content is about using BroadcastReceiver to get notification about connecting and disconnecting Android OS running device from external power source.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Name of this application is &lt;i&gt;&lt;b&gt;ConDisConInfo&lt;/b&gt;&lt;/i&gt;. So it has the main package with name &lt;i&gt;&lt;b&gt;com.blogspot.nipunswritings.ConDisConInfo&lt;/b&gt;&lt;/i&gt;. To create a BroadcastReceiver follow these steps.&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Right Click on Main package&lt;/li&gt;
&lt;li&gt;New&lt;/li&gt;
&lt;li&gt;Other&lt;/li&gt;
&lt;li&gt;Broadcast Receiver&lt;/li&gt;
&lt;li&gt;Give name and click Finish&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Once Finish is clicked a new java file can be seen in main package with the name PowerConnInfoReceiver.java. It has a PowerConnInfoReceiver class which extends from super class BroadcastReceiver. PowerConnInfoReceiver has below method which receive power connection/disconnection information which is sent by system.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
    
}&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
After&amp;nbsp; PowerConnInfoReceiver is created AndroidManifest.xml file has been updated with receiver element. That is registering a Broadcast Receiver. The receiver element in AndroidManifest.xml file is this.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;receiver&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;    &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:name=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;.PowerConnInfoReceiver&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:enabled=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:exported=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;lt;/&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;receiver&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Though that element is in the AndroidManifest.xml file PowerConnInfoReceiver will not receive any broadcast. A BroadcastReceiver to receive broadcasts it must be specified what broadcasts it must be received through the intent-filter element. Therefore receiver element is updated like below to receive broadcasts as mentioned above.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;receiver&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;   &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:name=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;.PowerConnInfoReceiver&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;   &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:enabled=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:exported=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&amp;gt;
    &amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;intent-filter&lt;/span&gt;&amp;gt;
        &amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;action &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:name=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;android.intent.action.ACTION_POWER_CONNECTED&quot; &lt;/span&gt;/&amp;gt;
        &amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;action &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:name=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;android.intent.action.ACTION_POWER_DISCONNECTED&quot; &lt;/span&gt;/&amp;gt;
    &amp;lt;/&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;intent-filter&lt;/span&gt;&amp;gt;
&amp;lt;/&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;receiver&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Notice the actions inside intent-filter. Without those actions this application doesn&#39;t work as expected.&amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Method &lt;b&gt;onReceive(Context, Intent)&lt;/b&gt; is updated to filter received information via Intent object and to display information to user as a Toast. So updated &lt;b&gt;onReceive(Context, Intent)&lt;/b&gt; method is this.&lt;/div&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;onReceive(Context context, Intent intent) {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(intent.getAction().equals(Intent.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;ACTION_POWER_CONNECTED&lt;/span&gt;)){
        Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Power Connected&quot;&lt;/span&gt;, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show();
    } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;else if &lt;/span&gt;(intent.getAction().equals(Intent.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;ACTION_POWER_DISCONNECTED&lt;/span&gt;)) {
        Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(context, &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Power Disconnected&quot;&lt;/span&gt;, Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show();
    }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Run this app on a emulator. This application can be tested with battery section in extended controls of emulator. When charger connection is toggled between &lt;b&gt;AC Charger&lt;/b&gt; and &lt;b&gt;None&lt;/b&gt; corresponding Toast is displayed.&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU7KEnLZmQrn5sgl5LQzEsNTY-EVPTLgJsbuD7juA2ryYBaWG_HJZYYlZ8RhUbt375RABEK0czborvMMAbtXGx02jNe7euRknH64WEEbX8EnnzMNLl8ThbZLAP0lFdqJZC0HrC-16woUGD/s1600/PowerChange.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;201&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU7KEnLZmQrn5sgl5LQzEsNTY-EVPTLgJsbuD7juA2ryYBaWG_HJZYYlZ8RhUbt375RABEK0czborvMMAbtXGx02jNe7euRknH64WEEbX8EnnzMNLl8ThbZLAP0lFdqJZC0HrC-16woUGD/s400/PowerChange.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Reference:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/BroadcastReceiver.html&lt;/li&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/Intent.html#ACTION_POWER_CONNECTED&lt;/li&gt;
&lt;li&gt;https://developer.android.com/reference/android/content/Intent.html#ACTION_POWER_DISCONNECTED&lt;/li&gt;
&lt;/ul&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/3438787243889244169/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/android-broadcastreceiver.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/3438787243889244169'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/3438787243889244169'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/android-broadcastreceiver.html' title='Android BroadcastReceiver'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiU7KEnLZmQrn5sgl5LQzEsNTY-EVPTLgJsbuD7juA2ryYBaWG_HJZYYlZ8RhUbt375RABEK0czborvMMAbtXGx02jNe7euRknH64WEEbX8EnnzMNLl8ThbZLAP0lFdqJZC0HrC-16woUGD/s72-c/PowerChange.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-8534462140963698561</id><published>2016-06-12T11:05:00.000-07:00</published><updated>2016-06-12T11:12:59.076-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Chart Library"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Java Chart Library"/><category scheme="http://www.blogger.com/atom/ns#" term="HelloCharts-Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>HelloCharts for Android Example</title><content type='html'>This example is using&amp;nbsp;&lt;a href=&quot;https://github.com/lecho/hellocharts-android&quot; target=&quot;_blank&quot;&gt;HelloCharts for Android&lt;/a&gt; to plot line chart for function |sin(x)|. The value range for x axis is from 0 to 360 degrees. The x values are given like 0, 15, 30, 45 ... 360. So there is difference of 15 degrees between&amp;nbsp; two points. But X axis is drawn with 30 degree scale. Therefore it goes like 0, 30, 60, 90, ..., 180, ...360. Y axis is drawn with scale of 0.25 from 0 to 1.0 to make Y axis range 0, 0.25, 0.5, 1.&lt;br /&gt;
&lt;br /&gt;
At the end of this example developed application creates the below chart.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2xjxuYQVHFDeX023PybTA2TDVCrmtNpcPaH_TZIUnTW4gcZVb4m4XjkHQlMypzYHQ5hlNk6waJQfhUj23NJut2COpoELZ7_1eg7L0QWEeKPlYwRm8YKAdQog5F5dF2l7En_y1CSW6_kgX/s1600/Screenshot_2016-06-12-23-18-08.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;208&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2xjxuYQVHFDeX023PybTA2TDVCrmtNpcPaH_TZIUnTW4gcZVb4m4XjkHQlMypzYHQ5hlNk6waJQfhUj23NJut2COpoELZ7_1eg7L0QWEeKPlYwRm8YKAdQog5F5dF2l7En_y1CSW6_kgX/s400/Screenshot_2016-06-12-23-18-08.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;/div&gt;
Add hello-charts library to app by adding below line to dependencies block of build.gradle file of app module. Then sync the project by using Android Studio.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;compile &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&#39;com.github.lecho:hellocharts-library:1.5.8@aar&#39;&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Project has one Activity file with the name &lt;i&gt;&lt;b&gt;MainActivity.java&lt;/b&gt;&lt;/i&gt;. It&#39;s layout resource file is &lt;i&gt;&lt;b&gt;activity_main.xml&lt;/b&gt;&lt;/i&gt;. &lt;br /&gt;
&lt;br /&gt;
This is the content of &lt;i&gt;&lt;b&gt;activity_main.xml&lt;/b&gt;&lt;/i&gt;. It has &lt;i&gt;&lt;b&gt;RelativeLayout&lt;/b&gt;&lt;/i&gt; as root element and RelativeLayout contain TextView and chart definition.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;TextView&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:id=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;@+id/chartLbl&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:gravity=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;center&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_width=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_height=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;wrap_content&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:text=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;|sin(x)|&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_alignParentTop=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt; android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_alignParentStart=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt; android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_alignParentLeft=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt; android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_margin=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;5dp&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:textStyle=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;bold&quot; &lt;/span&gt;/&amp;gt;

&amp;lt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;lecho.lib.hellocharts.view.LineChartView&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:id=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;@+id/chart&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt; android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_width=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_height=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;match_parent&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:padding=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;10dp&quot;&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;android&lt;/span&gt;&lt;span style=&quot;color: blue; font-weight: bold;&quot;&gt;:layout_below=&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;@+id/chartLbl&quot;&lt;/span&gt;/&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
The element &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;lecho.lib.hellocharts.view.LineChartView&lt;/span&gt; is xml definition of the line chart.&lt;br /&gt;
&lt;br /&gt;
Now reference to this LineChartView can be made with findViewById(int) method. To hold reference to that view, field of type LineChartView has been defined in the &lt;b&gt;MainActivity.java&lt;/b&gt; like this.&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;LineChartView &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;lineChartView&lt;/span&gt;;&lt;/pre&gt;
&lt;br /&gt;
Line chart has been created with below method. That is called in onCreate(Bundle) method.&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;drawSinAbsChart()&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Method definition of&amp;nbsp; drawSinAbsChart()&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;drawSinAbsChart() {
    String decimalPattern = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;#.##&quot;&lt;/span&gt;;
    DecimalFormat decimalFormat = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;DecimalFormat(decimalPattern);

    &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;lineChartView &lt;/span&gt;= (LineChartView) findViewById(R.id.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;chart&lt;/span&gt;);

    List&amp;lt;PointValue&amp;gt; values = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;PointValue&amp;gt;();

    PointValue tempPointValue;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;; i &amp;lt;= &lt;span style=&quot;color: blue;&quot;&gt;360.0&lt;/span&gt;; i+= &lt;span style=&quot;color: blue;&quot;&gt;15.0f&lt;/span&gt;) {
        tempPointValue = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;PointValue(i, Math.&lt;span style=&quot;font-style: italic;&quot;&gt;abs&lt;/span&gt;((&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float&lt;/span&gt;)Math.&lt;span style=&quot;font-style: italic;&quot;&gt;sin&lt;/span&gt;(Math.&lt;span style=&quot;font-style: italic;&quot;&gt;toRadians&lt;/span&gt;(i))));
        tempPointValue.setLabel(decimalFormat&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .format(Math.&lt;span style=&quot;font-style: italic;&quot;&gt;abs&lt;/span&gt;((&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float&lt;/span&gt;)Math.&lt;span style=&quot;font-style: italic;&quot;&gt;sin&lt;/span&gt;(Math.&lt;span style=&quot;font-style: italic;&quot;&gt;toRadians&lt;/span&gt;(i)))));
        values.add(tempPointValue);
    }

    Line line = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Line(values)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .setColor(Color.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;BLUE&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .setCubic(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;false&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .setHasPoints(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;).setHasLabels(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;);
    List&amp;lt;Line&amp;gt; lines = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;Line&amp;gt;();
    lines.add(line);

    LineChartData data = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;LineChartData();
    data.setLines(lines);

    List&amp;lt;AxisValue&amp;gt; axisValuesForX = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;&amp;gt;();
    List&amp;lt;AxisValue&amp;gt; axisValuesForY = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;&amp;gt;();
    AxisValue tempAxisValue;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;; i &amp;lt;= &lt;span style=&quot;color: blue;&quot;&gt;360.0f&lt;/span&gt;; i += &lt;span style=&quot;color: blue;&quot;&gt;30.0f&lt;/span&gt;){
        tempAxisValue = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;AxisValue(i);
        tempAxisValue.setLabel(i+&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;\u00b0&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&lt;/span&gt;);
        axisValuesForX.add(tempAxisValue);
    }

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0.0f&lt;/span&gt;; i &amp;lt;= &lt;span style=&quot;color: blue;&quot;&gt;1.00f&lt;/span&gt;; i += &lt;span style=&quot;color: blue;&quot;&gt;0.25f&lt;/span&gt;){
        tempAxisValue = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;AxisValue(i);
        tempAxisValue.setLabel(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&quot;&lt;/span&gt;+i);
        axisValuesForY.add(tempAxisValue);
    }

    Axis xAxis = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Axis(axisValuesForX);
    Axis yAxis = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Axis(axisValuesForY);
    data.setAxisXBottom(xAxis);
    data.setAxisYLeft(yAxis);


    &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;lineChartView&lt;/span&gt;.setLineChartData(data);


}&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Code Explanation&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    String decimalPattern = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;#.##&quot;&lt;/span&gt;;
    DecimalFormat decimalFormat = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;DecimalFormat(decimalPattern);&lt;/pre&gt;
&lt;br /&gt;
Values that are obtained must be shown only up to two decimal places in chart point labels. Object decimalFormat is created for that purpose.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;lineChartView &lt;/span&gt;= (LineChartView) findViewById(R.id.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;chart&lt;/span&gt;);&lt;/pre&gt;
&lt;br /&gt;
Above jave statement is to obtain the LineChartView object that is declared in the layout xml resource.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    List&amp;lt;PointValue&amp;gt; values = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;PointValue&amp;gt;();

    PointValue tempPointValue;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;; i &amp;lt;= &lt;span style=&quot;color: blue;&quot;&gt;360.0&lt;/span&gt;; i+= &lt;span style=&quot;color: blue;&quot;&gt;15.0f&lt;/span&gt;) {
        tempPointValue = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;PointValue(i, Math.&lt;span style=&quot;font-style: italic;&quot;&gt;abs&lt;/span&gt;((&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float&lt;/span&gt;)Math.&lt;span style=&quot;font-style: italic;&quot;&gt;sin&lt;/span&gt;(Math.&lt;span style=&quot;font-style: italic;&quot;&gt;toRadians&lt;/span&gt;(i))));
        tempPointValue.setLabel(decimalFormat&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .format(Math.&lt;span style=&quot;font-style: italic;&quot;&gt;abs&lt;/span&gt;((&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float&lt;/span&gt;)Math.&lt;span style=&quot;font-style: italic;&quot;&gt;sin&lt;/span&gt;(Math.&lt;span style=&quot;font-style: italic;&quot;&gt;toRadians&lt;/span&gt;(i)))));
        values.add(tempPointValue);
    }&lt;/pre&gt;
&lt;br /&gt;
Tha values that must be displayed in the line chart must be in the List of type List&amp;lt;PointValue&amp;gt;. That is doing here. That list contains points which are drawn in the chart. Point is represented with PointValue Class. for loop create those points and set it label that are shown at the points.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    Line line = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Line(values)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .setColor(Color.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;BLUE&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .setCubic(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;false&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;                   .setHasPoints(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;).setHasLabels(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;);
    List&amp;lt;Line&amp;gt; lines = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;Line&amp;gt;();
    lines.add(line);

    LineChartData data = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;LineChartData();
    data.setLines(lines);&lt;/pre&gt;
&lt;br /&gt;
Line chart can&amp;nbsp; contain many lines, Above code shows how to create new line with the values which was generated function |sin(x)|. Method setColor() is used to define color of the line. This chart has straight line as setCubic() has been provided false value. If setHasPoints() called with argument false value points wouldn&#39;t be displayed.When setHasLabels() is provided with argument true value labels that was set during PointValue object are created is shown. The lines variable of type List&amp;lt;Line&amp;gt; holds all the lines that is drawn in the chart. This example chart has only one line so it has been added. Finally that lines object is added to LineChartData object with setLines().&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    List&amp;lt;AxisValue&amp;gt; axisValuesForX = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;&amp;gt;();
    List&amp;lt;AxisValue&amp;gt; axisValuesForY = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;ArrayList&amp;lt;&amp;gt;();
    AxisValue tempAxisValue;
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;; i &amp;lt;= &lt;span style=&quot;color: blue;&quot;&gt;360.0f&lt;/span&gt;; i += &lt;span style=&quot;color: blue;&quot;&gt;30.0f&lt;/span&gt;){
        tempAxisValue = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;AxisValue(i);
        tempAxisValue.setLabel(i+&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;\u00b0&lt;/span&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&lt;/span&gt;);
        axisValuesForX.add(tempAxisValue);
    }

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;float &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0.0f&lt;/span&gt;; i &amp;lt;= &lt;span style=&quot;color: blue;&quot;&gt;1.00f&lt;/span&gt;; i += &lt;span style=&quot;color: blue;&quot;&gt;0.25f&lt;/span&gt;){
        tempAxisValue = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;AxisValue(i);
        tempAxisValue.setLabel(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&quot;&lt;/span&gt;+i);
        axisValuesForY.add(tempAxisValue);
    }

    Axis xAxis = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Axis(axisValuesForX);
    Axis yAxis = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Axis(axisValuesForY);
    data.setAxisXBottom(xAxis);
    data.setAxisYLeft(yAxis);&lt;/pre&gt;
&lt;br /&gt;
Java statements that are shown creating X and Y axis of the chart. First loop creates X axis which is started from 0 and goes upto 360 with increment 15.0 from previous x value. Values of axis is represented with object of type List&amp;lt;AxisValue&amp;gt; and AxisValue objects are used represent a location in the axis. AxisValue has setLabel() to display developer preferred label in the chart. In same way Y axis also has been created. After thata x &amp;amp; y axis is created as Axis objects. X axis is set to bottom with setAxisXBottom() and Y Axis is set to left with setAxisYLeft().&lt;br /&gt;
&lt;br /&gt;
Chart data is provided to LineChartView with below statement.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;Courier New&#39;; font-size: 9.0pt;&quot;&gt;    &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;lineChartView&lt;/span&gt;.setLineChartData(data);&lt;/pre&gt;
&lt;br /&gt;
If axis values is cut off this&amp;nbsp;&lt;a href=&quot;https://github.com/lecho/hellocharts-android/issues/243&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt; might be helpful. I had similar problem but I could resolve it without that link by adding 5dp padding around LineChartView.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Reference:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;https://github.com/lecho/hellocharts-android&lt;/li&gt;
&lt;/ul&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/8534462140963698561/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/hellocharts-for-android-example.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8534462140963698561'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8534462140963698561'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/hellocharts-for-android-example.html' title='HelloCharts for Android Example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2xjxuYQVHFDeX023PybTA2TDVCrmtNpcPaH_TZIUnTW4gcZVb4m4XjkHQlMypzYHQ5hlNk6waJQfhUj23NJut2COpoELZ7_1eg7L0QWEeKPlYwRm8YKAdQog5F5dF2l7En_y1CSW6_kgX/s72-c/Screenshot_2016-06-12-23-18-08.png" height="72" width="72"/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-2780929001559140252</id><published>2016-06-04T04:59:00.002-07:00</published><updated>2016-06-04T04:59:41.149-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Development Problems"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Studio"/><title type='text'>Renaming Android Studio Project and Main Package</title><content type='html'>Content of this blog post has been tested with Android Studio 2.0.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Changing Project Name&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Open Project Window (View → Tool Windows → Project or Alt + 1)&lt;/li&gt;
&lt;li&gt;By default Project window shows the Android view change it to Project view with the drop down menu at top left.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Gf0uqZ0vbSo6h8bXiXZeriPn0Ue5SV7ZjEIAJNTPY_pZYnOpcgCwjQKGU7a0JSOQKLrfmFMND-RyDytCzzn-ArTeNoowm5Pmb00_HvutmXGPCY7FgUYzSy50ZTUWGbgseTVAd1eP-BaB/s1600/project+Window.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;262&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Gf0uqZ0vbSo6h8bXiXZeriPn0Ue5SV7ZjEIAJNTPY_pZYnOpcgCwjQKGU7a0JSOQKLrfmFMND-RyDytCzzn-ArTeNoowm5Pmb00_HvutmXGPCY7FgUYzSy50ZTUWGbgseTVAd1eP-BaB/s320/project+Window.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&amp;nbsp;Right click on project root → Show in Explorer.&lt;/li&gt;
&lt;li&gt;Close Android Studio.&lt;/li&gt;
&lt;li&gt;Go into project folder.&lt;/li&gt;
&lt;li&gt;Change .iml file to new name that you want to have for the project.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;Change the project folder name to new name which is same name of .iml file.&lt;/li&gt;
&lt;li&gt;Start Android Studio&lt;/li&gt;
&lt;li&gt;Click on “Open an existing Android Studio project”&lt;/li&gt;
&lt;li&gt;Find &amp;amp; select the renamed project, then “Ok”&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Changing Package name &lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Right Click on package name (I tried this on main package)&lt;/li&gt;
&lt;li&gt;Refactor&lt;/li&gt;
&lt;li&gt;Rename&lt;/li&gt;
&lt;li&gt;Rename Package&lt;/li&gt;
&lt;li&gt;Type new name &amp;amp; click Refactor&lt;/li&gt;
&lt;li&gt;Click on Do Refactor&amp;nbsp; &lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
After doing this I have observed changing the main package name successfully renamed other two packages (androidTest &amp;amp; Test) during refactoring process. Although Generated files like AndroidManifest.xml also had correct values strings.xml file had old project name for the string element with attribute “app_name”.&lt;br /&gt;Above experiment was done on a very small project. &lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/2780929001559140252/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/renaming-android-studio-project-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/2780929001559140252'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/2780929001559140252'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/06/renaming-android-studio-project-and.html' title='Renaming Android Studio Project and Main Package'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Gf0uqZ0vbSo6h8bXiXZeriPn0Ue5SV7ZjEIAJNTPY_pZYnOpcgCwjQKGU7a0JSOQKLrfmFMND-RyDytCzzn-ArTeNoowm5Pmb00_HvutmXGPCY7FgUYzSy50ZTUWGbgseTVAd1eP-BaB/s72-c/project+Window.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-1164142880788295873</id><published>2016-02-14T10:20:00.000-08:00</published><updated>2016-02-14T10:20:13.609-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="iOS"/><category scheme="http://www.blogger.com/atom/ns#" term="iOS Adaptive Layout"/><category scheme="http://www.blogger.com/atom/ns#" term="iOS Auto Layout"/><category scheme="http://www.blogger.com/atom/ns#" term="iOS Universal Storyboards"/><category scheme="http://www.blogger.com/atom/ns#" term="Swift Language"/><title type='text'>iOS Auto Layout Notes - 1</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
This post has written about&amp;nbsp;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Adding auto layout constraints to UI object by using interface builder&lt;/li&gt;
&lt;li&gt;Changing auto layout constraints by using swift language&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Auto layout constraints can be defined to work with different Size classes. Size classes that is applied to different devices and orientations can be change with the below Pop over in the interface builder.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUv5jLTCZn6jIfLHaEJOGuCd9eoGfrdl7_cBWf6cVr79h7wNVvPQVxdcv2CYAo9fhfCO6aaqgJxEClG3e-jmr1uSHmhVXwnNe6dQhfC-rmljzfLaz_FMWkk8aTDTGNC-e9MIAtRaF_uS_Z/s1600/menu.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUv5jLTCZn6jIfLHaEJOGuCd9eoGfrdl7_cBWf6cVr79h7wNVvPQVxdcv2CYAo9fhfCO6aaqgJxEClG3e-jmr1uSHmhVXwnNe6dQhfC-rmljzfLaz_FMWkk8aTDTGNC-e9MIAtRaF_uS_Z/s320/menu.png&quot; width=&quot;266&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Since UI components of this application is needed to be fit to all the UIs height and width size class of &lt;b&gt;Any&lt;/b&gt; is selected here.&lt;br /&gt;
&lt;br /&gt;
There is a UIImageView object on the UIViewController of this class. To center it horizontally on the screen select the UIImageView click on the &lt;b&gt;align&lt;/b&gt; button to display &lt;b&gt;Add New Alignment Constraints&lt;/b&gt; pop up.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEZsnbw7gLq_WvLt-q9-bEh4BHpojJ_6weTts1ezH8uG9nqN9NqbbgoQtxsEVO1ayNHXML3kOLpVXTMm3kVzBrdv_9hJUG91w7K3YGzOzjgQWvujaU5yG_-I42UQv1GRj0QdIKzk01cZdB/s1600/aligncon.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEZsnbw7gLq_WvLt-q9-bEh4BHpojJ_6weTts1ezH8uG9nqN9NqbbgoQtxsEVO1ayNHXML3kOLpVXTMm3kVzBrdv_9hJUG91w7K3YGzOzjgQWvujaU5yG_-I42UQv1GRj0QdIKzk01cZdB/s320/aligncon.png&quot; width=&quot;298&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
On the pop up menu Select &lt;b&gt;Horizontally in Container&lt;/b&gt; and then click on the button &lt;b&gt;Add 1 Constraint&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
UIImageView needs to be 10 point below from the top layout guide. And it need to have width and height of 200 points. To do that click on the pin button to show pop up that can be used to add constraints for above requirements.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-2WOxBLZnn-LCb_YGH5A9-NQB_4C0ZWxHFWnAiCAvkQtHks2tc5RjYlu_S80a7saJVCCeNmDgxNSVA-duwrJvakDH8ieFbkitEr0uagvHWVHSsGvyO9XtVKae68lIHQ9XwOMXggr39-Jr/s1600/pin.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-2WOxBLZnn-LCb_YGH5A9-NQB_4C0ZWxHFWnAiCAvkQtHks2tc5RjYlu_S80a7saJVCCeNmDgxNSVA-duwrJvakDH8ieFbkitEr0uagvHWVHSsGvyO9XtVKae68lIHQ9XwOMXggr39-Jr/s320/pin.png&quot; width=&quot;221&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
Once the constraints were set by using pin pop up like above click on &lt;b&gt;Add 3 Constraints&lt;/b&gt; to add them to UIImageView.&lt;br /&gt;
&lt;br /&gt;
To see changes on the storyboard. Click on the &lt;b&gt;Resolve Auto Layout Issues&lt;/b&gt; button that shows a&amp;nbsp;&amp;nbsp; menu like below .Then click on Update Frames.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjU8B2DHkVLjmxfOX3bBr1K0Mb1ejWNbRfmJ4UhCB4gM90ZuU3HEK8zB5JmYeWTv1_rhNJd6y96KWLw8GZe6cMZXKc0EmqTmHrknPiKnWXAKrQfdkyTf-k1xmQurAnJZ1oAGgRGek1HdSKc/s1600/update+frame.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;161&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjU8B2DHkVLjmxfOX3bBr1K0Mb1ejWNbRfmJ4UhCB4gM90ZuU3HEK8zB5JmYeWTv1_rhNJd6y96KWLw8GZe6cMZXKc0EmqTmHrknPiKnWXAKrQfdkyTf-k1xmQurAnJZ1oAGgRGek1HdSKc/s320/update+frame.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
That will remove orange color lines if there are them and adjust UI dimensions to reflect constraints.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Change Auto layout Constraints with Swift Code&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Create &lt;b&gt;IBOutlet&lt;/b&gt; to UIImageView width and height constraints.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;@IBOutlet weak var imageHeight: NSLayoutConstraint!&lt;br /&gt;@IBOutlet weak var imageWidth: NSLayoutConstraint!&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Write a method like below to change Auto layout constraints which is referred through outlets by using swift code.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;func resizeImage(size: Int){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; imageHeight.constant = CGFloat(size)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; imageWidth.constant = CGFloat(size)&lt;br /&gt;}&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Value of instance of&amp;nbsp; &lt;b&gt;NSLayoutConstraint&lt;/b&gt; can be changed by assigning new CGFloat value to that instance&#39;s &lt;b&gt;constant&lt;/b&gt; property.</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/1164142880788295873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/02/ios-auto-layout-notes-1.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1164142880788295873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1164142880788295873'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/02/ios-auto-layout-notes-1.html' title='iOS Auto Layout Notes - 1'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUv5jLTCZn6jIfLHaEJOGuCd9eoGfrdl7_cBWf6cVr79h7wNVvPQVxdcv2CYAo9fhfCO6aaqgJxEClG3e-jmr1uSHmhVXwnNe6dQhfC-rmljzfLaz_FMWkk8aTDTGNC-e9MIAtRaF_uS_Z/s72-c/menu.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-4763667600319959481</id><published>2016-02-07T02:56:00.000-08:00</published><updated>2016-02-07T02:56:50.417-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Boost C++"/><category scheme="http://www.blogger.com/atom/ns#" term="C++"/><title type='text'>How To Install Boost C++ Library On Ubuntu 14.04 LTS</title><content type='html'>This is how boost C++ library can be installed into a location that is needed to be put. Here I have installed into a folder in my home home folder. All the below command executed from a folder that contain &lt;i&gt;&lt;b&gt;boost_1_59_0.tar.bz2&lt;/b&gt;&lt;/i&gt; file. Boost has been installed without ICU.&lt;br /&gt;
&lt;br /&gt;
$ lsb_release -a&lt;br /&gt;&lt;br /&gt;No LSB modules are available.&lt;br /&gt;Distributor ID:&amp;nbsp;&amp;nbsp;&amp;nbsp; Ubuntu&lt;br /&gt;Description:&amp;nbsp;&amp;nbsp;&amp;nbsp; Ubuntu 14.04.2 LTS&lt;br /&gt;Release:&amp;nbsp;&amp;nbsp;&amp;nbsp; 14.04&lt;br /&gt;Codename:&amp;nbsp;&amp;nbsp;&amp;nbsp; trusty&lt;br /&gt;&lt;br /&gt;$ python -V&lt;br /&gt;&lt;br /&gt;Python 2.7.6&lt;br /&gt;&lt;br /&gt;$ sudo apt-get install g++&lt;br /&gt;&lt;br /&gt;$ sudo apt-get install python2.7-dev&lt;br /&gt;&lt;br /&gt;$ sudo apt-get install libicu-dev&lt;br /&gt;&lt;br /&gt;$ sudo apt-get install libbz2-dev&lt;br /&gt;&lt;br /&gt;$ sudo apt-get install build-essential&lt;br /&gt;&lt;br /&gt;$ sudo apt-get install autotools-dev&lt;br /&gt;&lt;br /&gt;$ tar --bzip2 -xf boost_1_59_0.tar.bz2&lt;br /&gt;&lt;br /&gt;$ cd boost_1_59_0/&lt;br /&gt;&lt;br /&gt;$ ./bootstrap.sh --prefix=/home/nipun/BoostLibs --without-icu&lt;br /&gt;&lt;br /&gt;$ ./b2 install --prefix=/home/nipun/BoostLibs</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/4763667600319959481/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2016/02/how-to-install-boost-c-library-on.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/4763667600319959481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/4763667600319959481'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2016/02/how-to-install-boost-c-library-on.html' title='How To Install Boost C++ Library On Ubuntu 14.04 LTS'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-1587337933300925120</id><published>2015-07-25T04:05:00.002-07:00</published><updated>2015-07-25T04:08:49.394-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="APIs"/><category scheme="http://www.blogger.com/atom/ns#" term="Cloud Computing"/><category scheme="http://www.blogger.com/atom/ns#" term="Google Cloud Endpoints"/><category scheme="http://www.blogger.com/atom/ns#" term="HMAC"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><category scheme="http://www.blogger.com/atom/ns#" term="Web Services"/><title type='text'>Securing Google Cloud Endpoints API for Android App With HMAC</title><content type='html'>Google cloud endpoints can be secured by using Oauth protocol provided via Google accounts. Although that feature is provided by the Google cloud endpoints developers don&#39;t want to use Oauth sometimes. It&#39;s also possible to protect Google Cloud Endpoint APIs with the HMAC authentication. This blog post demonstrate how to protect APIs with HMAC. Example backend APIs have been written by using Java programming language.&lt;br /&gt;
&lt;br /&gt;
Following implementation details will be more described in paragraphs below.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;How to access HttpServletRequest instance within the API methods&lt;/li&gt;
&lt;li&gt;How to calculate HMAC in java&lt;/li&gt;
&lt;li&gt;How to read http authorization header by using&amp;nbsp; HttpServletRequest instance&lt;/li&gt;
&lt;li&gt;How to set HMAC in authorization header by using Google Cloud Endpoint client library for Android&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
System Requirement&lt;/h3&gt;
Android app send a name of a user. Server should reply with Hi, &amp;lt;name of user&amp;gt;. For example, if app send nipun it should reply with Hi, Nipun. Aforementioned requirement already implemented when Google Cloud Endpoint module is added with Android Studio. Codes from Reference [1] is on content of this post.&lt;br /&gt;
&lt;h3&gt;
User Interface&lt;/h3&gt;
Android client UI has a Button and a EditText. EditText has the id nameUiValue. nameUiValue has been assigned to&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;EditText &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;nameEditText&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
When a user click on the button it calls the method.&lt;br /&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendName(View view)&lt;/pre&gt;
&lt;br /&gt;
&lt;style type=&quot;text/css&quot;&gt;p { margin-bottom: 0.1in; line-height: 120%; }a:link {  }&lt;/style&gt;


&lt;br /&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Before securing the
APIs Endpoint class and client AsyncTask class is show below.&lt;/div&gt;
&lt;h3 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Cloud &lt;/h3&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;b&gt;MyEndpoint.java&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;   For step-by-step instructions on connecting your Android application to this backend module,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;   see &quot;App Engine Java Endpoints Module&quot; template documentation at&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;   https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;*/&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;package &lt;/span&gt;com.example.nipun.myapplication.backend&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;com.google.api.server.spi.config.Api&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;com.google.api.server.spi.config.ApiMethod&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;com.google.api.server.spi.config.ApiNamespace&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;javax.inject.Named&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;javax.servlet.http.HttpServletRequest&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/**&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; * An endpoint class we are exposing&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; */&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Api&lt;/span&gt;(name = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;myApi&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;version = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;v1&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;namespace = &lt;span style=&quot;color: olive;&quot;&gt;@ApiNamespace&lt;/span&gt;(ownerDomain = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;backend.myapplication.nipun.example.com&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;                   &lt;/span&gt;ownerName = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;backend.myapplication.nipun.example.com&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;packagePath = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;&quot;&lt;/span&gt;))
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;MyEndpoint {

    &lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/**&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;     * A simple endpoint method that takes a name and says Hi back&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;     */&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@ApiMethod&lt;/span&gt;(name = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;sayHi&quot;&lt;/span&gt;)
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public &lt;/span&gt;MyBean sayHi(HttpServletRequest request&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Named&lt;/span&gt;(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;name&quot;&lt;/span&gt;) String name) {
        MyBean response = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;MyBean()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;response.setData(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Hi, &quot; &lt;/span&gt;+ name)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;response&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}

}&amp;nbsp; &lt;/pre&gt;
&lt;h3 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Android Client AsyncTask&lt;/h3&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;class &lt;/span&gt;EndpointsAsyncTask &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;AsyncTask&amp;lt;Pair&amp;lt;Context&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;Void&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt; {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;MyApi &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;Context &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected &lt;/span&gt;String doInBackground(Pair&amp;lt;Context&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt;... params) {
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService &lt;/span&gt;== &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {
            MyApi.Builder builder = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;MyApi.Builder(AndroidHttp.&lt;span style=&quot;font-style: italic;&quot;&gt;newCompatibleTransport&lt;/span&gt;()&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;                new &lt;/span&gt;AndroidJsonFactory()&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;)

                    .setRootUrl(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;http://10.0.2.2:8080/_ah/api/&quot;&lt;/span&gt;)
                    .setGoogleClientRequestInitializer(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;GoogleClientRequestInitializer() {
                        &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;                        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;initialize(AbstractGoogleClientRequest&amp;lt;?&amp;gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                                            abstractGoogleClientRequest) &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;throws &lt;/span&gt;IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;                        &lt;/span&gt;}
                    })&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService &lt;/span&gt;= builder.build()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;}

        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;context &lt;/span&gt;= params[&lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;].&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;first&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;String name = params[&lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;].&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;second&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;try &lt;/span&gt;{
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService&lt;/span&gt;.sayHi(name).execute().getData()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;} &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(IOException e) {
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;e.getMessage()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;}
    }

    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onPostExecute(String result) {
        Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;result&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}
} &lt;/pre&gt;
&lt;h3 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Securing Google Cloud Endpoint With HMAC&lt;/h3&gt;
&lt;h4 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
How to access HttpServletRequest instance within the API methods &lt;/h4&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
In order to get the HMAC value from http authorization header &lt;b&gt;HttpServletRequest&lt;/b&gt; must be accessed from the API method. It can be done by using following way. &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public &lt;/span&gt;MyBean sayHi(HttpServletRequest request&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Named&lt;/span&gt;(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;name&quot;&lt;/span&gt;) String name)&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;b&gt;ApiMethod&lt;/b&gt; has included &lt;b&gt;HttpServletRequest&lt;/b&gt; as an parameter.&amp;nbsp;&amp;nbsp; &lt;/div&gt;
&lt;h4 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
How to calculate HMAC in java &lt;/h4&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Hmac has been generated according to reference 3 and 4. A new hmac is calculated by using at the endpoint by using &lt;b&gt;AuthManager.java&lt;/b&gt; class. &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;package &lt;/span&gt;com.example.nipun.myapplication.backend.auth&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;java.io.UnsupportedEncodingException&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;java.security.InvalidKeyException&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;java.security.NoSuchAlgorithmException&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;javax.crypto.Mac&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;javax.crypto.spec.SecretKeySpec&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/**&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; * Created by nipun on 7/25/15.&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; */&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;AuthManager {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;KEY &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;E34swDN1Fk1G44zmw9bL2N5B8R85w290&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;ALGO &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;HmacSHA1&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public boolean &lt;/span&gt;authenticationSuccessful(String dataForHmac&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String authHeader){

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(authHeader == &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;)
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return false&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;String tempHmacParts[] = authHeader.split(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot; &quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(tempHmacParts.&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;length &lt;/span&gt;!= &lt;span style=&quot;color: blue;&quot;&gt;2&lt;/span&gt;)
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return false&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;tempHmacParts = tempHmacParts[&lt;span style=&quot;color: blue;&quot;&gt;1&lt;/span&gt;].split(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;:&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(tempHmacParts.&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;length &lt;/span&gt;!= &lt;span style=&quot;color: blue;&quot;&gt;2&lt;/span&gt;)
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return false&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(tempHmacParts[&lt;span style=&quot;color: blue;&quot;&gt;1&lt;/span&gt;].equals(hmacDigest(dataForHmac&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;KEY&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;ALGO&lt;/span&gt;))){
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return true&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;}&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;else &lt;/span&gt;{
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return false&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;}
    }

    &lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    *&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    * This method is taken from&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    * http://www.supermind.org/blog/1102/generating-hmac-md5-sha1-sha256-etc-in-java&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    */&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;String hmacDigest(String msg&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String keyString&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String algo) {
        String digest = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;try &lt;/span&gt;{
            SecretKeySpec key = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;SecretKeySpec((keyString).getBytes(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;UTF-8&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;algo)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;Mac mac = Mac.&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(algo)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;mac.init(key)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;byte&lt;/span&gt;[] bytes = mac.doFinal(msg.getBytes(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;ASCII&quot;&lt;/span&gt;))&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;StringBuffer hash = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;StringBuffer()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;; &lt;/span&gt;i &amp;lt; bytes.&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;; &lt;/span&gt;i++) {
                String hex = Integer.&lt;span style=&quot;font-style: italic;&quot;&gt;toHexString&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;0xFF &lt;/span&gt;&amp;amp; bytes[i])&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;                &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(hex.length() == &lt;span style=&quot;color: blue;&quot;&gt;1&lt;/span&gt;) {
                    hash.append(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&#39;0&#39;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;                &lt;/span&gt;}
                hash.append(hex)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;}
            digest = hash.toString()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;} &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(UnsupportedEncodingException e) {
        } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(InvalidKeyException e) {
        } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(NoSuchAlgorithmException e) {
        }
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;digest&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}


}&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
The method &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;String hmacDigest(String msg&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String keyString&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String algo)&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
is used to calculate hmac. That method is used to check whether authentication is successful by using this method. &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public boolean &lt;/span&gt;authenticationSuccessful(String dataForHmac&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String authHeader)&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Client has class &lt;b&gt;HmacUtil.java&lt;/b&gt; to calculate hmac before send it to the server. &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;package &lt;/span&gt;com.blogspot.nipunswritings.hiname&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;java.io.UnsupportedEncodingException&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;java.security.InvalidKeyException&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;java.security.NoSuchAlgorithmException&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;javax.crypto.Mac&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;import &lt;/span&gt;javax.crypto.spec.SecretKeySpec&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/**&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; * Created by nipun on 7/25/15.&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; */&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;HmacUtil {

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;KEY &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;E34swDN1Fk1G44zmw9bL2N5B8R85w290&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private static &lt;/span&gt;String &lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ALGO &lt;/span&gt;= &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;HmacSHA1&quot;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    *&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    * This method is taken from&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    * http://www.supermind.org/blog/1102/generating-hmac-md5-sha1-sha256-etc-in-java&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    * but parameters have change here&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    * */&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;    &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static &lt;/span&gt;String hmacDigest(String msg) {
        String digest = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;try &lt;/span&gt;{
            SecretKeySpec key = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;SecretKeySpec((&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;KEY&lt;/span&gt;).getBytes(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;UTF-8&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ALGO&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;Mac mac = Mac.&lt;span style=&quot;font-style: italic;&quot;&gt;getInstance&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-style: italic;&quot;&gt;ALGO&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;mac.init(key)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;byte&lt;/span&gt;[] bytes = mac.doFinal(msg.getBytes(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;ASCII&quot;&lt;/span&gt;))&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;StringBuffer hash = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;StringBuffer()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;            for &lt;/span&gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;int &lt;/span&gt;i = &lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;; &lt;/span&gt;i &amp;lt; bytes.&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;; &lt;/span&gt;i++) {
                String hex = Integer.&lt;span style=&quot;font-style: italic;&quot;&gt;toHexString&lt;/span&gt;(&lt;span style=&quot;color: blue;&quot;&gt;0xFF &lt;/span&gt;&amp;amp; bytes[i])&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;                if &lt;/span&gt;(hex.length() == &lt;span style=&quot;color: blue;&quot;&gt;1&lt;/span&gt;) {
                    hash.append(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&#39;0&#39;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;                &lt;/span&gt;}
                hash.append(hex)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;}
            digest = hash.toString()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;} &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(UnsupportedEncodingException e) {
        } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(InvalidKeyException e) {
        } &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(NoSuchAlgorithmException e) {
        }
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;digest&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}

}&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Android client use below method to calculate hmac&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public static &lt;/span&gt;String hmacDigest(String msg) &lt;/pre&gt;
&lt;h4 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
How to read http authorization header by using&amp;nbsp; HttpServletRequest instance &lt;/h4&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;@ApiMethod&lt;/span&gt;(name = &lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;sayHi&quot;&lt;/span&gt;)
&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public &lt;/span&gt;MyBean sayHi(HttpServletRequest request&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Named&lt;/span&gt;(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;name&quot;&lt;/span&gt;) String name) &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;throws&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                                                                        HmacMismatchException{

    String authHeader = request.getHeader(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Authorization&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;    AuthManager authManager = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;AuthManager()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;    String digestData = request.getMethod()+&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;+/myApi/v1/sayHi/&quot;&lt;/span&gt;+name&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if &lt;/span&gt;(!authManager.authenticationSuccessful(digestData&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;authHeader)){
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;throw new &lt;/span&gt;HmacMismatchException()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}

    MyBean response = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;MyBean()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;response.setData(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Hi, &quot; &lt;/span&gt;+ name)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;response&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;}&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
It has added new parameter with the type &lt;b&gt;HttpServletRequest&lt;/b&gt; and it has name &lt;b&gt;request&lt;/b&gt;. Developers can use &lt;b&gt;getHeader()&lt;/b&gt; to get http header. After getting &lt;b&gt;Authorization&lt;/b&gt; header data for hmac generation is assigned to digestData. Then it is passed with &lt;b&gt;Authorization&lt;/b&gt; header value to&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public boolean &lt;/span&gt;authenticationSuccessful(String dataForHmac&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String authHeader) &lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
of &lt;b&gt;AuthManager&lt;/b&gt;. If the authentication is not successful Endpoint send &lt;b&gt;HmacMismatchException&lt;/b&gt; to client. &lt;b&gt;HmacMismatchException.java&lt;/b&gt; is shown below. &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;package &lt;/span&gt;com.example.nipun.myapplication.backend.auth&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;/**&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; * Created by nipun on 7/25/15.&lt;/span&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt; */&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: grey; font-style: italic;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public class &lt;/span&gt;HmacMismatchException &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;Exception {

    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public &lt;/span&gt;HmacMismatchException(){
        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;super&lt;/span&gt;(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;Can&#39;t produce received hmac&quot;&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}
}&lt;/pre&gt;
&lt;h4 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
How to set HMAC to authorization header by using Google Cloud Endpoint client library for Android&lt;/h4&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Android client in this blog post send a string,a name, to endpoint. Android user interface use EditText and Button to get user data. When button is clicked EndpointsAsyncTask is executed. It&#39;s provided name of the user. &lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Below method is called when a user touch the button.&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;sendName(View view){
    String name = &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;nameEditText&lt;/span&gt;.getText().toString()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;EndpointsAsyncTask().execute(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;Pair&amp;lt;Context&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt;(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;name))&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;}&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;b&gt;Code for&amp;nbsp; EndpointsAsyncTask with http headers setting&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;class &lt;/span&gt;EndpointsAsyncTask &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;extends &lt;/span&gt;AsyncTask&amp;lt;Pair&amp;lt;Context&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;Void&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt; {
    &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;MyApi &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService &lt;/span&gt;= &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;private &lt;/span&gt;Context &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected &lt;/span&gt;String doInBackground(Pair&amp;lt;Context&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;String&amp;gt;... params) {

        &lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;context &lt;/span&gt;= params[&lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;].&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;first&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;final &lt;/span&gt;String name = params[&lt;span style=&quot;color: blue;&quot;&gt;0&lt;/span&gt;].&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;second&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;if&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService &lt;/span&gt;== &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;) {  
            MyApi.Builder builder = &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;MyApi.Builder(AndroidHttp.&lt;span style=&quot;font-style: italic;&quot;&gt;newCompatibleTransport&lt;/span&gt;()&lt;span style=&quot;color: #cc7832;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;                new &lt;/span&gt;AndroidJsonFactory()&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;null&lt;/span&gt;)

                    .setRootUrl(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;http://10.0.2.2:8080/_ah/api/&quot;&lt;/span&gt;)
                    .setGoogleClientRequestInitializer(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;new &lt;/span&gt;GoogleClientRequestInitializer() {
                        &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: olive;&quot;&gt;                        &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;initialize(AbstractGoogleClientRequest&amp;lt;?&amp;gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                                           abstractGoogleClientRequest) &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;throws &lt;/span&gt;IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                            HttpHeaders httpHeaders =&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                                         abstractGoogleClientRequest.getRequestHeaders()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                            httpHeaders.setAuthorization(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;hmac &quot;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;                               &amp;nbsp;&lt;/span&gt;+&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;public:&quot;&lt;/span&gt;+HmacUtil.&lt;span style=&quot;font-style: italic;&quot;&gt;hmacDigest&lt;/span&gt;(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;POST+/myApi/v1/sayHi/&quot;&lt;/span&gt;+&lt;span style=&quot;color: #660e7a;&quot;&gt;name&lt;/span&gt;))&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;                            &lt;/span&gt;abstractGoogleClientRequest.setRequestHeaders(httpHeaders)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                        }
                    })&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;            &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService &lt;/span&gt;= builder.build()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;}

        &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;try &lt;/span&gt;{
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;myApiService&lt;/span&gt;.sayHi(name).execute().getData()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;} &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;catch &lt;/span&gt;(IOException e) {
            &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;return &lt;/span&gt;e.getMessage()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;        &lt;/span&gt;}
    }

    &lt;span style=&quot;color: olive;&quot;&gt;@Override&lt;/span&gt;&lt;span style=&quot;color: olive;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;protected void &lt;/span&gt;onPostExecute(String result) {
        Toast.&lt;span style=&quot;font-style: italic;&quot;&gt;makeText&lt;/span&gt;(&lt;span style=&quot;color: #660e7a; font-weight: bold;&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;result&lt;span style=&quot;color: #cc7832;&quot;&gt;, &lt;/span&gt;Toast.&lt;span style=&quot;color: #660e7a; font-style: italic; font-weight: bold;&quot;&gt;LENGTH_LONG&lt;/span&gt;).show()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;}
}&lt;/pre&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
Code to set Authorization header is this.&lt;/div&gt;
&lt;div style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;public void &lt;/span&gt;initialize(AbstractGoogleClientRequest&amp;lt;?&amp;gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                                  abstractGoogleClientRequest) &lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;throws &lt;/span&gt;IOException {
    abstractGoogleClientRequest.setDisableGZipContent(&lt;span style=&quot;color: navy; font-weight: bold;&quot;&gt;true&lt;/span&gt;)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;    HttpHeaders httpHeaders = abstractGoogleClientRequest.getRequestHeaders()&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;httpHeaders.setAuthorization(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;hmac &quot;&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;                      +&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;public:&quot;&lt;/span&gt;+HmacUtil.&lt;span style=&quot;font-style: italic;&quot;&gt;hmacDigest&lt;/span&gt;(&lt;span style=&quot;color: green; font-weight: bold;&quot;&gt;&quot;POST+/myApi/v1/sayHi/&quot;&lt;/span&gt;+&lt;span style=&quot;color: #660e7a;&quot;&gt;name&lt;/span&gt;))&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;background-color: white; color: black; font-family: &#39;DejaVu Sans Mono&#39;; font-size: 9.0pt;&quot;&gt;&lt;span style=&quot;color: #cc7832;&quot;&gt;    &lt;/span&gt;abstractGoogleClientRequest.setRequestHeaders(httpHeaders)&lt;span style=&quot;color: #cc7832;&quot;&gt;;&lt;/span&gt;} &lt;/pre&gt;
&lt;h3 style=&quot;line-height: 100%; margin-bottom: 0in;&quot;&gt;
References&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&amp;nbsp;https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints&lt;/li&gt;
&lt;li&gt;http://stackoverflow.com/questions/15056830/getting-raw-http-data-headers-cookies-etc-in-google-cloud-endpoints&lt;/li&gt;
&lt;li&gt;http://www.supermind.org/blog/1102/generating-hmac-md5-sha1-sha256-etc-in-java&lt;/li&gt;
&lt;li&gt;http://restcookbook.com/Basics/loggingin/&lt;/li&gt;
&lt;/ol&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/1587337933300925120/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2015/07/securing-google-cloud-endpoints-api-for.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1587337933300925120'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1587337933300925120'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2015/07/securing-google-cloud-endpoints-api-for.html' title='Securing Google Cloud Endpoints API for Android App With HMAC'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-5193397421185083652</id><published>2015-07-21T13:40:00.000-07:00</published><updated>2015-07-21T13:42:21.307-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Development Problems"/><category scheme="http://www.blogger.com/atom/ns#" term="Cloud Computing"/><category scheme="http://www.blogger.com/atom/ns#" term="Code Snippets"/><category scheme="http://www.blogger.com/atom/ns#" term="Google Cloud Endpoints"/><title type='text'>Include HttpHeaders to Google Cloud API call request made from Android</title><content type='html'>When making Cloud Endpoint API call request from the Android client sometimes it&#39;s needed to add Http Headers to that request. This blog post say how to add Date http header to request that calls myApi.sayHi() method which is described&amp;nbsp;&lt;a href=&quot;https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. Read the code of EndpointsAsyncTask class. The code where http headers must be set is shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new AndroidJsonFactory(), null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // options for running against local devappserver&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // - 10.0.2.2 is localhost&#39;s IP address in Android emulator&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // - turn off compression when running against local devappserver&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .setRootUrl(&quot;http://10.0.2.2:8080/_ah/api/&quot;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void initialize(AbstractGoogleClientRequest&amp;lt;?&amp;gt; abstractGoogleClientRequest) throws IOException {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; abstractGoogleClientRequest.setDisableGZipContent(true);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // end options for devappserver&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myApiService = builder.build();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now add the Date http header as below. The bold text are the code that is used to add http header to request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new AndroidJsonFactory(), null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // options for running against local devappserver&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // - 10.0.2.2 is localhost&#39;s IP address in Android emulator&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // - turn off compression when running against local devappserver&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .setRootUrl(&quot;http://10.0.2.2:8080/_ah/api/&quot;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @Override&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void initialize(AbstractGoogleClientRequest&amp;lt;?&amp;gt; abstractGoogleClientRequest) throws IOException {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; abstractGoogleClientRequest.setDisableGZipContent(true);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Calendar calendar = Calendar.getInstance();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SimpleDateFormat dateFormat = new SimpleDateFormat(&quot;EEE, dd MMM yyyy HH:mm:ss z&quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpHeaders headers = abstractGoogleClientRequest.getRequestHeaders();&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Log.d(TAG, &quot;Date: &quot;+dateFormat.format(calendar.getTime()));&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headers.setDate(dateFormat.format(calendar.getTime()));&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; abstractGoogleClientRequest.setRequestHeaders(headers);&lt;/b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myApiService = builder.build();</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/5193397421185083652/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2015/07/include-httpheaders-to-google-cloud-api.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5193397421185083652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5193397421185083652'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2015/07/include-httpheaders-to-google-cloud-api.html' title='Include HttpHeaders to Google Cloud API call request made from Android'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-8750500233936424121</id><published>2015-06-21T11:27:00.000-07:00</published><updated>2015-06-21T11:27:40.299-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android Studio"/><category scheme="http://www.blogger.com/atom/ns#" term="Cloud Computing"/><category scheme="http://www.blogger.com/atom/ns#" term="Google Cloud Endpoints"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="MBaaS"/><category scheme="http://www.blogger.com/atom/ns#" term="Mobile Backend as a service"/><title type='text'>Google Cloud Endpoints: Creating New Endpoint</title><content type='html'>This blog post is about using Google cloud endpoint and Adding new endpoint to existing backend project by using android studio. In order to learn more about Google cloud endpoints with Android use the references.&lt;br /&gt;
&lt;h3&gt;
Integrate Cloud Endpoint Module To Project&lt;/h3&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;File &amp;gt; New Module&lt;/li&gt;
&lt;li&gt;Under &lt;b&gt;Choose Module Type&lt;/b&gt; select &lt;b&gt;Google Cloud Module&lt;/b&gt; and click &lt;b&gt;Next&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;In next screen, select &lt;b&gt;App Engine Java Endpoint Module&lt;/b&gt; for the &lt;b&gt;Module type&lt;/b&gt;, give a &lt;b&gt;Module Name&lt;/b&gt; and a &lt;b&gt;Package name&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;Click Finish&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
After Gradle sync finish new module can be seen with the name that has been given as Module Name. Google Cloud Endpoint module has example with MyBean.java, MyEndpoint.java classes. Select &amp;nbsp;Cloud endpoint module in run configuration and click run button. This endpoint can be seen in API Explorer.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
After Android Studio Console display “INFO: Dev App Server is now running”.&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Go to this http://localhost:8080/&lt;/li&gt;
&lt;li&gt;Click on Google Cloud Endpoints API Explorer, It ll direct you to this link&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;https://apis-explorer.appspot.com/apis-explorer/?base=http%3A%2F%2Flocalhost%3A8080%2F_ah%2Fapi#p/&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;Above link is the address of API Explorer for the services that is running on the localhost&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;If services are not displayed click on the shield on Chromes address bar&lt;/li&gt;
&lt;li&gt;It ll show a pop up with message “This page is trying to load scripts from unauthorized sources”, click on load unsafe scripts.&lt;/li&gt;
&lt;li&gt;After that myApi API can be seen under services&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;API can be tested by clicking on the API.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
Adding A New Endpoint&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
New endpoint is used to calculate sum of two floats. So This API need two java classes they are CalculatorEndpoint.java and ResultBean.Java. Both are under same package of Cloud Module.&lt;/div&gt;
&lt;h4&gt;
ResultBean.java&lt;/h4&gt;
&lt;div&gt;
&lt;div&gt;
package com.example.nipun.myapplication.backend;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
/**&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;* Created by nipun on 6/21/15.&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;*/&lt;/div&gt;
&lt;div&gt;
public class ResultBean {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; float result;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; public float getResult() {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return result;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; public void setResult(float result) {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.result = result;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;
CalculatorEndpoint.java&lt;/h4&gt;
&lt;div&gt;
&lt;div&gt;
package com.example.nipun.myapplication.backend;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
/**&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;* Created by nipun on 6/21/15.&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;*/&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
import com.google.api.server.spi.config.Api;&lt;/div&gt;
&lt;div&gt;
import com.google.api.server.spi.config.ApiMethod;&lt;/div&gt;
&lt;div&gt;
import com.google.api.server.spi.config.ApiNamespace;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
import javax.inject.Named;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
@Api(name = &quot;calculatorApi&quot;,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; version = &quot;v1&quot;,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; namespace = @ApiNamespace(&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ownerDomain = &quot;backend.myapplication.nipun.example.com&quot;,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ownerName = &quot;backend.myapplication.nipun.example.com&quot;,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; packagePath = &quot;&quot;))&lt;/div&gt;
&lt;div&gt;
public class CalculatorEndpoint {&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; @ApiMethod(name = &quot;addTwoNumbers&quot;)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; public ResultBean addTwoNumbers(@Named(&quot;num1&quot;) float num1, @Named(&quot;num2&quot;) float num2){&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; float result = num1 + num2;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ResultBean resultBean = new ResultBean();&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; resultBean.setResult(result);&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return resultBean;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In order to use &lt;b&gt;calculatorApi&lt;/b&gt;&amp;nbsp;add fully qualified class name of &amp;nbsp;CalculatorEndpoint.java to the &lt;b&gt;web.xml&lt;/b&gt; file of backend module as below. web.xml can be located in&amp;nbsp;&lt;b&gt;&amp;lt;backend_module_name&amp;gt;/src/main/webapp/WEB-INF/web.xml&lt;/b&gt;. backend_module_name is the name of the Google Cloud Module.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;servlet&amp;gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;servlet-name&amp;gt;SystemServiceServlet&amp;lt;/servlet-name&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;servlet-class&amp;gt;com.google.api.server.spi.SystemServiceServlet&amp;lt;/servlet-class&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;init-param&amp;gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;param-name&amp;gt;services&amp;lt;/param-name&amp;gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;param-value&amp;gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; com.example.nipun.myapplication.backend.MyEndpoint,&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; com.example.nipun.myapplication.backend.CalculatorEndpoint&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/param-value&amp;gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/init-param&amp;gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/servlet&amp;gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Run the cloud backend module again and go to API explorer. Now two numbers can be added with calculatorApi.&lt;/div&gt;
&lt;h3&gt;
References&lt;/h3&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/8750500233936424121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2015/06/google-cloud-endpoints-creating-new.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8750500233936424121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8750500233936424121'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2015/06/google-cloud-endpoints-creating-new.html' title='Google Cloud Endpoints: Creating New Endpoint'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-8257711698721327962</id><published>2015-06-20T02:46:00.000-07:00</published><updated>2015-06-20T02:52:01.604-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Development Problems"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Studio"/><title type='text'>Rendering Problems: The following class could not found</title><content type='html'>&lt;div&gt;
&lt;h3&gt;
&lt;b&gt;
Problem&lt;/b&gt;&lt;/h3&gt;
&lt;/div&gt;
In android studio, after a project was created a message was displayed with following content.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;The following class could not found: android.support.v7.internal.widget.actionbaroverlaylayout&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5Lcv5QoujKEjXUtvavQALMh3ehrMrDkgSyfMMoP0DoEU9-eb-N19E6AzjgR2u052t9scTKUXqHpxlmAPmWaZVA_BZYjhyldm317a56silJrtZZ-ljopn1OOUTM0Hh94fc_BOU1E1RSqMb/s1600/Screenshot+from+2015-06-14+14%253A16%253A40.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;260&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5Lcv5QoujKEjXUtvavQALMh3ehrMrDkgSyfMMoP0DoEU9-eb-N19E6AzjgR2u052t9scTKUXqHpxlmAPmWaZVA_BZYjhyldm317a56silJrtZZ-ljopn1OOUTM0Hh94fc_BOU1E1RSqMb/s400/Screenshot+from+2015-06-14+14%253A16%253A40.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Solution&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Buld &amp;gt; Rebuild Project&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Go to build menu then rebuild the project.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/8257711698721327962/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2015/06/rendering-problems-following-class.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8257711698721327962'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/8257711698721327962'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2015/06/rendering-problems-following-class.html' title='Rendering Problems: The following class could not found'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5Lcv5QoujKEjXUtvavQALMh3ehrMrDkgSyfMMoP0DoEU9-eb-N19E6AzjgR2u052t9scTKUXqHpxlmAPmWaZVA_BZYjhyldm317a56silJrtZZ-ljopn1OOUTM0Hh94fc_BOU1E1RSqMb/s72-c/Screenshot+from+2015-06-14+14%253A16%253A40.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-7009899446069148953</id><published>2015-04-22T03:18:00.001-07:00</published><updated>2015-06-20T02:50:32.344-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Android"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Development Problems"/><category scheme="http://www.blogger.com/atom/ns#" term="Android Virtual Device (AVD)"/><category scheme="http://www.blogger.com/atom/ns#" term="AVD Error"/><title type='text'>NAND: could not write file Error</title><content type='html'>&lt;b&gt;Error:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;NAND: could not write file /tmp/android-nipun/emulator-kOjnli, File exists&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The hard-disk partition which has /tmp folder had not enough space when the Android virtual device (AVD) was started. Making free space that is more than needed by AVD resolve this error.</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/7009899446069148953/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2015/04/nand-could-not-write-file-error.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/7009899446069148953'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/7009899446069148953'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2015/04/nand-could-not-write-file-error.html' title='NAND: could not write file Error'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-1331202784901586335</id><published>2014-07-23T03:12:00.000-07:00</published><updated>2014-07-23T03:12:03.049-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Snippets"/><category scheme="http://www.blogger.com/atom/ns#" term="iOS"/><category scheme="http://www.blogger.com/atom/ns#" term="iOS7"/><category scheme="http://www.blogger.com/atom/ns#" term="Objective C"/><title type='text'>How To Hide Status Bar In iOS 7</title><content type='html'>- (BOOL)prefersStatusBarHidden&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return YES;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Write above method on every UIViewController class which has a UIView to show without status bar.</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/1331202784901586335/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2014/07/how-to-hide-status-bar-in-ios-7.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1331202784901586335'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/1331202784901586335'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2014/07/how-to-hide-status-bar-in-ios-7.html' title='How To Hide Status Bar In iOS 7'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-2599621209105532363</id><published>2014-06-10T22:04:00.003-07:00</published><updated>2014-06-12T20:45:17.941-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="NSDate"/><category scheme="http://www.blogger.com/atom/ns#" term="NSDateFormatter"/><category scheme="http://www.blogger.com/atom/ns#" term="NSString"/><title type='text'>A Code Snippet For Converting NSDate To NSString With NSDateFormatter</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
When working on programs that manipulate Dates sometimes those days need to be store &amp;amp; printed in particular format. iOS has given &lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/init&quot; target=&quot;_blank&quot;&gt;NSDate&lt;/a&gt;&amp;nbsp;for date and time related data handling and &amp;nbsp;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html&quot; target=&quot;_blank&quot;&gt;NSDateFormatter&lt;/a&gt;&amp;nbsp;can be used to give a date of NSDate object a visual form.&amp;nbsp;&lt;a href=&quot;https://www.blogger.com/&quot;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Code&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Current Date and Time is obtained by &lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/init&quot; target=&quot;_blank&quot;&gt;init&lt;/a&gt;&amp;nbsp;method of NSDate.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;NSDate&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;&amp;nbsp;*date = [[&lt;/span&gt;&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;NSDate&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;alloc&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;]&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;];&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Create a&amp;nbsp;NSDateFormatter for set the date format that is needed to print and get a NSString object from the &lt;i style=&quot;font-weight: bold;&quot;&gt;date.&lt;/i&gt;&lt;br /&gt;
&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;NSDateFormatter&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Menlo; font-size: 11px;&quot;&gt;&amp;nbsp;*dateFormatter = [[&lt;/span&gt;&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;NSDateFormatter&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Menlo; font-size: 11px;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;alloc&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Menlo; font-size: 11px;&quot;&gt;]&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Menlo; font-size: 11px;&quot;&gt;];&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Use &lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/setDateFormat:&quot; target=&quot;_blank&quot;&gt;setDateFormat&lt;/a&gt;&amp;nbsp;of NSDateFormatter to get a date format that is needed to display. This method get the required date format by using NSString parameter and returns nothing. &amp;nbsp;The method has been called on the object &lt;b&gt;&lt;i&gt;dateFormatter&lt;/i&gt;&lt;/b&gt;.&lt;br /&gt;
&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;[dateFormatter&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;setDateFormat&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;@&quot;dd-MM-YYYY HH:mm:ss&quot;&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;];&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Method signature for setDateFormat&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
- (void)setDateFormat:(NSString *)string&lt;br /&gt;
&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;NSString&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;&amp;nbsp;*dateString = [dateFormatter&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;stringFromDate&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;:date];&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Invoke&amp;nbsp;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateFormatter/stringFromDate:&quot; target=&quot;_blank&quot;&gt;stringFromDate&lt;/a&gt;&amp;nbsp;on&amp;nbsp;&lt;b&gt;&lt;i&gt;dateFormatter&lt;/i&gt;&lt;/b&gt;&amp;nbsp;object to get the the date value of NSDate object in specified date format. This method consumes previously created &lt;b&gt;&lt;i&gt;date&lt;/i&gt;&lt;/b&gt; object and assign a NSString represent of the date on &lt;i style=&quot;font-weight: bold;&quot;&gt;date &lt;/i&gt;object to &lt;b&gt;dateString&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Method signature for&amp;nbsp;stringFromDate&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
- (NSString *)stringFromDate:(NSDate *)date&lt;br /&gt;
&lt;br /&gt;
Print the &lt;b&gt;&lt;i&gt;dateString&lt;/i&gt;&lt;/b&gt;&amp;nbsp;using &amp;nbsp;NSLog();&lt;br /&gt;
&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;NSLog&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;@&quot;The Date: %@&quot;&lt;/span&gt;&lt;span style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;, dateString);&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Complete Code&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #703daa;&quot;&gt;NSDate&lt;/span&gt; *date = [[&lt;span style=&quot;color: #703daa;&quot;&gt;NSDate&lt;/span&gt; &lt;span style=&quot;color: #3d1d81;&quot;&gt;alloc&lt;/span&gt;] &lt;span style=&quot;color: #3d1d81;&quot;&gt;init&lt;/span&gt;];&lt;/div&gt;
&lt;div style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;NSDateFormatter&lt;span style=&quot;color: black;&quot;&gt; *dateFormatter = [[&lt;/span&gt;NSDateFormatter&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #3d1d81;&quot;&gt;alloc&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #3d1d81;&quot;&gt;init&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; [dateFormatter &lt;span style=&quot;color: #3d1d81;&quot;&gt;setDateFormat&lt;/span&gt;:&lt;span style=&quot;color: #d12f1b;&quot;&gt;@&quot;dd-MM-YYYY HH:mm:ss&quot;&lt;/span&gt;];&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #703daa;&quot;&gt;NSString&lt;/span&gt; *dateString = [dateFormatter &lt;span style=&quot;color: #3d1d81;&quot;&gt;stringFromDate&lt;/span&gt;:date];&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #3d1d81;&quot;&gt;NSLog&lt;/span&gt;(&lt;span style=&quot;color: #d12f1b;&quot;&gt;@&quot;The Date: %@&quot;&lt;/span&gt;, dateString);&lt;/div&gt;

&lt;a href=&quot;http://www.codeproject.com/script/Articles/BlogArticleList.aspx?amid=10881584&quot; rel=&quot;tag&quot; style=&quot;display:none&quot;&gt;CodeProject&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/2599621209105532363/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2014/06/a-code-snippet-for-converting-nsdate-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/2599621209105532363'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/2599621209105532363'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2014/06/a-code-snippet-for-converting-nsdate-to.html' title='A Code Snippet For Converting NSDate To NSString With NSDateFormatter'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-5740049052974015552</id><published>2014-04-07T06:28:00.000-07:00</published><updated>2014-04-07T06:28:51.517-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="iOS"/><category scheme="http://www.blogger.com/atom/ns#" term="Objective C"/><title type='text'>isDescendantOfView: To check whether UIView is a subview of another UIView in iOS</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
Sometimes it&#39;s needed to check whether a UIView is a subview of another view. A typical use case is show and hide UIViews with button click events. it&#39;s better if the code check whether a UIView is a sub view of another view before the view is removed from the super view. UIView class have method &amp;nbsp;&quot;&lt;b&gt;isDescendantOfView&lt;/b&gt;&quot; [1] it returns &quot;&lt;b&gt;YES&lt;/b&gt;&quot; if the specified UIView is a subview of another UIView if it&#39;s not it returns &quot;&lt;b&gt;NO&lt;/b&gt;&quot;.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
In example code below, &quot;&lt;span style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;I m in&lt;/span&gt;&quot; is printed if the&amp;nbsp;&lt;span style=&quot;color: #4f8187; font-family: Menlo; font-size: 11px;&quot;&gt;theSubView&lt;/span&gt;&amp;nbsp;is a sub view of the UIView&amp;nbsp;&lt;span style=&quot;color: #bb2ca2; font-family: Menlo; font-size: 11px;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: black; font-family: Menlo; font-size: 11px;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #703daa; font-family: Menlo; font-size: 11px;&quot;&gt;view&lt;/span&gt;&amp;nbsp;otherwise it prints &quot;&lt;span style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;I m out&lt;/span&gt;&quot;.&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;div style=&quot;color: #4f8187;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4f8187;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #4f8187;&quot;&gt;
&lt;span style=&quot;color: #bb2ca2;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt; ([&lt;/span&gt;theSubView&lt;span style=&quot;color: black;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #3d1d81;&quot;&gt;isDescendantOfView&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #bb2ca2;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #703daa;&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;]) {&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #3d1d81;&quot;&gt;NSLog&lt;/span&gt;(&lt;span style=&quot;color: #d12f1b;&quot;&gt;@&quot;I m in&quot;&lt;/span&gt;);&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; }&lt;span style=&quot;color: #bb2ca2;&quot;&gt;else&lt;/span&gt;{&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #3d1d81;&quot;&gt;NSLog&lt;/span&gt;(&lt;span style=&quot;color: #d12f1b;&quot;&gt;@&quot;I m out&quot;&lt;/span&gt;);&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;b&gt;Reference&lt;/b&gt;:&lt;br /&gt;
[1].&amp;nbsp;&lt;a href=&quot;https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/isDescendantOfView:&quot; target=&quot;_blank&quot;&gt;http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/isDescendantOfView:&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/5740049052974015552/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2014/04/isdescendantofview-to-check-whether.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5740049052974015552'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5740049052974015552'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2014/04/isdescendantofview-to-check-whether.html' title='isDescendantOfView: To check whether UIView is a subview of another UIView in iOS'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4976004055150276620.post-5790133168477608869</id><published>2014-03-24T04:44:00.002-07:00</published><updated>2014-04-08T02:15:36.795-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="iOS"/><category scheme="http://www.blogger.com/atom/ns#" term="NSData"/><category scheme="http://www.blogger.com/atom/ns#" term="NSString"/><category scheme="http://www.blogger.com/atom/ns#" term="Objective C"/><title type='text'>Converting NSString to NSData and NSData to NSString</title><content type='html'>&lt;h4&gt;
Description&lt;/h4&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Below code segment shows how to convert a NSString value to NSData and NSData value to NSString value. At the beginning, It create a NSString literal with the variable name &quot;firstString&quot; then value of &quot;firstString&quot; variable is taken as a way that can be assigned into NSData variable by using method [1]. The variable &quot;firstString&quot; is assigned with &quot;nil&quot; in order to show that the variable contains nothing. The value of the variable has printed to by using &quot;NSLog&quot;. After the printing a NSString object is created with by using previously created NSData. Method [2] has been used. Then the &quot;firstString&quot; value is printed again with NSLog.&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;- (&lt;/span&gt;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSData&quot; style=&quot;background-color: white; color: #3366cc; font-family: Courier, Consolas, monospace; font-size: 13px; text-decoration: none;&quot;&gt;NSData&lt;/a&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;&amp;nbsp;*)dataUsingEncoding:(&lt;/span&gt;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSStringEncoding&quot; style=&quot;background-color: white; color: #3366cc; font-family: Courier, Consolas, monospace; font-size: 13px; text-decoration: none;&quot;&gt;NSStringEncoding&lt;/a&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;)&lt;/span&gt;&lt;em style=&quot;background-color: white; font-family: &#39;Lucida Grande&#39;, &#39;Lucida Sans Unicode&#39;, Helvetica, Arial, Verdana, sans-serif; font-size: 13px;&quot;&gt;encoding&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;- (&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;instancetype&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;)initWithData:(&lt;/span&gt;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSData&quot; style=&quot;background-color: white; color: #3366cc; font-family: Courier, Consolas, monospace; font-size: 13px; text-decoration: none;&quot;&gt;NSData&lt;/a&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;&amp;nbsp;*)&lt;/span&gt;&lt;em style=&quot;background-color: white; font-family: &#39;Lucida Grande&#39;, &#39;Lucida Sans Unicode&#39;, Helvetica, Arial, Verdana, sans-serif; font-size: 13px;&quot;&gt;data&lt;/em&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;&amp;nbsp;encoding:(&lt;/span&gt;&lt;a href=&quot;https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/c_ref/NSStringEncoding&quot; style=&quot;background-color: white; color: #3366cc; font-family: Courier, Consolas, monospace; font-size: 13px; text-decoration: none;&quot;&gt;NSStringEncoding&lt;/a&gt;&lt;span style=&quot;background-color: white; font-family: Courier, Consolas, monospace; font-size: 13px;&quot;&gt;)&lt;/span&gt;&lt;em style=&quot;background-color: white; font-family: &#39;Lucida Grande&#39;, &#39;Lucida Sans Unicode&#39;, Helvetica, Arial, Verdana, sans-serif; font-size: 13px;&quot;&gt;encoding&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;
Code&lt;/h4&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #703daa;&quot;&gt;NSString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp;*firstString =&amp;nbsp;&lt;/span&gt;@&quot;This is the string that is wanted to convert&quot;&lt;span style=&quot;color: black;&quot;&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #703daa;&quot;&gt;NSData&lt;/span&gt;&amp;nbsp;*dataFromString = [firstString&amp;nbsp;&lt;span style=&quot;color: #3d1d81;&quot;&gt;dataUsingEncoding&lt;/span&gt;:&lt;span style=&quot;color: #3d1d81;&quot;&gt;NSUTF8StringEncoding&lt;/span&gt;];&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&amp;nbsp; &amp;nbsp; firstString =&amp;nbsp;&lt;span style=&quot;color: #bb2ca2;&quot;&gt;nil&lt;/span&gt;;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81;&quot;&gt;NSLog&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;@&quot;firstString (nil) = %@&quot;&lt;span style=&quot;color: black;&quot;&gt;, firstString);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #3d1d81; font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp; firstString = [[&lt;/span&gt;&lt;span style=&quot;color: #703daa;&quot;&gt;NSString&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp;&lt;/span&gt;alloc&lt;span style=&quot;color: black;&quot;&gt;]&amp;nbsp;&lt;/span&gt;initWithData&lt;span style=&quot;color: black;&quot;&gt;:dataFromString&amp;nbsp;&lt;/span&gt;encoding&lt;span style=&quot;color: black;&quot;&gt;:&lt;/span&gt;NSUTF8StringEncoding&lt;span style=&quot;color: black;&quot;&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px; min-height: 13px;&quot;&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #d12f1b; font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #3d1d81;&quot;&gt;NSLog&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;(&lt;/span&gt;@&quot;firstString (!nil) = %@&quot;&lt;span style=&quot;color: black;&quot;&gt;, firstString);&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4&gt;
Result&lt;/h4&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;b&gt;2014-03-24 17:10:11.367 TestProject[3048:60b] firstString (nil) = (null)&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: Menlo; font-size: 11px;&quot;&gt;
&lt;b&gt;2014-03-24 17:10:11.369 TestProject[3048:60b] firstString (!nil) = This is the string that is wanted to convert&lt;/b&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://nipunswritings.blogspot.com/feeds/5790133168477608869/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://nipunswritings.blogspot.com/2014/03/converting-nsstring-to-nsdata-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5790133168477608869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4976004055150276620/posts/default/5790133168477608869'/><link rel='alternate' type='text/html' href='http://nipunswritings.blogspot.com/2014/03/converting-nsstring-to-nsdata-and.html' title='Converting NSString to NSData and NSData to NSString'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>