当前位置:   article > 正文

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

android10 添加apn

介绍

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

修改

首先创建ApnReceiver继承BroadcastReceiver

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

  1. package com.android.settings;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.util.Log;
  7. import android.database.Cursor;
  8. import android.content.ContentResolver;
  9. import android.content.ContentValues;
  10. import android.provider.Telephony;
  11. public final class ApnReceiver extends BroadcastReceiver {
  12. private static final String TAG = "ApnReceiver";
  13. private Context mContext;
  14. public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
  15. public static final Uri CURRENT_APN_URI =
  16. Uri.parse("content://telephony/carriers/preferapn");
  17. private Cursor mCursor;
  18. private int ID_5G = -1;
  19. private int ID_CTNET = -1;
  20. public void SetAPN(int id) {
  21. Log.i(TAG,"SetAPN , id = "+id);
  22. ContentResolver resolver = mContext.getContentResolver();
  23. ContentValues values = new ContentValues();
  24. values.put("apn_id", id);
  25. resolver.update(CURRENT_APN_URI, values, null, null);
  26. }
  27. @Override
  28. public void onReceive(Context context, Intent intent) {
  29. if (intent.getAction() == null) {
  30. return;
  31. }
  32. mContext = context;
  33. if ("com.xxx.ACTION_SET_APN_POLICE".equals(intent.getAction())) {
  34. getAPN();
  35. if(ID_5G != -1){
  36. SetAPN(ID_5G);
  37. }
  38. } else if ("com.xxx.ACTION_SET_APN_CT".equals(intent.getAction())) {
  39. getAPN();
  40. if(ID_CTNET != -1){
  41. SetAPN(ID_CTNET);
  42. }
  43. } else {
  44. Log.i(TAG, "Not ACTION!");
  45. }
  46. }
  47. public void getAPN() {
  48. Cursor cr = mContext.getContentResolver().query(APN_URI, null, null, null, null);
  49. if (cr != null) {
  50. while (cr.moveToNext()) {
  51. String name = cr.getString(cr.getColumnIndex(Telephony.Carriers.NAME));
  52. String apn = cr.getString(cr.getColumnIndex(Telephony.Carriers.APN));
  53. String mcc = cr.getString(cr.getColumnIndex(Telephony.Carriers.MCC));
  54. String mnc = cr.getString(cr.getColumnIndex(Telephony.Carriers.MNC));
  55. if (name != null && name.equals("CTNET") && apn.equals("ctnet") && mcc.equals("460") && mnc.equals("11")) {
  56. ID_CTNET = cr.getInt(cr.getColumnIndex(Telephony.Carriers._ID));
  57. Log.d(TAG, "ID_CTNET = " + ID_CTNET);
  58. } else if (name != null && name.equals("5G") && apn.equals("cthzsgaj.5gzx.gd") && mcc.equals("460") && mnc.equals("11")) {
  59. ID_5G = cr.getInt(cr.getColumnIndex(Telephony.Carriers._ID));
  60. Log.d(TAG, "ID_5G = " + ID_5G);
  61. }
  62. }
  63. cr.close();
  64. }
  65. }
  66. }

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

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

  1. <!-- soda water.20230916 Switching apn -->
  2. <receiver
  3. android:exported="true"
  4. android:name=".ApnReceiver">
  5. <intent-filter>
  6. <action android:name="com.xxx.ACTION_SET_APN_POLICE" />
  7. <action android:name="com.xxx.ACTION_SET_APN_CT" />
  8. </intent-filter>
  9. </receiver>
  10. <!-- soda water.20230916 Switching apn -->

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/988224
推荐阅读
相关标签
  

闽ICP备14008679号