Android设置暗码简要流程

设置暗码

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

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

/**
     * Handles secret codes to launch arbitrary receivers in the form of *#*#<code>#*#*.
     * If a secret code is encountered, an broadcast intent is sent with the
     * android_secret_code://<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 *#*#<code>#*#*
        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://<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
声明接收器,定义自己的暗码

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

路径: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";
    }
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值