Android phone signal strength detection details _android

Source: Internet
Author: User

Recently to be in the running to find a job, inevitably in the interview process encountered such a problem, remember the most clearly in the interview process was asked, when the mobile phone in a weak network state, how to deal with, how to monitor the network signal intensity changes. But it was so, the answer was messy, the idea was not clear at all. Today's small knitting here to lead you to understand the mobile phone signal strength related to several concepts.

Android Phone Signal Strength Introduction

Android defines 2 signal units: DBM and ASU. The relationship between them is: DBm =-113+2asu, a unique unit of signal that Google defines for Android phones. For example, my signal strength is -53dbm, then 30asu, because-53 =-113 + (230).

Describe the two in detail:

Asu:alone signal Unit is an analog signal. ASU only represents the rate at which the phone transmits its position to a nearby signal tower. It measures the same thing as dbm, but it is expressed in a more linear way.
DBm: is a value that represents the absolute values of the power (which can also be considered a ratio based on 1mW power), calculated as: 10log (Power value/1MW).

[Example] if the power p is 1MW, the converted to dbm is 0dBm.

[For example] for 0.01mW power, the value in dbm unit should be: 10log (0.01/1) =-20dbm.

The larger the value, the better the signal. Because the cell phone signal strength is generally small, converted into dbm is generally negative.
The Chinese mobile Code stipulates that the mobile phone receives the level >= (the city takes the -90dbm; The village takes the -94dbm), then satisfies the coverage request,

That is, the wireless signal strength to meet the coverage requirements. -67DBM is more than 20 db than the -90DBM signal,

It will be much better when it comes to the success of the call and the quality of the voice during the call. Then introduce a related concept db.

DB: is a value that represents a relative value, a pure ratio, represents only two of the relative size of the relationship, no units, when considering a power compared to the power of a large or small number of db, according to the following formula: 10log (a power/b power), if the use of the voltage ratio of both calculation, to use 20log (methyl voltage /b voltage).

[Example] a power than the second power of one-fold, then 10LG (a power/b power) =10lg2=3db, that is, a power than B power of 3 DB. Conversely, if the power of a is half of the power of B, then the power of a is smaller than the power of B 3 DB.

Summary: DBM is a negative number, the closer to 0 the higher the signal strength, the better the signal, but not 0. ASU is positive, the greater the value the better the signal. DB is the ratio between two quantities, representing the relative size of the two quantities, and dbm is the value that represents the absolute size of the power.

On the logarithmic operation, do not remember to review their own.

Below to understand the network type bar, or a lot of, but in the country as long as the three major operators of the network type probably on it.

Telecom

2G CDMA
3G CDMA2000
4G Td-lte,fdd-lte

Move

2G GSM
3G TD-SCDMA
4G Td-lte,fdd-lte

Unicom

2G GSM
3G WCDMA
4G Td-lte,fdd-lte

The Google API provides us with 19 types, defined in the Telephonymanager class, as follows

/** network type is unknown * * public static final int network_type_unknown = 0;
/** Current Network is GPRS */public static final int network_type_gprs = 1;
/** Current Network is EDGE/public static final int network_type_edge = 2;
/** Current Network is UMTS/public static final int network_type_umts = 3;
/** Current network are cdma:either is95a or is95b*/public static final int NETWORK_TYPE_CDMA = 4;
/** Current network are EVDO revision 0*/public static final int network_type_evdo_0 = 5;
/** Current network are EVDO revision a*/public static final int network_type_evdo_a = 6;
/** Current network are 1xrtt*/public static final int network_type_1xrtt = 7;
/** Current Network is HSDPA * * public static final int NETWORK_TYPE_HSDPA = 8;
/** Current Network is HSUPA * * public static final int network_type_hsupa = 9;
/** Current Network is HSPA * * public static final int network_type_hspa = 10;
/** Current Network is iDen * * public static final int network_type_iden = 11; /** CurRent Network is EVDO revision b*/public static final int network_type_evdo_b = 12;
/** Current Network is LTE */public static final int network_type_lte = 13;
/** Current Network is EHRPD * * public static final int NETWORK_TYPE_EHRPD = 14;
/** Current Network is hspa+ * * public static final int NETWORK_TYPE_HSPAP = 15;
/** Current Network is GSM {@hide} */public static final int network_type_gsm = 16;
/** Current Network is TD_SCDMA {@hide} */public static final int NETWORK_TYPE_TD_SCDMA = 17; /** Current Network is IWLAN {@hide} */public static final int network_type_iwlan = 18;

Here are some examples to test it.

First remember to add permission

<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= " Android.permission.CHANGE_NETWORK_STATE "/>
<uses-permission android:name=" android.permission.ACCESS_ Wifi_state "/>
<uses-permission android:name=" Android.permission.ACCESS_NETWORK_STATE "/>"

Instance Code

public class Mainactivity extends Appcompatactivity {private static final int networktype_wifi = 0; private static final
int networktype_4g = 1;
private static final int networktype_2g = 2;
private static final int networktype_none = 3;
Public TextView Mtextview;
Public Telephonymanager Mtelephonymanager;
Public Phonestatlistener Mlistener; /** * Network Signal Strength Monitoring * * @param savedinstancestate * * @Override protected void onCreate (Bundle savedinstancestate) {Super.oncre
Ate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mtextview = (TextView) Findviewbyid (R.id.textview);
Get Telephonymanager Mtelephonymanager = (telephonymanager) getsystemservice (Context.telephony_service);
Start listening Mlistener = new Phonestatlistener ();
Monitor signal strength Mtelephonymanager.listen (Mlistener, phonestatlistener.listen_signal_strengths); @Override protected void Onresume () {super.onresume (); Mtelephonymanager.listen (Mlistener, Phonestatlistener.listen
_signal_strengths); } @Override protected void OnPause () {sUper.onpause ();
When the user is not on the current page, stop listening Mtelephonymanager.listen (Mlistener, Phonestatlistener.listen_none); Private class Phonestatlistener extends Phonestatelistener {//Get signal strength @Override public void onsignalstrengthschanged (Si Gnalstrength signalstrength) {super.onsignalstrengthschanged (signalstrength);//Get network signal strength//get 0-4 of 5 signal levels, the greater the signal the better,
But api23 begin to use//int level = Signalstrength.getlevel ();
int gsmsignalstrength = Signalstrength.getgsmsignalstrength ();
Gets the network type int networktype = Getnetworktype (mainactivity.this); Switch (networktype) {case NETWORKTYPE_WIFI:mTextView.setText ("Current network is WIFI, signal strength is:" + gsmsignalstrength);
NETWORKTYPE_2G:mTextView.setText ("The current network is 2G mobile network, signal strength is:" + gsmsignalstrength);
Break
Case NETWORKTYPE_4G:mTextView.setText ("Current network is 4G mobile network, signal strength is:" + gsmsignalstrength);
Break
Case NETWORKTYPE_NONE:mTextView.setText ("Currently no network, signal strength:" + gsmsignalstrength);
Break
Case-1: Mtextview.settext ("Current network error, Signal strength:" + gsmsignalstrength);
Break }} public static int Getnetworktype(Context context)
{int mnetworktype =-1;
Connectivitymanager manager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);
Networkinfo networkinfo = Manager.getactivenetworkinfo (); if (networkinfo!= null && networkinfo.isconnected ()) {String type = Networkinfo.gettypename (); if (Type.equalsig Norecase ("WIFI")) {Mnetworktype = Networktype_wifi} else if (Type.equalsignorecase ("MOBILE")) {return isfastmobilenet Work (context)?
networktype_4g:networktype_2g;
} else {mnetworktype = networktype_none;//no network} return mnetworktype; /** to determine the network type/private static Boolean Isfastmobilenetwork (context context) {Telephonymanager Telephonymanager = (telephon
Ymanager) Context.getsystemservice (Context.telephony_service);
if (telephonymanager.getnetworktype () = = Telephonymanager.network_type_lte) {//Here is a simple distinction between two types of network, that 4G network for fast, but ultimately need reference signal value
return true;
return false; }
}

On the signal acquisition process, this needs to be further studied, at present only in the application layer to obtain a simple network signal.

The above is a small set to introduce the Android phone signal strength Testing Detailed introduction, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.