Android手机安全码编程知识

什么是安全码?

在android系统中,安全码就是类似这种样式的字符串: *#*#<code>#*#*

如果这样的系统安全码执行,系统会触发下面的方法:(来自 AOSP Android Open Source Project)

static private boolean handleSecretCode(Context context, String input) {
    int len = input.length();
    if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) {
        Intent intent = new Intent(TelephonyIntents.SECRET_CODE_ACTION,
                Uri.parse("android_secret_code://" + input.substring(4, len - 4)));
        context.sendBroadcast(intent);
        return true;
    }

    return false;
}

如何运行安全码?

有两种方式可以执行运行安全码:

直接在手机电话拨号界面输入安全码,例如:*#*#123456789#*#*

或者直接在代码中进行调用。

String secretCode = "123456789";
Intent intent = new Intent(Intent.ACTION_DIAL);    
intent.setData(Uri.parse("tel:*#*#" + secretCode + "#*#*"));
startActivity(intent);
String secretCode = "123456789";
String action = "android.provider.Telephony.SECRET_CODE";
Uri uri = Uri.parse("android_secret_code://" + secretCode);
Intent intent = new Intent(action, uri);
sendBroadcast(intent);

如何创建自定义安全码?

在你的应用AndroidManifest.xml文件中,增加以下代码,用来定义手机安全码。

不管什么时候安全码 *#*#123456789#*#* 被触发,你都将收到对应的广播。

<receiver android:name=".MySecretCodeReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SECRET_CODE" />
        <data android:scheme="android_secret_code" android:host="123456789" />
    </intent-filter>
</receiver>

本文文字及图片出自 www.jianshu.com

余下全文(1/3)
分享这篇文章:

请关注我们:

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注