In BaseStationInfoHelper.java
package com.main;
import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
public class BaseStationInfoHelper {
//android.permission.ACCESS_COARSE_LOCATION
public static class BaseStationInfo {
/** Country */
public int mcc = -1;
/** ISP */
public int mnc = -1;
/** Base Station Number*/
public int lac = -1;
/** Small Region Number */
public int cid = -1;
}
public static BaseStationInfo getSimCardInfo(Context context) {
final TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
BaseStationInfo res = new BaseStationInfo();
CellLocation clll = telephony.getCellLocation();
if (clll == null) {
return res;
}
if (clll instanceof GsmCellLocation) {
GsmCellLocation gsm = (GsmCellLocation) clll;
int lac = gsm.getLac();
String mcc = telephony.getNetworkOperator().substring(0, 3);
String mnc = telephony.getNetworkOperator().substring(3);
res.cid = gsm.getCid();
res.mcc = Integer.parseInt(mcc);
res.mnc = Integer.parseInt(mnc);
res.lac = lac;
} else if (clll instanceof CdmaCellLocation) {
CdmaCellLocation cdma = (CdmaCellLocation) clll;
int lac = cdma.getNetworkId();
String mcc = telephony.getNetworkOperator().substring(0, 3);
String mnc = telephony.getNetworkOperator().substring(3);
int cid = cdma.getBaseStationId();
res.cid = cid;
res.mcc = Integer.parseInt(mcc);
res.mnc = Integer.parseInt(mnc);
res.lac = lac;
}
return res;
}
}
In MainActivity.java
package com.main;
import com.example.celllocation.R;
import com.main.BaseStationInfoHelper.BaseStationInfo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
TextView tv = (TextView) this.findViewById(R.id.textView1);
//
BaseStationInfo info = BaseStationInfoHelper.getSimCardInfo(this);
String displayString = "";
if(info != null){
displayString += "Country mcc = " + info.mcc + "\r\n";
displayString += "ISP mnc = " + info.mnc + "\r\n";
displayString += "Base Station Number lac = " + info.lac + "\r\n";
displayString += "Cell id = " + info.cid + "\r\n";
//
tv.setText(displayString);
}
else{
tv.setText("Cannot get the base station information.");
}
}
}
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView"
android:textSize="20sp" />
</RelativeLayout>
In AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.celllocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<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.main.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
執行結果:

作者已經移除這則留言。
回覆刪除