Android 通过计算器暗码启动应用

项目不带通话功能,要求通过计算器输入暗码打开测试应用。

  • 查看布局文件 数字显示控件
    packages\apps\ExactCalculator\res\layout\display_one_line.xml
xml 复制代码
...

        <com.android.calculator2.CalculatorScrollView
            android:id="@+id/formula_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:overScrollMode="never"
            android:scrollbars="none">
			<!--用户输入显示控件-->
            <com.android.calculator2.CalculatorFormula
                android:id="@+id/formula"
                style="@style/DisplayTextStyle.Formula"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="bottom|end"
                android:ellipsize="none"
                android:gravity="bottom|end"
                android:longClickable="true"
                android:singleLine="true"
                android:textColor="@color/display_formula_text_color"
                android:textIsSelectable="false" />

        </com.android.calculator2.CalculatorScrollView>
...
  • 自定义控件CalculatorFormula最终继承了TextView,并且重写了onTextChanged方法。

packages/apps/ExactCalculator/src/com/android/calculator2/CalculatorFormula.java

java 复制代码
package com.android.calculator2;

...
import android.content.Intent;///mh.modify 

/**
 * TextView adapted for displaying the formula and allowing pasting.
 */
public class CalculatorFormula extends AlignedTextView implements MenuItem.OnMenuItemClickListener,
        ClipboardManager.OnPrimaryClipChangedListener {

    public static final String TAG_ACTION_MODE = "ACTION_MODE";

	...
    
    private Context mContext;///mh.modify

    public CalculatorFormula(Context context) {
        this(context, null /* attrs */);
    }

    public CalculatorFormula(Context context, AttributeSet attrs) {
        this(context, attrs, 0 /* defStyleAttr */);
    }
	//修改构造方法,context启动应用时需要
    public CalculatorFormula(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;//mh.modify

        mClipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

        final TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.CalculatorFormula, defStyleAttr, 0);
        mMaximumTextSize = a.getDimension(
                R.styleable.CalculatorFormula_maxTextSize, getTextSize());
        mMinimumTextSize = a.getDimension(
                R.styleable.CalculatorFormula_minTextSize, getTextSize());
        mStepTextSize = a.getDimension(R.styleable.CalculatorFormula_stepTextSize,
                (mMaximumTextSize - mMinimumTextSize) / 3);
        a.recycle();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setupActionMode();
        } else {
            setupContextMenu();
        }
    }
	
	...

    ///mh.modify start 20190615
    private void handleSecretCode(Context context, String input) {
        android.util.Log.d("mh.log", "input:"+input);
		int len = input.length();
		if (len >= 8 && input.startsWith("!^!^") && input.endsWith("^!^!")) {
			Intent intent = new Intent();
			if("!^!^6,868^!^!".equals(input)) {
				intent.setClassName("com.xckj.factorytest", "com.xckj.factorytest.FactoryTestEntry");
			}
            
            if("!^!^3,646,633^!^!".equals(input)) {
				intent.setClassName("com.mediatek.engineermode", "com.mediatek.engineermode.EngineerMode");
			}
			try {
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
			}catch(Exception e){}
		}
	}
    ///mh.modify end 20190615
    
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);

        setTextSize(TypedValue.COMPLEX_UNIT_PX, getVariableTextSize(text.toString()));
        
        handleSecretCode(mContext, text.toString());//调用
    }

	...
相关推荐
Ya-Jun3 小时前
常用第三方库精讲:cached_network_image图片加载优化
android·flutter
_一条咸鱼_3 小时前
Android嵌套滑动详解
android·面试·android jetpack
zhishishe6 小时前
工具指南:免费将 PDF 转换为 Word 的 10 个工具
android·windows·pdf·word
孙同学_6 小时前
【MySQL】004.MySQL数据类型
android·数据库·mysql
流浪汉kylin8 小时前
Android 图片选择器改系统
android
前行的小黑炭9 小时前
Android 上下位机开发:串口是什么,为什么android版本都比较低?粘包半包的原因以及处理思路,缓冲区处理,以及超时清空缓冲区....
android
移动开发者1号9 小时前
你知道Android中配置resourcePrefix的作用吗?
android
tangweiguo030519879 小时前
Android Compose 系统 Scope 的优化实践
android
我命由我123459 小时前
Android Cordova 开发 - Cordova 快速入门(Cordova 环境配置、Cordova 第一个应用程序)
android·开发语言·前端框架·android studio·h5·安卓·android-studio
老板来根葱10 小时前
应用进程创建二三事
android·源码阅读