Android - 处理更改 apn 连接

发布于 2024-12-22 05:55:27 字数 227 浏览 2 评论 0原文

我想知道 Android 是如何做到这一点的。

示例

  1. WAP 推送表示收到彩信。
  2. 将 APN 更改为彩信。
  3. 下载彩信。
  4. 将连接恢复到默认 APN。

那么这个APN的改变是如何完成的呢?

为什么: 我想使用其他 APN 而不是默认或彩信 APN 来连接到互联网。

I was wondering how Android is doing that.

Example:

  1. WAP Push indicates an incoming MMS.
  2. Change APN to MMS.
  3. Download MMS.
  4. Restore connectivity to default APN.

So how is this change of APN done?

Why:
I want to use an other APN to connect to the internet instead of the default or MMS APN.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2024-12-29 05:55:27

以下活动解决了更改默认 APN 并恢复到原始状态的问题。

package com.slk.apnapp;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class NewAPNActivity extends Activity {
/*
 * Information of all APNs Details can be found in
 * com.android.providers.telephony.TelephonyProvider
 */
public static final Uri APN_TABLE_URI = Uri
        .parse("content://telephony/carriers");
/*
 * Information of the preferred APN
 */
public static final Uri PREFERRED_APN_URI = Uri
        .parse("content://telephony/carriers/preferapn");
private static final String TAG = "CHANGE_APN";
public static final String NEW_APN = "NewAPN";

private int getDafaultAPN() {
    Cursor c = this.getContentResolver().query(PREFERRED_APN_URI,
            new String[] { "_id", "name" }, null, null, null);
    int id = -1;
    if (c != null) {
        try {
            if (c.moveToFirst())
                id = c.getInt(c.getColumnIndex("_id"));
        } catch (SQLException e) {
            Log.d(TAG, e.getMessage());
        }
        c.close();
    }
    return id;

}

/*
 * Set an apn to be the default apn for web traffic Require an input of the
 * apn id to be set
 */
public boolean setDefaultAPN(int id) {
    boolean res = false;
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();

    // See /etc/apns-conf.xml. The TelephonyProvider uses this file to
    // provide
    // content://telephony/carriers/preferapn URI mapping
    values.put("apn_id", id);
    try {
        resolver.update(PREFERRED_APN_URI, values, null, null);
        Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name",
                "apn" }, "_id=" + id, null, null);
        if (c != null) {
            res = true;
            c.close();
        }
    } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
    }
    return res;
}

private int checkNewAPN() {
    int id = -1;
    Cursor c = this.getContentResolver().query(APN_TABLE_URI,
            new String[] { "_id", "name" }, "name=?",
            new String[] { NEW_APN }, null);
    if (c == null) {
        id = -1;
    } else {
        int record_cnt = c.getCount();
        if (record_cnt == 0) {
            id = -1;
        } else if (c.moveToFirst()) {
            if (c.getString(c.getColumnIndex("name")).equalsIgnoreCase(
                    NEW_APN)) {
                id = c.getInt(c.getColumnIndex("_id"));
            }
        }
        c.close();
    }
    return id;
}

public int addNewAPN() {
    int id = -1;
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();
    values.put("name", NEW_APN);
    values.put("apn", NEW_APN);

    /*
     * The following three field values are for testing in Android emulator
     * only The APN setting page UI will ONLY display APNs whose 'numeric'
     * filed is TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC. On
     * Android emulator, this value is 310260, where 310 is mcc, and 260
     * mnc. With these field values, the newly added apn will appear in
     * system UI.
     */
    values.put("mcc", "310");
    values.put("mnc", "260");
    values.put("numeric", "310260");

    Cursor c = null;
    try {
        Uri newRow = resolver.insert(APN_TABLE_URI, values);
        if (newRow != null) {
            c = resolver.query(newRow, null, null, null, null);

            // Obtain the apn id
            int idindex = c.getColumnIndex("_id");
            c.moveToFirst();
            id = c.getShort(idindex);
            Log.d(TAG, "New ID: " + id + ": Inserting new APN succeeded!");
        }
    } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
    }

    if (c != null)
        c.close();
    return id;
}

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

    if (id == -1) {
        id = addNewAPN();
    }

    if (setDefaultAPN(id)) {
        Log.i(TAG, NEW_APN
                + " set new default APN successfully and Default id is "
                + id);
    }
    if (setDefaultAPN(default_id)) {
        Log.i(TAG,
                NEW_APN
                        + " set previous default APN successfully and Default id is "
                        + default_id);
    }

}
}

Following Activity solves your problem of changing default APN and than revert back to original.

package com.slk.apnapp;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class NewAPNActivity extends Activity {
/*
 * Information of all APNs Details can be found in
 * com.android.providers.telephony.TelephonyProvider
 */
public static final Uri APN_TABLE_URI = Uri
        .parse("content://telephony/carriers");
/*
 * Information of the preferred APN
 */
public static final Uri PREFERRED_APN_URI = Uri
        .parse("content://telephony/carriers/preferapn");
private static final String TAG = "CHANGE_APN";
public static final String NEW_APN = "NewAPN";

private int getDafaultAPN() {
    Cursor c = this.getContentResolver().query(PREFERRED_APN_URI,
            new String[] { "_id", "name" }, null, null, null);
    int id = -1;
    if (c != null) {
        try {
            if (c.moveToFirst())
                id = c.getInt(c.getColumnIndex("_id"));
        } catch (SQLException e) {
            Log.d(TAG, e.getMessage());
        }
        c.close();
    }
    return id;

}

/*
 * Set an apn to be the default apn for web traffic Require an input of the
 * apn id to be set
 */
public boolean setDefaultAPN(int id) {
    boolean res = false;
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();

    // See /etc/apns-conf.xml. The TelephonyProvider uses this file to
    // provide
    // content://telephony/carriers/preferapn URI mapping
    values.put("apn_id", id);
    try {
        resolver.update(PREFERRED_APN_URI, values, null, null);
        Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name",
                "apn" }, "_id=" + id, null, null);
        if (c != null) {
            res = true;
            c.close();
        }
    } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
    }
    return res;
}

private int checkNewAPN() {
    int id = -1;
    Cursor c = this.getContentResolver().query(APN_TABLE_URI,
            new String[] { "_id", "name" }, "name=?",
            new String[] { NEW_APN }, null);
    if (c == null) {
        id = -1;
    } else {
        int record_cnt = c.getCount();
        if (record_cnt == 0) {
            id = -1;
        } else if (c.moveToFirst()) {
            if (c.getString(c.getColumnIndex("name")).equalsIgnoreCase(
                    NEW_APN)) {
                id = c.getInt(c.getColumnIndex("_id"));
            }
        }
        c.close();
    }
    return id;
}

public int addNewAPN() {
    int id = -1;
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();
    values.put("name", NEW_APN);
    values.put("apn", NEW_APN);

    /*
     * The following three field values are for testing in Android emulator
     * only The APN setting page UI will ONLY display APNs whose 'numeric'
     * filed is TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC. On
     * Android emulator, this value is 310260, where 310 is mcc, and 260
     * mnc. With these field values, the newly added apn will appear in
     * system UI.
     */
    values.put("mcc", "310");
    values.put("mnc", "260");
    values.put("numeric", "310260");

    Cursor c = null;
    try {
        Uri newRow = resolver.insert(APN_TABLE_URI, values);
        if (newRow != null) {
            c = resolver.query(newRow, null, null, null, null);

            // Obtain the apn id
            int idindex = c.getColumnIndex("_id");
            c.moveToFirst();
            id = c.getShort(idindex);
            Log.d(TAG, "New ID: " + id + ": Inserting new APN succeeded!");
        }
    } catch (SQLException e) {
        Log.d(TAG, e.getMessage());
    }

    if (c != null)
        c.close();
    return id;
}

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

    if (id == -1) {
        id = addNewAPN();
    }

    if (setDefaultAPN(id)) {
        Log.i(TAG, NEW_APN
                + " set new default APN successfully and Default id is "
                + id);
    }
    if (setDefaultAPN(default_id)) {
        Log.i(TAG,
                NEW_APN
                        + " set previous default APN successfully and Default id is "
                        + default_id);
    }

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文