关于APN的一些东西2

关于布局文件没有什么好解释的,

直接看java的吧

MainActivity当中

  // 获取所有的APN所用的URI
    private static final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
     // 获取当前使用的APN所用的uri
    private static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

设置2个button的监听

一个是添加新的APN

// 设置添加APN的按钮监听
    private OnClickListener addOnClickListener = new OnClickListener() {
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            addAPN();
            // addNewApn();
        }
    };

调用addAPN()

  // 添加APN
    void addAPN()
    {
        Cursor cursor_all = getContentResolver().query(APN_TABLE_URI, null,
                null, null, null);// 所有的移动网络设置中的APN列表
        if (cursor_all.getCount() > 0)
        {
            while (cursor_all != null && cursor_all.moveToNext())
            {
                if (cursor_all.getString(cursor_all.getColumnIndex("proxy")).equals(
                        "10.0.0.200"))
                {
                    // ||cursor_all.getString(cursor_all.getColumnIndex("apn")).equals("ctnet")
                    Toast.makeText(MainActivity.this, "您所添加的APN已有直接设置为默认选择",
                            Toast.LENGTH_SHORT).show();
                    ContentResolver resolver = getContentResolver();
                    ContentValues values = new ContentValues();
                    values.put(
                            "apn_id",
                            cursor_all.getString(cursor_all.getColumnIndex("_id")));
                    resolver.update(PREFERRED_APN_URI, values, null, null);
                    break;
                }
                else
                {
                    addNewApn();
                    break;
                }
            }
        }
        else
        {
            addNewApn();
        }
    }

首先查询所有的apn如果返回的cursor返回的大小不为0代表有记录,(反之则没有记录直接调用 addNewApn();下面有解释)

下面开始循环while (cursor_all != null && cursor_all.moveToNext())如果不为空直接游标下移一个

在该循环里判断if (cursor_all.getString(cursor_all.getColumnIndex("proxy")).equals("10.0.0.200"))

当cursor里的proxy(代理)是10.0.0.200就代表已经有这条记录,

直接弹出对话框提示然后取出_id设置为默认选中。

Toast.makeText(MainActivity.this, "您所添加的APN已有直接设置为默认选择",
                            Toast.LENGTH_SHORT).show();
                    ContentResolver resolver = getContentResolver();
                    ContentValues values = new ContentValues();
                    values.put(
                            "apn_id",
                            cursor_all.getString(cursor_all.getColumnIndex("_id")));
                    resolver.update(PREFERRED_APN_URI, values, null, null);
                    break;

 这个apn系统保存在一个xml文件里,地址为:/data/data/com.android.providers.telephony/shared_prefs/preferred-apn.xml 。同样可以取出这个文件打开看看,里面内容很简单:

     <?xml version="1.0" encoding="utf-8" standalone="yes" ?>

        <map>
              <long name="apn_id" value="218" />
    </map>
    就是说当前apn设定为数据库中_id为218的那个apn了。
    要判断这个apn是wap还是net,最好是看proxy是不是10.0.0.172,因为apn字段是可以任意修改的,有可能用户把apn字段随便填写。
    对了,关于apn的操作相关代码在android源代码的packages\providers\TelephonyProvider\src\com\android\providers\telephony\TelephonyProvider.java中,有兴趣可以看看

看到上面的就明白   values.put("apn_id",cursor_all.getString(cursor_all.getColumnIndex("_id")));

这句的意思了~~取出_id将他存入到apn_id中使其成为默认(http://blog.csdn.net/liujian885/archive/2010/06/08/5656350.aspx)原文地址

如果没有这条记录会直接调用

 addNewApn();

注意无论是否为真都要停止循环,否则会出现多次循环添加的问题。


    void addNewApn()
    {
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", "cheer");
        values.put("apn", "cmnet");
        //values.put("proxy", "10.0.0.200");
        values.put("port", "80");
        values.put("user", "card");
        values.put("password", "card");
        values.put("mcc", getMCC());
        values.put("mnc", getMNC());
        values.put("numeric", getSimOperator());
//        values.put("name", "中国电信CTNET");
//        values.put("apn", "ctnet");
//        values.put("proxy", "10.0.0.200");
//        values.put("port", "80");
//        values.put("user", "card");
//        values.put("password", "card");
//        values.put("mcc", getMCC());
//        values.put("mnc", getMNC());
//        values.put("numeric", getSimOperator());
        Cursor c = null;
        Uri newRow = resolver.insert(APN_TABLE_URI, values);
        if (newRow != null)
        {
            c = resolver.query(newRow, null, null, null, null);
            int idindex = c.getColumnIndex("_id");
            c.moveToFirst();
            addID = c.getShort(idindex);
        }
    }
打开ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
直接添加

values里的key在(http://blog.csdn.net/liujian885/archive/2010/06/08/5656350.aspx)里面有 16个字段但是一般不会用这么多

最最重要的就是name,apn,mnc,mcc等几个

就连proxy和port都是可有可无

比较主要的是

   values.put("mcc", getMCC());
        values.put("mnc", getMNC());
        values.put("numeric", getSimOperator());
其实都差不多,只列举一个

// 获得mnc
    private String getMNC()
    {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String numeric = tm.getSimOperator();
        String mnc = numeric.substring(3, numeric.length());
        return mnc;
    }

TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法。其中包括手机SIM的状态和信息、电信网络的状态及手机用户的信息。在应用程序中可以使用这些get方法获取相关数据。

我们通过里面的方法来获取mnc,mmc等等的一些数据(http://www.cnblogs.com/linjiqin/archive/2011/02/26/1965682.html)可能不是所有的但是写的挺详细

将数据加入后还会有个判断

 if (newRow != null)
        {
            c = resolver.query(newRow, null, null, null, null);
            int idindex = c.getColumnIndex("_id");
            c.moveToFirst();
            addID = c.getShort(idindex);
        }

如果成功就返回不是空,查询这条记录,返回id,游标移到第一位,将id提出来。这样的好处就是以后我们有可能会用到这条记录进行新的操作等等。

我们返回oncreat方法,里面还有个按钮就是查询所有已知的apn设置。

点击后直接跳转界面到ShowAllApnActivity.java所有的操作在ShowAllApnActivity里进行。

这里是MainActivity的解释。

下一篇ShowAllApnActivity注解

posted @ 2011-06-15 10:00  飞翔的熊猫  阅读(1123)  评论(0编辑  收藏  举报