Android Gets the information about the GSM/CDMA signal of the mobile phone and obtains the base station information _android

Source: Internet
Author: User
Tags stub

How do we get a CDMA or GSM phone signal on Android, where we often easily get a list of WiFi signals?

The system provides the Telephonymanager class, this kind is very rich, basically you need the handset information to be able to obtain, that below to see we need the CDMA and the GSM signal is how obtains.

Private Telephonymanager Telephonymanager;
Private Phonestatelistener Phonestatelistener;

First declare two variables

To initialize a variable in the OnCreate () method

Initphonestatelistener ();
Telephonymanager = (Telephonymanager) this.ctx.getSystemService (Context.telephony_service);
Telephonymanager.listen (Phonestatelistener, phonestatelistener.listen_signal_strengths);
Telephonymanager.listen (phonestatelistener,phonestatelistener.listen_cell_location); if (Telephonymanager.getcelllocation ()!=null) {//Get current base station information phonestatelistener.oncelllocationchanged (
Telephonymanager.getcelllocation ()); /** Initialize Phonestatelistener/private void Initphonestatelistener () {phonestatelistener = new Phonestatelistener () {@O Verride public void oncelllocationchanged (celllocation location) {//TODO auto-generated Method stub if (location Instanc
EOF gsmcelllocation) {//GSM network Phonetype = 1;
Gsmcell Gsmcell = new Gsmcell ();
Gsmcell.lac = ((gsmcelllocation) location). Getlac ();
Gsmcell.cid = ((gsmcelllocation) location). Getcid ();
/** get MCC,MNC/String mccmnc=telephonymanager.getnetworkoperator (); if (Mccmnc!=null&&mccmnc.length () >=5) {GSMCELL.MCC = Mccmnc.subString (0, 3);
GSMCELL.MNC = mccmnc.substring (3, 5);
} gsmcell.signal=lastsignal;
Gsmcell.time = System.currenttimemillis (); if (Gsmcell.lac!=-1 && gsmcell.cid!=-1) {gsmcells.add (0, Gsmcell);//Collections.sort (gsmcells);///** Save up to
3 Base Station INFORMATION *//if (Gsmcells.size () >3)//Gsmcells.remove (3);
//Get adjacent base station information List Neighboringlist=telephonymanager.getneighboringcellinfo (); for (Neighboringcellinfo ni:neighboringlist) {Gsmcell GB = new Gsmcell (); gb.mnc=mccmnc.substring (3, 5); gb.lac=
Ni.getlac ();
Gb.cid=ni.getcid ();
Gb.signal=-133+2*ni.getrssi ();
Gb.time=system.currenttimemillis ();
Gsmcells.add (GB);
} else {//other CDMA network try {Class cdmaclass = Class.forName ("android.telephony.cdma.CdmaCellLocation"); phonetype = 2;
Cdmacelllocation CDMA = (cdmacelllocation) location;
Cdmacell Cdmacell = new Cdmacell ();
Cdmacell.stationid = Cdma.getbasestationid () >= 0?
Cdma.getbasestationid (): Cdmacell.stationid; Cdmacell.networkid = Cdma.getnetworkid () >= 0? CDMA. Getnetworkid (): CDMAcell.networkid; Cdmacell.systemid = Cdma.getsystemid () >= 0?
CDMA. Getsystemid (): Cdmacell.systemid;
/** get MCC,MNC/String mccmnc=telephonymanager.getnetworkoperator (); if (Mccmnc!=null&&mccmnc.length () >=5) {CDMACELL.MCC = mccmnc.substring (0, 3); CDMACELL.MNC =
Mccmnc.substring (3, 5);
} cdmacell.signal=lastsignal;
Cdmacell.time = System.currenttimemillis ();
int lat = Cdma.getbasestationlatitude ();
int lon = Cdma.getbasestationlongitude (); if (Lat <Integer.MAX_VALUE&& lon <integer.max_value) {Cdmacell.lat = lat; cdmacell.lon = lon;} if (Cdmacel
L.stationid!= -1&& cdmacell.networkid!= -1&& cdmacell.systemid!=-1) {cdmaCells.add (0, CdmaCell);
List Neighboringlist=telephonymanager.getneighboringcellinfo ();
for (Neighboringcellinfo ni:neighboringlist) {Cdmacell Cdmabean = new Cdmacell (); cdmabean.systemid=cdmacell.systemid;
Cdmabean.lac=ni.getlac ();
Cdmabean.cellid=ni.getcid ();
Cdmabean.signal=-113+2*ni.getrssi (); Cdmacells.add (Cdmabean);
The catch (ClassNotFoundException classnotfoundexception) {}}//End CDMA network super.oncelllocationchanged (location); }//End oncelllocationchanged @Override public void onservicestatechanged (ServiceState servicestate) {//TODO Auto-gener
Ated method Stub super.onservicestatechanged (servicestate); @Override public void onsignalstrengthschanged (Signalstrength signalstrength) {//TODO auto-generated method stub int A
Su=signalstrength.getgsmsignalstrength (); Lastsignal=-113+2*asu;
Signal Strength super.onsignalstrengthschanged (signalstrength);
}
}; }//End Initphonestatelistener

The above is a small series for everyone to get the Android phone GSM/CDMA signal information, and access to the base station information of the entire content of the method, I hope that we support cloud-Habitat Community ~

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.