赞
踩
因客户的一些需求希望系统上能写好接口通过广播去切换指定的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 -->
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。