Android 3G mobile phone signal Information Acquisition

Source: Internet
Author: User

The mobile phone signal information in androi can be obtained through the TELEPHONY_SERVICE provided by the system.

[Java]
TelephonyManager tel = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );

TelephonyManager can listen to PhoneStateListener, which provides
[Java]
OnSignalStrengthsChanged mobile phone signal change
OnServiceStateChanged mobile service status change

OnSignalStrengthsChanged can obtain the following useful information:
[Java]
/*
SignalStrength. isGsm () whether the GSM signal is 2G or 3G
SignalStrength. getCdmaDbm (); Unicom 3G signal strength
SignalStrength. getcdma ecio (); 3G load ratio of China Unicom
SignalStrength. getEvdoDbm (); Telecom 3G signal strength
SignalStrength. getEvdoEcio (); 3G load ratio of China Telecom
SignalStrength. getEvdoSnr (); 3G signal-to-noise ratio of China Telecom
SignalStrength. getGsmSignalStrength (); 2G signal strength
SignalStrength. getGsmBitErrorRate (); 2G Error Rate
 
Load-to-dry ratio refers to the ratio of signal to noise in simulated radio waves in the air.
*/
The strange thing is that mobile 3G signal TD-SCDMA information can not be obtained

OnServiceStateChanged provides the following status changes:

[Java]
/*
ServiceState. STATE_EMERGENCY_ONLY only for emergency calls
ServiceState. STATE_IN_SERVICE signal normal
ServiceState. STATE_OUT_OF_SERVICE is not in the service area
ServiceState. STATE_POWER_OFF Power Failure
*/

The following are examples:

1. Add a 3G icon and text (activity_g3_example.xml) to the layout file)

[Html]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: gravity = "center"
Android: orientation = "vertical">
 
<ImageButton
Android: id = "@ + id/Icon_3GStatus"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/ic_3g"/>

<TextView
Android: id = "@ + id/Label_3GDetail"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: maxWidth = "320dp"/>
 
</LinearLayout>

2. Add an optional set of 3G icons and add the ic_3g.xml file to the drawable-hdpi directory. The content is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
 
<Level-list xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: maxLevel = "60" android: drawable = "@ drawable/ic_3g_s3"/>
<Item android: maxLevel = "90" android: drawable = "@ drawable/ic_3g_s2"/>
<Item android: maxLevel = "96" android: drawable = "@ drawable/ic_3g_s1"/>
<Item android: maxLevel = "100" android: drawable = "@ drawable/ic_3g_s0"/>
</Level-list>

Note the image to be imported

3. Listen for changes in the cell phone signal and status in the Code (G3Example. java)
[Java]
Package com. example. g3example;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Context;
Import android. telephony. PhoneStateListener;
Import android. telephony. ServiceState;
Import android. telephony. SignalStrength;
Import android. telephony. TelephonyManager;
Import android. util. Log;
Import android. view. Menu;
Import android. widget. ImageButton;
Import android. widget. TextView;
 
Public class G3Example extends Activity {
 
Private final String TAG = "G3Example ";
 
Private ImageButton mIcon3G;
Private TextView mLabel3G;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_g3_example );
 
MIcon3G = (ImageButton) findViewById (R. id. Icon_3GStatus );
MLabel3G = (TextView) findViewById (R. id. Label_3GDetail );
 
TelephonyManager tel = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE );
Tel. listen (new PhoneStateMonitor (), PhoneStateListener. LISTEN_SIGNAL_STRENGTHS | PhoneStateListener. LISTEN_SERVICE_STATE );
}
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_g3_example, menu );
Return true;
}
 
Public class PhoneStateMonitor extends PhoneStateListener {
Public void onSignalStrengthsChanged (SignalStrength signalStrength ){
Super. onSignalStrengthsChanged (signalStrength );
/*
SignalStrength. isGsm () whether the GSM signal is 2G or 3G
SignalStrength. getCdmaDbm (); Unicom 3G signal strength
SignalStrength. getcdma ecio (); 3G load ratio of China Unicom
SignalStrength. getEvdoDbm (); Telecom 3G signal strength
SignalStrength. getEvdoEcio (); 3G load ratio of China Telecom
SignalStrength. getEvdoSnr (); 3G signal-to-noise ratio of China Telecom
SignalStrength. getGsmSignalStrength (); 2G signal strength
SignalStrength. getGsmBitErrorRate (); 2G Error Rate
 
Load-to-dry ratio refers to the ratio of signal to noise in simulated radio waves in the air.
*/
MLabel3G. setText ("IsGsm:" + signalStrength. isGsm () +
"\ NCDMA Dbm:" + signalStrength. getCdmaDbm () + "Dbm" +
"\ NCDMA Ecio:" + signalStrength. getcdma Ecio () + "dB * 10" +
"\ NEvdo Dbm:" + signalStrength. getEvdoDbm () + "Dbm" +
"\ NEvdo Ecio:" + signalStrength. getEvdoEcio () + "dB * 10" +
"\ NGsm SignalStrength:" + signalStrength. getGsmSignalStrength () +
"\ NGsm BitErrorRate:" + signalStrength. getGsmBitErrorRate ());
 
MIcon3G. setImageLevel (Math. abs (signalStrength. getGsmSignalStrength ()));
}
 
Public void onServiceStateChanged (ServiceState serviceState ){
Super. onServiceStateChanged (serviceState );
 
/*
ServiceState. STATE_EMERGENCY_ONLY only for emergency calls
ServiceState. STATE_IN_SERVICE signal normal
ServiceState. STATE_OUT_OF_SERVICE is not in the service area
ServiceState. STATE_POWER_OFF Power Failure
*/
Switch (serviceState. getState ())
{
Case ServiceState. STATE_EMERGENCY_ONLY:
Log. d (TAG, "3g status: STATE_EMERGENCY_ONLY ");
Break;
Case ServiceState. STATE_IN_SERVICE:
Log. d (TAG, "3g status: STATE_IN_SERVICE ");
Break;
Case ServiceState. STATE_OUT_OF_SERVICE:
Log. d (TAG, "3g status: STATE_OUT_OF_SERVICE ");
Break;
Case ServiceState. STATE_POWER_OFF:
Log. d (TAG, "3g status: STATE_POWER_OFF ");
Break;
Default:
Break;
}
}
}
}

4. Added support for obtaining mobile phone signals in AndroidManifest. xml.

[Html] view plaincopy
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>


 

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.