Android 移动基站地位内容

package com.wmi;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class wmiact extends Activity {
    // 文字输出
    private TextView myTextView;
    private Button myBtnRefresh;
    private OnClickListener myBtnCallfn;

    /** on Refresh Button Click */
    public void clickHandler(View v) {
        downloadAndShowInternetFile();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 使用findViewById函数,利用ID找到该TextView对象
        myTextView = (TextView) findViewById(R.id.myTextView);
        String strTitle = "正在获取";

        // 利用setText方法将TextView文字改变为welcom_mes
        myTextView.setText(strTitle);

        // 刷新按钮
        myBtnRefresh = (Button) findViewById(R.id.BtnRefresh);
        strTitle = "刷新";
        myBtnRefresh.setText(strTitle);
        myBtnCallfn = new OnClickListener() {
            public void onClick(View v) {
                clickHandler(v);
            }
        };
        myBtnRefresh.setOnClickListener(myBtnCallfn);

        // 解码
        downloadAndShowInternetFile();
    }

    // 网络解码
    void downloadAndShowInternetFile() {
        myTextView.setText("正在获取");

        TelephonyManager mTelephonyManager = (TelephonyManager) this
                .getSystemService(Service.TELEPHONY_SERVICE);
        if (mTelephonyManager == null) {
            ShowDebugInfo("mTelephonyManager == null");
            return;
        }
        GsmCellLocation mGsmCellLocation = (GsmCellLocation) mTelephonyManager
                .getCellLocation();
        int cid;
        int lac;
        if (mGsmCellLocation == null) {
            ShowDebugInfo("无法获得Gsm对象");
            cid = 7272;
            lac = 28709;
        } else {
            ShowDebugInfo("获得了gms对象");
            cid = mGsmCellLocation.getCid();
            lac = mGsmCellLocation.getLac();

        }
        String strOutput = "CellID:" + cid + ", LAC:" + lac + "/n";

        if (cid == -1 || lac == -1) {
            myTextView.setText(strOutput + "无法获取本手机基站数据");
            return;
        }

        strOutput += "基站定位:/n经度:";
        String strCid = "http://www.anttna.com/cell2gps/cell2gps.php?lac="
                + lac + "&cellid=" + cid;
        ;
        String strRS = GetCIDName(strCid);

        myTextView.setText(strOutput +strRS);
        
        if (strRS.equals("无效结果")) {
            myTextView.setText(strOutput + "服务器无法获取地址信息,可尝试刷新");
            return;
        }
        String[] strSP = strRS.split(",");
        strOutput += strSP[0];
        strOutput += "/n";
        strOutput += "纬度:";
        strOutput += strSP[1];
        strOutput += "/n";
        String[] strSP2 = strSP[2].split(";");
        strOutput += "省份:";
        strOutput += strSP2[0].substring(1);
        strOutput += "/n";
        strOutput += "城市:";
        strOutput += strSP2[1];
        strOutput += "/n";
        strOutput += "区:";
        strOutput += strSP2[2];
        strOutput += "/n";
        strOutput += "街道:";
        strOutput += strSP2[3];
        strOutput += "/n";
        strOutput += "周边:";
        strOutput += strSP2[4];
        strOutput += "/n";
        myTextView.setText(strOutput);
    }

    // 网络IO访问
    public String GetCIDName(String url) {
        URL internetUrl = null;
        String strRS = new String("无效结果");
        int length = -1;

        try {
            internetUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            ShowDebugInfo("网络连接异常,地址错误");
            return strRS;
        }

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) internetUrl.openConnection();
            conn.setDoInput(true);
            conn.setConnectTimeout(2000);
            byte[] data = new byte[4096];
            for (int i = 0; i < 4096; i++) {
                data[i] = 0;
            }
            conn.connect();

            InputStream is = conn.getInputStream();
            length = is.read(data);
            strRS = new String(data, "GBK");
        } catch (IOException e) {
            e.printStackTrace();
            ShowDebugInfo(e.getMessage());
            // myTextView.setText(e.getMessage());
        } finally {
            if (conn != null) {
                conn.disconnect();
                conn = null;
                internetUrl = null;
            }
        }

        if (length == -1 || strRS.length() < 10) {
            return strRS;
        }

        ShowDebugInfo("网络获取成功");
        return strRS.substring(0, length);
    }

    // 调试输出
    public void ShowDebugInfo(String txt) {
        Toast.makeText(this, txt, Toast.LENGTH_LONG).show();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值