android 获取基站信息

package com.example.gsmcelllocation;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/**
 * 功能描述:通过手机信号获取基站信息
 * # 通过TelephonyManager 获取lac:mcc:mnc:cell-id
 * # MCC,Mobile Country Code,移动国家代码(中国的为460);
 * # MNC,Mobile Network Code,移动网络号码(中国移动为0,中国联通为1,中国电信为2); 
 * # LAC,Location Area Code,位置区域码;
 * # CID,Cell Identity,基站编号;
 * # BSSS,Base station signal strength,基站信号强度。
 * @author android_ls
 */
public class GSMCellLocationActivity extends Activity {
    private static final String TAG = "GSMCellLocationActivity";
    TextView MCCtext,MNCtext,LACtext,CIDtext,SBtext,numtext;
    
    int lac;
    int cellId;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gsmcell_location);
        MCCtext = (TextView)findViewById(R.id.textViewMCC);
        MNCtext = (TextView)findViewById(R.id.textViewMNC);
        LACtext = (TextView)findViewById(R.id.textViewLAC);
        CIDtext = (TextView)findViewById(R.id.textViewCID);
        SBtext = (TextView)findViewById(R.id.textView7SB);
        // 获取基站信息
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                //设备是否装有sim卡
                if(mTelephonyManager.SIM_STATE_ABSENT == mTelephonyManager.getSimState())
            	{
            		Log.i(TAG,"SIM STATE ABSENT");
            		return ;
            	}
                // 返回值MCC + MNC
                String operator = mTelephonyManager.getNetworkOperator();
                int mcc = Integer.parseInt(operator.substring(0, 3));
                int mnc = Integer.parseInt(operator.substring(3));
                
                if(mTelephonyManager.PHONE_TYPE_GSM == mTelephonyManager.getPhoneType())
            	{
            		// 中国移动和中国联通获取LAC、CID的方式  
            		GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();  
            		lac = location.getLac();  
            		cellId = location.getCid();  
            	}
            	else if(mTelephonyManager.PHONE_TYPE_CDMA == mTelephonyManager.getPhoneType())
            	{
            		// 中国电信获取LAC、CID的方式  
                	CdmaCellLocation location1 = (CdmaCellLocation) mTelephonyManager.getCellLocation(); 
                	lac = location1.getNetworkId(); 
                	cellId = location1.getBaseStationId(); 
                	cellId /= 16;
            	}
                Log.i(TAG, " MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cellId);
                MCCtext.setText(""+mcc);
                MNCtext.setText(""+mnc);
                LACtext.setText(""+lac);
                CIDtext.setText(""+cellId);
                
                // 获取邻区基站信息
                List<NeighboringCellInfo> infos = mTelephonyManager.getNeighboringCellInfo();
                StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");
                for (NeighboringCellInfo info1 : infos) { // 根据邻区总数进行循环
                    sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC
                    sb.append(" CID : " + info1.getCid()); // 取出当前邻区的CID
                    sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度
                }
                Log.i(TAG, " 获取邻区基站信息:" + sb.toString());
                SBtext.setText(sb.toString());
            }
        });
    }
}


在AndroidManifest.xml添加获取位置信息的权限:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gsmcelllocation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gsmcelllocation.GSMCellLocationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_gsmcell_location.xml


<RelativeLayout 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"
    tools:context=".GSMCellLocationActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="
        MCC,Mobile Country Code,移动国家代码(中国的为460);
        MNC,Mobile Network Code,移动网络号码(中国移动为0,中国联通为1,中国电信为2);
        LAC,Location Area Code,位置区域码;
        CID,Cell Identity,基站编号;
        BSSS,Base station signal strength,基站信号强度。" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="22dp"
        android:text="MCC" />

    <TextView
        android:id="@+id/textViewMCC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="25dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="MNC" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="LAC" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="CID" />

    <TextView
        android:id="@+id/textView7SB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:text="TextView" />

    <TextView
        android:id="@+id/textViewLAC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewCID"
        android:layout_alignLeft="@+id/textViewMNC"
        android:text="TextView" />

    <TextView
        android:id="@+id/textViewCID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignLeft="@+id/textViewLAC"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="19dp"
        android:text="获取邻区基站信息" />

    <TextView
        android:id="@+id/textViewMNC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewMCC"
        android:layout_below="@+id/textViewMCC"
        android:text="TextView" />

</RelativeLayout>

源代码下载:
http://download.csdn.net/detail/u011212411/5839973


展开阅读全文

没有更多推荐了,返回首页