Android 系统源码 修改如何使用拨号键发广播(*#*#暗码#*#*)

1 在AndroidManifest.xml中添加:

<receiver android:name="com.android.settings.MyReceiver" > 
    <intent-filter> 
        <action android:name="android.provider.Telephony.SECRET_CODE" /> 
        <data android:host="1234567" android:scheme="android_secret_code" /> 
    </intent-filter> 
</receiver>

其中android:host=“1234567” 设置暗码

2 新建一个广播接收的类

public class MyReceiver extends BroadcastReceiver {
    private final String TAG = "MyReceiver";
    Uri engineerUri = Uri.parse("android_secret_code://1234567");
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
                Uri uri = intent.getData();
                if (uri.equals(engineerUri)) {
                    // TODO 这里处理相关事情
                }
            }
        }
    }
}

分析

/packages/apps/Dialer/java/com/android/dialer/app/SpecialCharSequenceMgr.java

static boolean handleSecretCode(Context context, String input) {
    // Secret codes are accessed by dialing *#*#<code>#*#*
    int len = input.length();
    if (len <= 8 || !input.startsWith("*#*#") || !input.endsWith("#*#*")) {
      return false;
    }
    String secretCode = input.substring(4, len - 4);
    TelephonyManagerCompat.handleSecretCode(context, secretCode);
    return true;
  }

handleSecretCode方法对输入的暗码进行解析,然后执行TelephonyManagerCompat.handleSecretCode方法。

/packages/apps/Dialer/java/com/android/contacts/common/compat/TelephonyManagerCompat.java

private static final String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
public static void handleSecretCode(Context context, String secretCode) {
    // Must use system service on O+ to avoid using broadcasts, which are not allowed on O+.
    if (BuildCompat.isAtLeastO()) {
      if (!TelecomUtil.isDefaultDialer(context)) {
        LogUtil.e(
            "TelephonyManagerCompat.handleSecretCode",
            "not default dialer, cannot send special code");
        return;
      }
      context.getSystemService(TelephonyManager.class).sendDialerSpecialCode(secretCode);
    } else {
      // System service call is not supported pre-O, so must use a broadcast for N-.
      Intent intent =
          new Intent(SECRET_CODE_ACTION, Uri.parse("android_secret_code://" + secretCode));
      context.sendBroadcast(intent);
    }
  }

此方法通过发送一条SECRET_CODE_ACTION,并携带Uri数据形式的广播,把消息传播出去。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值