Android 10.0 通过广播切换APN-在Settings中加入

介绍

因客户的一些需求希望系统上能写好接口通过广播去切换指定的APN,这里我们是将ApnReceiver放在Settings下了,

修改

首先创建ApnReceiver继承BroadcastReceiver

路径:vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/ApnReceiver.java


package com.android.settings;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import android.database.Cursor;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.provider.Telephony;

public final class ApnReceiver extends BroadcastReceiver {

    private static final String TAG = "ApnReceiver";
    private Context mContext;
    public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
    public static final Uri CURRENT_APN_URI = 
    Uri.parse("content://telephony/carriers/preferapn");
    private Cursor mCursor;
    private int ID_5G = -1;
    private int ID_CTNET = -1;
    public void SetAPN(int id) {
        Log.i(TAG,"SetAPN , id = "+id);
        ContentResolver resolver = mContext.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", id);
        resolver.update(CURRENT_APN_URI, values, null, null);
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) {
            return;
        }
        mContext = context;

        if ("com.xxx.ACTION_SET_APN_POLICE".equals(intent.getAction())) {
        getAPN();
        if(ID_5G != -1){
        SetAPN(ID_5G);
        }
        } else if ("com.xxx.ACTION_SET_APN_CT".equals(intent.getAction())) {
        getAPN();
        if(ID_CTNET != -1){
        SetAPN(ID_CTNET);
        }
        } else {
            Log.i(TAG, "Not ACTION!");
        }
    }

    public void getAPN() {
                Cursor cr = mContext.getContentResolver().query(APN_URI, null, null, null, null);
                if (cr != null) {
                    while (cr.moveToNext()) {
                        String name = cr.getString(cr.getColumnIndex(Telephony.Carriers.NAME));
                        String apn = cr.getString(cr.getColumnIndex(Telephony.Carriers.APN));
                        String mcc = cr.getString(cr.getColumnIndex(Telephony.Carriers.MCC));
                        String mnc = cr.getString(cr.getColumnIndex(Telephony.Carriers.MNC));

                        if (name != null && name.equals("CTNET") && apn.equals("ctnet") && mcc.equals("460") && mnc.equals("11")) {
                            ID_CTNET = cr.getInt(cr.getColumnIndex(Telephony.Carriers._ID));
                            Log.d(TAG, "ID_CTNET = " + ID_CTNET);
                        } else if (name != null && name.equals("5G") && apn.equals("cthzsgaj.5gzx.gd") && mcc.equals("460") && mnc.equals("11")) {
                            ID_5G = cr.getInt(cr.getColumnIndex(Telephony.Carriers._ID));
                            Log.d(TAG, "ID_5G = " + ID_5G);
                        }
                    }
                    cr.close();
                }
        }
    


}

这里我们注册广播需要注意的是”android:exported”一定要为true否则其他应用无法调用此广播,另一点需要注意的是在Androud9.0后如果是静态注册的广播想要调用需要在调用时指定ComponentName。

路径:vendor/mediatek/proprietary/packages/apps/MtkSettings/AndroidManifest.xml

       <!-- soda water.20230916  Switching apn -->
        <receiver
            android:exported="true"
            android:name=".ApnReceiver">
            <intent-filter>
                <action android:name="com.xxx.ACTION_SET_APN_POLICE" />
                <action android:name="com.xxx.ACTION_SET_APN_CT" />
            </intent-filter>
        </receiver>
       <!-- soda water.20230916  Switching apn -->

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不太正常的移动开发工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值