安卓随手笔记六:(定位系统 下)

来源:互联网 发布:网络兼职宣传语 编辑:程序博客网 时间:2024/04/30 13:59

上篇文章中简要的说明了一下gps定位与agps定位各自的优点和缺点,可以根据自己的情况选择使用。

下面我们就说明一下gprs定位的使用方法。

我们首先判断一下agps是否开启

    /**
     * 判断是否打开AGPS
     */
    public boolean isOpenAgps(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位)
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

打开gprs的操作

    /**
     * 打开移动网络(gprs) 0 成功 -1失败
     */
    public int openGprs() {
        boolean isOpen = isOpenAgps(mContext);
        if (false == isOpen) {
            int sucess = setMobileDataEnabled(mContext, true);
            return sucess;
        }
        return 0;
    }


0 0