<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>telephonymanager Archives - Truiton</title>
	<atom:link href="https://www.truiton.com/tag/telephonymanager/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Technology Reaching Us In Time - Online</description>
	<lastBuildDate>Mon, 25 Nov 2019 11:12:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i0.wp.com/www.truiton.com/wp-content/uploads/2022/11/cropped-truiton_new_logo_half_white.png?fit=32%2C32&#038;ssl=1</url>
	<title>telephonymanager Archives - Truiton</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">54534495</site>	<item>
		<title>Android onSignalStrengthsChanged &#8211; LTE Strength Measurement</title>
		<link>https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/</link>
					<comments>https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sat, 16 Aug 2014 09:32:48 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[PhoneStateListener]]></category>
		<category><![CDATA[telephonymanager]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=668</guid>

					<description><![CDATA[<p>In this tutorial I would show how to measure the signal strengths in Android. Since Android is a mobile operating system, therefore measuring signal strengths from device itself would be a very easy task.&#160;Android onSignalStrengthsChanged is a method in Android PhoneStateListener class which observes the changes in telephony states of a device. In my previous&#8230;&#160;<a href="https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android onSignalStrengthsChanged &#8211; LTE Strength Measurement</span></a></p>
<p>The post <a href="https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/">Android onSignalStrengthsChanged &#8211; LTE Strength Measurement</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="550" height="367" data-attachment-id="673" data-permalink="https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/175837-verizon4g/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/175837-verizon4g.jpg?fit=550%2C367&amp;ssl=1" data-orig-size="550,367" data-comments-opened="1" data-image-title="175837-verizon4g" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/175837-verizon4g.jpg?fit=550%2C367&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/175837-verizon4g.jpg?resize=550%2C367" alt="Android onSignalStrengthsChanged" class="wp-image-673" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/175837-verizon4g.jpg?w=550&amp;ssl=1 550w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/175837-verizon4g.jpg?resize=300%2C200&amp;ssl=1 300w" sizes="(max-width: 550px) 100vw, 550px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">In this tutorial I would show how to measure the signal strengths in Android. Since Android is a mobile operating system, therefore measuring signal strengths from device itself would be a very easy task.&nbsp;Android onSignalStrengthsChanged is a method in Android <a title="Android PhoneStateListener" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html" target="_blank" rel="noopener noreferrer">PhoneStateListener</a> class which observes the changes in telephony states of a device. In my previous tutorials I&nbsp;utilized the power of&nbsp;Android PhoneStateListener class to determine the changes in&nbsp;message waiting indicator, service state, data activity and also to monitor the change in cell info.&nbsp;If you wish to read the full series, links are below:</p>



<p class="wp-block-paragraph"><span style="color: #000000;">1)&nbsp;</span><a style="color: #0090d3;" title="Android PhoneStateListener Example" href="http://www.truiton.com/2014/08/android-phonestatelistener-example/" target="_blank" rel="noopener noreferrer">Android PhoneStateListener Example</a><br style="color: #000000;"><span style="color: #000000;">2)&nbsp;</span><a style="color: #0090d3;" title="Android onDataConnectionStateChanged – Detect Network Type" href="http://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/" target="_blank" rel="noopener noreferrer">Android onDataConnectionStateChanged – Detect Network Type</a><br style="color: #000000;"><span style="color: #000000;">3) <a title="Android onSignalStrengthsChanged – LTE Strength Measurement" href="http://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/">Android onSignalStrengthsChanged – LTE Strength Measurement</a></span></p>



<p class="wp-block-paragraph">To listen the change in signal strengths a flag <code>PhoneStateListener.LISTEN_SIGNAL_STRENGTHS</code> is used in the <a title="Android TelephonyManager Listen" rel="noopener noreferrer" href="http://developer.android.com/reference/android/telephony/TelephonyManager.html#listen%28android.telephony.PhoneStateListener,%20int%29" target="_blank">listen</a> method of <code>TelphonyManager</code> class. By using this flag we will start getting the callbacks in <code>onSignalStrengthsChanged(SignalStrength signalStrength)</code> method whenever the signal strength of network changes.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">No Permissions needed</h2>



<p class="wp-block-paragraph">Now the interesting part here is that, to use the<span style="text-decoration: underline;"> Android onSignalStrengthsChanged</span> method we don&#8217;t need to add any permissions in our manifest. I tested this on a Kitkat 4.4.4 device.</p>



<p class="wp-block-paragraph">Lets start with PhoneStateListener Activity:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.phonestatelistener;

import android.support.v7.app.ActionBarActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class PhoneStateListenerActivity extends ActionBarActivity {
 TelephonyManager tManager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_phone_state_listener);

 tManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
 tManager.listen(new CustomPhoneStateListener(this),
 PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.phone_state_listener, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}</pre>



<p class="wp-block-paragraph">As you can see in the above class, a custom Android PhoneStateListener is used to capture the updates of signal strengths with the help of TelephonyManager class. as soon as the listener is registered, we start receiving callbacks in Android&nbsp;PhoneStateListener onSignalStrengthsChanged method.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android onSignalStrengthsChanged LTE measurement</h2>



<p class="wp-block-paragraph">LTE (Long-Term Evolution) is a new emerging standard for wireless communication. LTE is an upgrade for both GSM/UMTS and <a title="CDMA2000" href="http://en.wikipedia.org/wiki/CDMA2000" target="_blank" rel="noopener noreferrer">CDMA2000</a> Networks. Here the good part is, android supports LTE standard. With the help of PhoneStateListener and Android onSignalStrengthsChanged method we can measure LTE signal strengths. In this <a title="Android onSignalStrengthsChanged – LTE Strength Measurement" href="http://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/">Android onSignalStrengthsChanged &#8211; LTE Strength Measurement</a> tutorial I would show how to measure the signal strength for LTE network by using the Java Reflection APIs. Lets define the custom PhoneStateListener:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.phonestatelistener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.util.Log;

public class CustomPhoneStateListener extends PhoneStateListener {
 Context mContext;
 public static String LOG_TAG = "CustomPhoneStateListener";

 public CustomPhoneStateListener(Context context) {
 mContext = context;
 }

 /**
  * In this method Java Reflection API is being used please see link before
  * using.
  * 
  * @see &lt;a
  *      href="http://docs.oracle.com/javase/tutorial/reflect/">http://docs.oracle.com/javase/tutorial/reflect/&lt;/a>
  * 
  */
 @Override
 public void onSignalStrengthsChanged(SignalStrength signalStrength) {
 super.onSignalStrengthsChanged(signalStrength);
 Log.i(LOG_TAG, "onSignalStrengthsChanged: " + signalStrength);
 if (signalStrength.isGsm()) {
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getGsmBitErrorRate "
 + signalStrength.getGsmBitErrorRate());
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getGsmSignalStrength "
 + signalStrength.getGsmSignalStrength());
 } else if (signalStrength.getCdmaDbm() > 0) {
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getCdmaDbm "
 + signalStrength.getCdmaDbm());
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getCdmaEcio "
 + signalStrength.getCdmaEcio());
 } else {
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getEvdoDbm "
 + signalStrength.getEvdoDbm());
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getEvdoEcio "
 + signalStrength.getEvdoEcio());
 Log.i(LOG_TAG, "onSignalStrengthsChanged: getEvdoSnr "
 + signalStrength.getEvdoSnr());
 }

 // Reflection code starts from here
 try {
 Method[] methods = android.telephony.SignalStrength.class
 .getMethods();
 for (Method mthd : methods) {
 if (mthd.getName().equals("getLteSignalStrength")
 || mthd.getName().equals("getLteRsrp")
 || mthd.getName().equals("getLteRsrq")
 || mthd.getName().equals("getLteRssnr")
 || mthd.getName().equals("getLteCqi")) {
 Log.i(LOG_TAG,
 "onSignalStrengthsChanged: " + mthd.getName() + " "
 + mthd.invoke(signalStrength));
 }
 }
 } catch (SecurityException e) {
 e.printStackTrace();
 } catch (IllegalArgumentException e) {
 e.printStackTrace();
 } catch (IllegalAccessException e) {
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 e.printStackTrace();
 }
 // Reflection code ends here
 }
}</pre>



<p class="wp-block-paragraph">Before moving forward let me give a brief intro to Java Reflection API. Reflection API is used to examine or modify the run-time behavior of any application in running in virtual machine (DVM or ART). Since this is a very powerful API, it is advised to use this only if you know what you are doing. To read more about reflection API, please go <a title="Java Reflection API" href="http://docs.oracle.com/javase/tutorial/reflect/" target="_blank" rel="noopener noreferrer">here</a>. Coming back to this tutorial, here we are using the reflection APIs to invoke following methods which are not included in Android APIs:</p>



<ol class="wp-block-list"><li><code>getLteSignalStrength</code> &#8211; Returns LTE signal strength</li><li><code>getLteRsrp</code> &#8211; Returns LTE Reference Signal Received Power</li><li><code>getLteRsrq</code> &#8211; Returns LTE Reference Signal Received Quality</li><li><code>getLteRssnr</code> &#8211; Returns LTE rssnr</li><li><code>getLteCqi</code> &#8211; Returns LTE Channel Quality Indication</li></ol>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android Signal Strength</h2>



<p class="wp-block-paragraph">Although if we go by standard Android APIs, whenever Android onSignalStrengthsChanged method is invoked an object of <a title="Android Signal Strength" rel="noopener noreferrer" href="http://developer.android.com/reference/android/telephony/SignalStrength.html" target="_blank"><code>SignalStrength</code></a> is passed as an argument. As its shown in the above code, with this object we can determine the type of signal being receiving on device. It could be any type of network like GSM, CDMA or EVDO, hence one can get signal strength as well. Hope this helped you out, if it did please share it with your friends.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/">Android onSignalStrengthsChanged &#8211; LTE Strength Measurement</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">668</post-id>	</item>
		<item>
		<title>Android onDataConnectionStateChanged &#8211; Detect Network Type</title>
		<link>https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/</link>
					<comments>https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/#respond</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Wed, 06 Aug 2014 08:59:44 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[PhoneStateListener]]></category>
		<category><![CDATA[telephonymanager]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=656</guid>

					<description><![CDATA[<p>While playing with network and phone states in android there could always be a situation where one would like to detect the network type and monitor the changes of data connection state. Both of these situations can be taken care off, with the help of Android PhoneStateListener onDataConnectionStateChanged method. As I mentioned in my previous&#8230;&#160;<a href="https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android onDataConnectionStateChanged &#8211; Detect Network Type</span></a></p>
<p>The post <a href="https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/">Android onDataConnectionStateChanged &#8211; Detect Network Type</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" decoding="async" width="640" height="480" data-attachment-id="659" data-permalink="https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/2344670438_d8a9a24683_b/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/2344670438_d8a9a24683_b.jpg?fit=640%2C480&amp;ssl=1" data-orig-size="640,480" data-comments-opened="1" data-image-title="2344670438_d8a9a24683_b" data-image-description="&lt;p&gt;Photo Credit: &lt;a href=&quot;https://www.flickr.com/photos/37996586641@N01/2344670438/&quot;&gt;Brandon C. Watson&lt;/a&gt; via &lt;a href=&quot;http://compfight.com&quot;&gt;Compfight&lt;/a&gt; &lt;a href=&quot;https://www.flickr.com/help/general/#147&quot;&gt;cc&lt;/a&gt;&lt;/p&gt;
" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/2344670438_d8a9a24683_b.jpg?fit=640%2C480&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/2344670438_d8a9a24683_b.jpg?resize=640%2C480" alt="Android onDataConnectionStateChanged" class="wp-image-659" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/2344670438_d8a9a24683_b.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/2344670438_d8a9a24683_b.jpg?resize=300%2C225&amp;ssl=1 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">While playing with network and phone states in android there could always be a situation where one would like to detect the network type and monitor the changes of data connection state. Both of these situations can be taken care off, with the help of <a title="Android PhoneStateListener onDataConnectionStateChanged" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onDataConnectionStateChanged%28int,%20int%29" target="_blank" rel="noopener noreferrer">Android PhoneStateListener onDataConnectionStateChanged</a> method. As I mentioned in my previous tutorial that PhoneStateListener class works as an interface along with TelephonyManager class. Although this tutorial is focused on&nbsp;Android onDataConnectionStateChanged if you wish to read in detail about these classes please go through my complete series on PhoneStateListener.</p>



<p class="wp-block-paragraph">1) <a title="Android PhoneStateListener Example" href="http://www.truiton.com/2014/08/android-phonestatelistener-example/">Android PhoneStateListener Example</a><br>2) <a title="Android onDataConnectionStateChanged – Detect Network Type" href="http://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/">Android onDataConnectionStateChanged – Detect Network Type</a><br>3)<a title="Android onSignalStrengthsChanged – LTE Strength Measurement" href="http://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/" target="_blank" rel="noopener noreferrer"> Android onSignalStrengthsChanged – LTE Strength Measurement</a></p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android onDataConnectionStateChanged</h2>



<p class="wp-block-paragraph">This&nbsp;is a method in PhoneStateListener class of Android, which is used to monitor the data connection state changes (only for network not wifi). Although if you want to monitor the changes in overall data connectivity you could go for the&nbsp; ConnectivityManager class and use <code>ConnectivityManager.CONNECTIVITY_ACTION</code>. Since the connectivity manager can monitor the changes in both wifi and data, obviously its a better option. But at times there are situations when one needs to monitor several network related things and this can be done only through&nbsp;Android PhoneStateListener class. Threfore in such&nbsp;situation one can definitely use Android onDataConnectionStateChanged method to monitor network data changes.</p>



<p class="wp-block-paragraph">Another situation when this Android onDataConnectionStateChanged method could be used is when, device is connected to the wifi network and you need to detect operator data connection state. To use this method we need to define following permission in the manifest:</p>



<p class="lang:xhtml decode:true wp-block-paragraph"><code>&lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /></code></p>



<p class="wp-block-paragraph">Lets define an activity where PhoneStateListener class can be initialized:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.phonestatelistener;

import android.support.v7.app.ActionBarActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class PhoneStateListenerActivity extends ActionBarActivity {
 TelephonyManager tManager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_phone_state_listener);

 tManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
 tManager.listen(new CustomPhoneStateListener(this),
 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.phone_state_listener, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}</pre>



<p class="wp-block-paragraph">In the above class we created an instance of TelephonyManager and used its method <code>listen(PhoneStateListener listener, int events)</code>&nbsp;to listen the specified <a href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html">PhoneStateListener</a> <code>LISTEN_</code> flags. Now if we closely observe the listen method, we passed new object of our custom PhoneStateListener and specified the <code>PhoneStateListener.LISTEN_DATA_CONNECTION_STATE</code> flag. Meaning that the specified&nbsp;object of PhoneStateListener would get callbacks of <code>onDataConnectionStateChanged(int state, int networkType)</code> method, which would solve our purpose here.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Lets define custom Android PhoneStateListener onDataConnectionStateChanged method:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.phonestatelistener;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class CustomPhoneStateListener extends PhoneStateListener {
 Context mContext;
 public static String LOG_TAG = "CustomPhoneStateListener";

 public CustomPhoneStateListener(Context context) {
 mContext = context;
 }

 @Override
 public void onDataConnectionStateChanged(int state, int networkType) {
 super.onDataConnectionStateChanged(state, networkType);
 switch (state) {
 case TelephonyManager.DATA_DISCONNECTED:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_DISCONNECTED");
 break;
 case TelephonyManager.DATA_CONNECTING:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_CONNECTING");
 break;
 case TelephonyManager.DATA_CONNECTED:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_CONNECTED");
 break;
 case TelephonyManager.DATA_SUSPENDED:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: DATA_SUSPENDED");
 break;
 default:
 Log.w(LOG_TAG, "onDataConnectionStateChanged: UNKNOWN " + state);
 break;
 }

 switch (networkType) {
 case TelephonyManager.NETWORK_TYPE_CDMA:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_CDMA");
 break;
 case TelephonyManager.NETWORK_TYPE_EDGE:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_EDGE");
 break;
 case TelephonyManager.NETWORK_TYPE_EVDO_0:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_EVDO_0");
 break;
 case TelephonyManager.NETWORK_TYPE_GPRS:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_GPRS");
 break;
 case TelephonyManager.NETWORK_TYPE_HSDPA:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_HSDPA");
 break;
 case TelephonyManager.NETWORK_TYPE_HSPA:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_HSPA");
 break;
 case TelephonyManager.NETWORK_TYPE_IDEN:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_IDEN");
 break;
 case TelephonyManager.NETWORK_TYPE_LTE:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_LTE");
 break;
 case TelephonyManager.NETWORK_TYPE_UMTS:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_UMTS");
 break;
 case TelephonyManager.NETWORK_TYPE_UNKNOWN:
 Log.i(LOG_TAG, "onDataConnectionStateChanged: NETWORK_TYPE_UNKNOWN");
 break;
 default:
 Log.w(LOG_TAG, "onDataConnectionStateChanged: Undefined Network: "
 + networkType);
 break;
 }
 }
}</pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">After running&nbsp;this piece of code, you would observe that whenever there is a change in the of data connection, a callback is received in the Android PhoneStateListener&nbsp;<code>onDataConnectionStateChanged(int state, int networkType)</code> method.</p>



<p class="wp-block-paragraph">With this method we can&nbsp;monitor two things</p>



<ol class="wp-block-list"><li>Data Connection&nbsp;State (<code>int state</code>) &#8211; This parameter would give us the current state of data connection, it could range from <code>TelephonyManager.DATA_DISCONNECTED</code> to <code>TelephonyManager.DATA_SUSPENDED</code>.</li><li>Network Type (<code>int networkType</code>) &#8211; This parameter would tell the us the type of&nbsp;device&#8217;s network connection. At present there are a total of 16 network types and it can range from <code>TelephonyManager.NETWORK_TYPE_1xRTT</code> to <code>TelephonyManager.NETWORK_TYPE_UNKNOWN</code>.</li></ol>



<p class="wp-block-paragraph">To conclude I would say that with this&nbsp;<a title="Android onDataConnectionStateChanged – Detect Network Type" href="http://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/">Android onDataConnectionStateChanged &#8211; Detect Network Type</a> tutorial one can monitor changes in the data connection state and cell network type.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/">Android onDataConnectionStateChanged &#8211; Detect Network Type</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">656</post-id>	</item>
		<item>
		<title>Android PhoneStateListener Example</title>
		<link>https://www.truiton.com/2014/08/android-phonestatelistener-example/</link>
					<comments>https://www.truiton.com/2014/08/android-phonestatelistener-example/#comments</comments>
		
		<dc:creator><![CDATA[Mohit Gupt]]></dc:creator>
		<pubDate>Sun, 03 Aug 2014 17:25:29 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[oncallstatechanged]]></category>
		<category><![CDATA[onCellLocationChanged]]></category>
		<category><![CDATA[ondataactivity]]></category>
		<category><![CDATA[onServiceStateChanged]]></category>
		<category><![CDATA[telephonymanager]]></category>
		<guid isPermaLink="false">http://www.truiton.com/?p=645</guid>

					<description><![CDATA[<p>While searching through some documentation I found an interesting class of android through which we can listen to some basic network related events, called Android PhoneStateListener which lead to this tutorial Android PhoneStateListener Example. Another class which needs an introduction is TelephonyManager, as this is the class which uses the Android PhoneStateListener to listen telephony&#8230;&#160;<a href="https://www.truiton.com/2014/08/android-phonestatelistener-example/" rel="bookmark">Read More &#187;<span class="screen-reader-text">Android PhoneStateListener Example</span></a></p>
<p>The post <a href="https://www.truiton.com/2014/08/android-phonestatelistener-example/">Android PhoneStateListener Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" loading="lazy" decoding="async" width="640" height="427" data-attachment-id="649" data-permalink="https://www.truiton.com/2014/08/android-phonestatelistener-example/android-phonestatelistener/" data-orig-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/Android-PhoneStateListener.jpg?fit=640%2C427&amp;ssl=1" data-orig-size="640,427" data-comments-opened="1" data-image-title="Android PhoneStateListener" data-image-description="&lt;p&gt;Photo Credit: &lt;a href=&quot;https://www.flickr.com/photos/79688583@N03/8703538283/&quot;&gt;Jamie Frith&lt;/a&gt; via &lt;a href=&quot;http://compfight.com&quot;&gt;Compfight&lt;/a&gt; &lt;a href=&quot;https://www.flickr.com/help/general/#147&quot;&gt;cc&lt;/a&gt; &lt;/p&gt;
" data-image-caption="" data-large-file="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/Android-PhoneStateListener.jpg?fit=640%2C427&amp;ssl=1" src="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/Android-PhoneStateListener.jpg?resize=640%2C427" alt="Android PhoneStateListener" class="wp-image-649" srcset="https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/Android-PhoneStateListener.jpg?w=640&amp;ssl=1 640w, https://i0.wp.com/www.truiton.com/wp-content/uploads/2014/08/Android-PhoneStateListener.jpg?resize=300%2C200&amp;ssl=1 300w" sizes="auto, (max-width: 640px) 100vw, 640px" /></figure></div>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">While searching through some documentation I found an interesting class of android through which we can listen to some basic network related events, called Android PhoneStateListener which lead to this tutorial Android PhoneStateListener Example. Another class which needs an introduction is <a title="Android TelephonyManager" href="http://developer.android.com/reference/android/telephony/TelephonyManager.html" target="_blank" rel="noopener noreferrer">TelephonyManager</a>, as this is the class which uses the Android <a title="Android PhoneStateListener" href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html" target="_blank" rel="noopener noreferrer">PhoneStateListener</a> to listen telephony service updates. This tutorial is the first one in the series of exploring Android PhoneStateListener and TelephonyManager classes at <a title="Truiton" href="http://www.truiton.com" target="_blank" rel="noopener noreferrer">Truiton.com</a>. Links to complete series are:</p>



<p class="wp-block-paragraph">1) <a title="Android PhoneStateListener Example" href="http://www.truiton.com/2014/08/android-phonestatelistener-example/">Android PhoneStateListener Example</a><br>2) <a title="Android onDataConnectionStateChanged – Detect Network Type" href="http://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/" target="_blank" rel="noopener noreferrer">Android onDataConnectionStateChanged &#8211; Detect Network Type</a><br>3) <a title="Android onSignalStrengthsChanged – LTE Strength Measurement" href="http://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/" target="_blank" rel="noopener noreferrer">Android onSignalStrengthsChanged &#8211; LTE Strength Measurement</a></p>



<p class="wp-block-paragraph">To start off let me give a brief intro about what all things I&#8217;ll be covering in this tutorial. Here I would be instantiating an object of Android TelephonyManager and listening to the updates of <code>Context.TELEPHONY_SERVICE</code>. To monitor the changes of specific telephony states like <code>PhoneStateListener.LISTEN_CELL_INFO, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS,&nbsp;PhoneStateListener.LISTEN_DATA_CONNECTION_STATE</code> and many more, I will be declaring a class <code>CustomPhoneStateListener</code> which will extend&nbsp;Android PhoneStateListener and override all of its methods.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android TelephonyManager</h2>



<p class="wp-block-paragraph">This class of android is the main class through which one can access the telephony services of a device. All the information in this class is accessed through a system-level service called Context.TELEPHONY_SERVICE. Since this tutorial is focused on Android PhoneStateListener lets not get in depth of this class. The only relevant method for us in this class is <code>TelephonyManager.listen(PhoneStateListener listener, int events)</code>. And for this method I will be declaring a custom PhoneStateListener.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android PhoneStateListener</h2>



<p class="wp-block-paragraph">This is the actual class which gets callbacks, whenever some change is observed in TELEPHONY_SERVICE. Android PhoneStateListener has many callback methods which are called if we register for their specific <code>LISTEN_</code> flag in the TelephonyManager&#8217;s listen method. Have a look at the listing below to correlate <code>LISTEN_</code> flags with Android PhoneStateListener&#8217;s callback methods:</p>



<h4 class="wp-block-heading">ANDROID PHONESTATELISTENER CALLBACK METHODS</h4>



<table class="wp-block-table"><tbody><tr><td><strong>LISTEN_ Flag</strong></td><td><strong>Callback Method</strong></td></tr><tr><td>PhoneStateListener.LISTEN_CALL_STATE</td><td>onCallStateChanged(int state, String incomingNumber)</td></tr><tr><td>PhoneStateListener.LISTEN_CELL_INFO</td><td>onCellInfoChanged(List&lt;CellInfo> cellInfo)</td></tr><tr><td>PhoneStateListener.LISTEN_CELL_LOCATION</td><td>onCellLocationChanged(CellLocation location)</td></tr><tr><td>PhoneStateListener.LISTEN_DATA_ACTIVITY</td><td>onDataActivity(int direction)</td></tr><tr><td>PhoneStateListener.LISTEN_DATA_CONNECTION_STATE</td><td>onDataConnectionStateChanged(int state, int networkType)</td></tr><tr><td>PhoneStateListener.LISTEN_SERVICE_STATE</td><td>onServiceStateChanged(ServiceState serviceState)</td></tr><tr><td>PhoneStateListener.LISTEN_SIGNAL_STRENGTHS</td><td>onSignalStrengthsChanged(SignalStrength signalStrength)</td></tr><tr><td>PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR</td><td>onCallForwardingIndicatorChanged(boolean cfi)</td></tr><tr><td>PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR</td><td>onMessageWaitingIndicatorChanged(boolean mwi)</td></tr></tbody></table>



<p class="wp-block-paragraph">Now since we are dealing here with phone states and locations we would require to declare some permissions in manifest:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.truiton.phonestatelistener"
    android:versionCode="1"
    android:versionName="1.0" >

    &lt;uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" />
    &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    &lt;application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        &lt;activity
            android:name=".PhoneStateListenerActivity"
            android:label="@string/app_name" >
            &lt;intent-filter>
                &lt;action android:name="android.intent.action.MAIN" />

                &lt;category android:name="android.intent.category.LAUNCHER" />
            &lt;/intent-filter>
        &lt;/activity>
    &lt;/application>

&lt;/manifest></pre>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Next lets define our PhoneStateListenerActivity</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.phonestatelistener;

import android.support.v7.app.ActionBarActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class PhoneStateListenerActivity extends ActionBarActivity {
 TelephonyManager tManager;

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 // Added because of PhoneStateListener.LISTEN_CELL_INFO
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_phone_state_listener);

 tManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
 tManager.listen(new CustomPhoneStateListener(this),
 PhoneStateListener.LISTEN_CALL_STATE
 | PhoneStateListener.LISTEN_CELL_INFO // Requires API 17
 | PhoneStateListener.LISTEN_CELL_LOCATION
 | PhoneStateListener.LISTEN_DATA_ACTIVITY
 | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
 | PhoneStateListener.LISTEN_SERVICE_STATE
 | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS
 | PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
 | PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.phone_state_listener, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}</pre>



<p class="wp-block-paragraph">In this class one can see that in the <code>tManager.listen</code> method a new object for CustomPhoneStateListener is defined and in the events parameter a bitwise-OR combination of Android <a href="http://developer.android.com/reference/android/telephony/PhoneStateListener.html" target="_blank" rel="noopener noreferrer">PhoneStateListener</a> LISTEN_ flags is passed. Now lets have a look at some of the main methods of CustomPhoneStateListener.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">package com.truiton.phonestatelistener;

import java.util.List;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.telephony.CellInfo;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

public class CustomPhoneStateListener extends PhoneStateListener {
 Context mContext;
 public static String LOG_TAG = "CustomPhoneStateListener";

 public CustomPhoneStateListener(Context context) {
 mContext = context;
 }

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 @Override
 public void onCellInfoChanged(List&lt;CellInfo> cellInfo) {
 super.onCellInfoChanged(cellInfo);
 Log.i(LOG_TAG, "onCellInfoChanged: " + cellInfo);
 }

 @Override
 public void onDataActivity(int direction) {
 super.onDataActivity(direction);
 switch (direction) {
 case TelephonyManager.DATA_ACTIVITY_NONE:
 Log.i(LOG_TAG, "onDataActivity: DATA_ACTIVITY_NONE");
 break;
 case TelephonyManager.DATA_ACTIVITY_IN:
 Log.i(LOG_TAG, "onDataActivity: DATA_ACTIVITY_IN");
 break;
 case TelephonyManager.DATA_ACTIVITY_OUT:
 Log.i(LOG_TAG, "onDataActivity: DATA_ACTIVITY_OUT");
 break;
 case TelephonyManager.DATA_ACTIVITY_INOUT:
 Log.i(LOG_TAG, "onDataActivity: DATA_ACTIVITY_INOUT");
 break;
 case TelephonyManager.DATA_ACTIVITY_DORMANT:
 Log.i(LOG_TAG, "onDataActivity: DATA_ACTIVITY_DORMANT");
 break;
 default:
 Log.w(LOG_TAG, "onDataActivity: UNKNOWN " + direction);
 break;
 }
 }

 @Override
 public void onServiceStateChanged(ServiceState serviceState) {
 super.onServiceStateChanged(serviceState);
 Log.i(LOG_TAG, "onServiceStateChanged: " + serviceState.toString());
 Log.i(LOG_TAG, "onServiceStateChanged: getOperatorAlphaLong "
 + serviceState.getOperatorAlphaLong());
 Log.i(LOG_TAG, "onServiceStateChanged: getOperatorAlphaShort "
 + serviceState.getOperatorAlphaShort());
 Log.i(LOG_TAG, "onServiceStateChanged: getOperatorNumeric "
 + serviceState.getOperatorNumeric());
 Log.i(LOG_TAG, "onServiceStateChanged: getIsManualSelection "
 + serviceState.getIsManualSelection());
 Log.i(LOG_TAG,
 "onServiceStateChanged: getRoaming "
 + serviceState.getRoaming());

 switch (serviceState.getState()) {
 case ServiceState.STATE_IN_SERVICE:
 Log.i(LOG_TAG, "onServiceStateChanged: STATE_IN_SERVICE");
 break;
 case ServiceState.STATE_OUT_OF_SERVICE:
 Log.i(LOG_TAG, "onServiceStateChanged: STATE_OUT_OF_SERVICE");
 break;
 case ServiceState.STATE_EMERGENCY_ONLY:
 Log.i(LOG_TAG, "onServiceStateChanged: STATE_EMERGENCY_ONLY");
 break;
 case ServiceState.STATE_POWER_OFF:
 Log.i(LOG_TAG, "onServiceStateChanged: STATE_POWER_OFF");
 break;
 }
 }

 @Override
 public void onCallStateChanged(int state, String incomingNumber) {
 super.onCallStateChanged(state, incomingNumber);
 switch (state) {
 case TelephonyManager.CALL_STATE_IDLE:
 Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_IDLE");
 break;
 case TelephonyManager.CALL_STATE_RINGING:
 Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_RINGING");
 break;
 case TelephonyManager.CALL_STATE_OFFHOOK:
 Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
 break;
 default:
 Log.i(LOG_TAG, "UNKNOWN_STATE: " + state);
 break;
 }
 }

 @TargetApi(Build.VERSION_CODES.GINGERBREAD)
 @Override
 public void onCellLocationChanged(CellLocation location) {
 super.onCellLocationChanged(location);
 if (location instanceof GsmCellLocation) {
 GsmCellLocation gcLoc = (GsmCellLocation) location;
 Log.i(LOG_TAG,
 "onCellLocationChanged: GsmCellLocation "
 + gcLoc.toString());
 Log.i(LOG_TAG, "onCellLocationChanged: GsmCellLocation getCid "
 + gcLoc.getCid());
 Log.i(LOG_TAG, "onCellLocationChanged: GsmCellLocation getLac "
 + gcLoc.getLac());
 Log.i(LOG_TAG, "onCellLocationChanged: GsmCellLocation getPsc"
 + gcLoc.getPsc()); // Requires min API 9
 } else if (location instanceof CdmaCellLocation) {
 CdmaCellLocation ccLoc = (CdmaCellLocation) location;
 Log.i(LOG_TAG,
 "onCellLocationChanged: CdmaCellLocation "
 + ccLoc.toString());
 Log.i(LOG_TAG,
 "onCellLocationChanged: CdmaCellLocation getBaseStationId "
 + ccLoc.getBaseStationId());
 Log.i(LOG_TAG,
 "onCellLocationChanged: CdmaCellLocation getBaseStationLatitude "
 + ccLoc.getBaseStationLatitude());
 Log.i(LOG_TAG,
 "onCellLocationChanged: CdmaCellLocation getBaseStationLongitude"
 + ccLoc.getBaseStationLongitude());
 Log.i(LOG_TAG,
 "onCellLocationChanged: CdmaCellLocation getNetworkId "
 + ccLoc.getNetworkId());
 Log.i(LOG_TAG,
 "onCellLocationChanged: CdmaCellLocation getSystemId "
 + ccLoc.getSystemId());
 } else {
 Log.i(LOG_TAG, "onCellLocationChanged: " + location.toString());
 }
 }

 @Override
 public void onCallForwardingIndicatorChanged(boolean cfi) {
 super.onCallForwardingIndicatorChanged(cfi);
 Log.i(LOG_TAG, "onCallForwardingIndicatorChanged: " + cfi);
 }

 @Override
 public void onMessageWaitingIndicatorChanged(boolean mwi) {
 super.onMessageWaitingIndicatorChanged(mwi);
 Log.i(LOG_TAG, "onMessageWaitingIndicatorChanged: " + mwi);
 }
}</pre>



<p class="wp-block-paragraph">Here in the above class I extended the Android PhoneStateListener class and I am overriding some of its key methods. Through these overridden methods we can access the telephony states, cell locations, network types, incoming call number and much more network carrier related information. In this&nbsp;<a title="Android PhoneStateListener Example" href="http://www.truiton.com/2014/08/android-phonestatelistener-example/">Android PhoneStateListener Example</a> I&#8217;ll be explaining all key methods in short except two key callback methods as they have separate tutorials. First one onDataConnectionStateChanged and second one onSignalStrengthsChanged. To keep it simple and self explanatory I am just using logs to display the all the states. Now lets start with first overridden method:</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android PhoneStateListener onDataActivity</h2>



<p class="wp-block-paragraph">This method is called when some data activity is observed like upload or download or both. Whenever there is a change in network data you would receive a callback here.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android PhoneStateListener&nbsp;onServiceStateChanged</h2>



<p class="wp-block-paragraph">This method is called when something changes in network operator service. It could be a change in service when phone goes from in service to out of service or to emergency only. In this callback method, along with states some other parameters are returned like operator name from <code>getOperatorAlphaLong</code> method. To detect if phone is in roaming <code>getRoaming</code> method can be used.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android PhoneStateListener onCallStateChanged</h2>



<p class="wp-block-paragraph">Well this method is one of the most used method of this class as this method listens to the call state changes. Like when phone starts ringing, call is put on hold or a call is ended.</p>



<p class="wp-block-paragraph"></p>



<h2 class="wp-block-heading">Android PhoneStateListener&nbsp;onCellLocationChanged</h2>



<p class="wp-block-paragraph">This method is called when your device switches operator cell location. This method can provide great amounts of information regarding the location of device as its object can be cast to GsmCellLocation or CdmaCellLocation. Once we have location area code through <code>GsmCellLocation.getLac()</code> method and cell id through <code>GsmCellLocation.getCid()</code> method we can query and get actual latitude and longitude of that location by any web-service freely available on internet. Also would like to mention here that in case of CDMA device this process is not required as CdmaCellLocation class contains the latitude and longitude parameters in itself.</p>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Concluding this tutorial I would like to mention separate tutorials are available for more methods like <a title="Android onSignalStrengthsChanged – LTE Strength Measurement" href="http://www.truiton.com/2014/08/android-onsignalstrengthschanged-lte-strength-measurement/" target="_blank" rel="noopener noreferrer">onSignalStrengthsChanged</a> and <a title="Android onDataConnectionStateChanged – Detect Network Type" href="http://www.truiton.com/2014/08/android-ondataconnectionstatechanged-detect-network-type/" target="_blank" rel="noopener noreferrer">onDataConnectionStateChanged</a>. Please share this tutorial if it helped you.</p>
<div class="saboxplugin-wrap" itemtype="http://schema.org/Person" itemscope itemprop="author"><div class="saboxplugin-tab"><div class="saboxplugin-gravatar"><img alt='Mohit Gupt' src='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=100&#038;d=mm&#038;r=g' srcset='https://secure.gravatar.com/avatar/a816092ad56645d2635ccfceb9f7e9d44821c19dec126cb394c882ba574eaa04?s=200&#038;d=mm&#038;r=g 2x' class='avatar avatar-100 photo' height='100' width='100' itemprop="image"/></div><div class="saboxplugin-authorname"><a href="https://www.truiton.com/author/mohitgupt/" class="vcard author" rel="author"><span class="fn">Mohit Gupt</span></a></div><div class="saboxplugin-desc"><div itemprop="description"><p>Born in New Delhi, India. A software engineer by profession, an android enthusiast and mobile development evangelist. My motive here is to create a group of skilled engineers, who can build better software. Reason being programming is my passion, and also it feels good to make a device do something you want. Professionally I have worked with many software engineering and product development firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, but whenever I get spare time I share my thoughts here at Truiton.</p>
</div></div><div class="saboxplugin-web "><a href="http://www.truiton.com" target="_self" >www.truiton.com</a></div><div class="clearfix"></div><div class="saboxplugin-socials "><a title="Wordpress" target="_blank" href="https://www.truiton.com/author/mohitgupt/" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-wordpress" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 512 512"><path fill="currentColor" d="M61.7 169.4l101.5 278C92.2 413 43.3 340.2 43.3 256c0-30.9 6.6-60.1 18.4-86.6zm337.9 75.9c0-26.3-9.4-44.5-17.5-58.7-10.8-17.5-20.9-32.4-20.9-49.9 0-19.6 14.8-37.8 35.7-37.8.9 0 1.8.1 2.8.2-37.9-34.7-88.3-55.9-143.7-55.9-74.3 0-139.7 38.1-177.8 95.9 5 .2 9.7.3 13.7.3 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l77.5 230.4L249.8 247l-33.1-90.8c-11.5-.7-22.3-2-22.3-2-11.5-.7-10.1-18.2 1.3-17.5 0 0 35.1 2.7 56 2.7 22.2 0 56.7-2.7 56.7-2.7 11.5-.7 12.8 16.2 1.4 17.5 0 0-11.5 1.3-24.3 2l76.9 228.7 21.2-70.9c9-29.4 16-50.5 16-68.7zm-139.9 29.3l-63.8 185.5c19.1 5.6 39.2 8.7 60.1 8.7 24.8 0 48.5-4.3 70.6-12.1-.6-.9-1.1-1.9-1.5-2.9l-65.4-179.2zm183-120.7c.9 6.8 1.4 14 1.4 21.9 0 21.6-4 45.8-16.2 76.2l-65 187.9C426.2 403 468.7 334.5 468.7 256c0-37-9.4-71.8-26-102.1zM504 256c0 136.8-111.3 248-248 248C119.2 504 8 392.7 8 256 8 119.2 119.2 8 256 8c136.7 0 248 111.2 248 248zm-11.4 0c0-130.5-106.2-236.6-236.6-236.6C125.5 19.4 19.4 125.5 19.4 256S125.6 492.6 256 492.6c130.5 0 236.6-106.1 236.6-236.6z"></path></svg></span></a><a title="Twitter" target="_blank" href="http://twitter.com/mohitgupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-twitter" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 30 30"><path d="M26.37,26l-8.795-12.822l0.015,0.012L25.52,4h-2.65l-6.46,7.48L11.28,4H4.33l8.211,11.971L12.54,15.97L3.88,26h2.65 l7.182-8.322L19.42,26H26.37z M10.23,6l12.34,18h-2.1L8.12,6H10.23z" /></svg></span></a><a title="Facebook" target="_blank" href="https://www.facebook.com/Mr.Mohit.Gupt" rel="nofollow noopener" class="saboxplugin-icon-grey"><svg aria-hidden="true" class="sab-facebook" role="img" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 264 512"><path fill="currentColor" d="M76.7 512V283H0v-91h76.7v-71.7C76.7 42.4 124.3 0 193.8 0c33.3 0 61.9 2.5 70.2 3.6V85h-48.2c-37.8 0-45.1 18-45.1 44.3V192H256l-11.7 91h-73.6v229"></path></svg></span></a></div></div></div><p>The post <a href="https://www.truiton.com/2014/08/android-phonestatelistener-example/">Android PhoneStateListener Example</a> appeared first on <a href="https://www.truiton.com">Truiton</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.truiton.com/2014/08/android-phonestatelistener-example/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">645</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: www.truiton.com @ 2026-07-08 16:46:54 by W3 Total Cache
-->