Android设置暗码简要流程

您所在的位置:网站首页 安卓设置向导是什么 Android设置暗码简要流程

Android设置暗码简要流程

2023-08-27 21:42| 来源: 网络整理| 查看: 265

设置暗码 1.Phone对暗码的简要处理流程

路径:packages/services/Telephony/src/com/android/phone/SpecialCharSequenceMgr.java phone中对暗码的处理

/** * Handles secret codes to launch arbitrary receivers in the form of *#*##*#*. * If a secret code is encountered, an broadcast intent is sent with the * android_secret_code:// URI. * * @param input the text to check for a secret code in * @return true if a secret code was encountered and intent is sent out */ static private boolean handleSecretCode(String input) { // Secret codes are in the form *#*##*#* int len = input.length(); if (len > 8 && input.startsWith("*#*#") && input.endsWith("#*#*")) { final Phone phone = PhoneGlobals.getPhone(); phone.sendDialerSpecialCode(input.substring(4, len - 4)); return true; } return false; }

路径:packages/services/Telephony/src/com/android/phone/SpecialCharSequenceMgr.java

/** * Send the dialer code if called from the current default dialer or the caller has * carrier privilege. * @param inputCode The dialer code to send */ @Override public void sendDialerSpecialCode(String callingPackage, String inputCode) { final Phone defaultPhone = getDefaultPhone(); mAppOps.checkPackage(Binder.getCallingUid(), callingPackage); TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class); String defaultDialer = tm.getDefaultDialerPackage(); if (!TextUtils.equals(callingPackage, defaultDialer)) { TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp, getDefaultSubscription(), "sendDialerSpecialCode"); } final long identity = Binder.clearCallingIdentity(); try { defaultPhone.sendDialerSpecialCode(inputCode); } finally { Binder.restoreCallingIdentity(identity); } }

路径:frameworks/opt/telephony/src/java/com/android/internal/telephony/Phone.java 发送暗码广播

/** * send secret dialer codes to launch arbitrary activities. * an Intent is started with the android_secret_code:// URI. * * @param code stripped version of secret code without *#*# prefix and #*#* suffix */ public void sendDialerSpecialCode(String code) { if (!TextUtils.isEmpty(code)) { final BroadcastOptions options = BroadcastOptions.makeBasic(); options.setBackgroundActivityStartsAllowed(true); Intent intent = new Intent(TelephonyIntents.SECRET_CODE_ACTION, Uri.parse("android_secret_code://" + code)); intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); mContext.sendBroadcast(intent, null, options.toBundle()); // {@link TelephonyManager.ACTION_SECRET_CODE} will replace {@link // TelephonyIntents#SECRET_CODE_ACTION} in the next Android version. Before // that both of these two actions will be broadcast. Intent secrectCodeIntent = new Intent(TelephonyManager.ACTION_SECRET_CODE, Uri.parse("android_secret_code://" + code)); secrectCodeIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); mContext.sendBroadcast(secrectCodeIntent, null, options.toBundle()); } }

路径:frameworks/base/telephony/java/android/telephony/TelephonyManager.java

/** * Send the special dialer code. The IPC caller must be the current default dialer or have * carrier privileges (see {@link #hasCarrierPrivileges}). * * @param inputCode The special dialer code to send * * @throws SecurityException if the caller does not have carrier privileges or is not the * current default dialer */ ublic void sendDialerSpecialCode(String inputCode) { try { final ITelephony telephony = getITelephony(); if (telephony == null) { if (!isSystemProcess()) { throw new RuntimeException("Telephony service unavailable"); } return; } telephony.sendDialerSpecialCode(mContext.getOpPackageName(), inputCode); } catch (RemoteException ex) { // This could happen if binder process crashes. if (!isSystemProcess()) { ex.rethrowAsRuntimeException(); } }

以上就是phone对暗码的处理简要流程,所以只要接收广播进行处理即可.

2.简单例子

路径:packages/apps/Settings/AndroidManifest.xml 声明接收器,定义自己的暗码

路径:packages/apps/Settings/src/com/android/settings/CustomizeScretCodeReceiver.java 实现自己的接收器类,进行逻辑处理,此处为显示了一个dialog

public class CustomizeScretCodeReceiver extends BroadcastReceiver { private final String Tag = "CustomizeScretCodeReceiver"; private final String SECRET_CODE = "android.provider.Telephony.SECRET_CODE"; private final Uri mInformationUri = Uri.parse("android_secret_code://888"); private final URI mHardwareInfoUri = Uri.parse("android_secret_code://666"); //重写onReceive方法 @SuppressLint("LongLogTag") @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (SECRET_CODE.equals(action)) { Uri uri = intent.getData(); if ( mHardwareInfoUri.equals(uri)) { showHardwareInfoDialog(context); }else { android.util.Log.e(Tag, "Unsupport uri:" + uri); } } } private void showHardwareInfoDialog(Context context) { StringBuffer msg = new StringBuffer("") .append(readHardwareInfo()) .append("\n"); AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext()) .setTitle(context.getResources().getString(R.string.hardware_information)) .setMessage(msg) .setPositiveButton("ok", null); AlertDialog dialog = builder.create(); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.show(); } private String readHardwareInfo(){ return "HardwareInfo"; }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3