赞
踩
在我们的手机的版本信息的地方连击三下会出现相应版本的logo
我们先来看看Google工程师是如何实现的:
- public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
- if (preference.getKey().equals(KEY_FIRMWARE_VERSION)) {
- System.arraycopy(mHits, 1, mHits, 0, mHits.length-1); //重要的是这两行代码
- mHits[mHits.length-1] = SystemClock.uptimeMillis(); //重要的是这两行代码
- if (mHits[0] >= (SystemClock.uptimeMillis()-500)) {
- Intent intent = new Intent(Intent.ACTION_MAIN);
- intent.setClassName("android",
- com.android.internal.app.PlatLogoActivity.class.getName());
- try {
- startActivity(intent);
- } catch (Exception e) {
- Log.e(LOG_TAG, "Unable to start activity " + intent.toString());
- }
- }
- } else if (preference.getKey().equals(KEY_BUILD_NUMBER)) {
- // Don't enable developer options for secondary users.
- if (UserHandle.myUserId() != UserHandle.USER_OWNER) return true;
-
- if (mDevHitCountdown > 0) {
- if (mDevHitCountdown == 1) {
- if (super.ensurePinRestrictedPreference(preference)) {
- return true;
- }
- }
- mDevHitCountdown--;
- if (mDevHitCountdown == 0) {
- getActivity().getSharedPreferences(DevelopmentSettings.PREF_FILE,
- Context.MODE_PRIVATE).edit().putBoolean(
- DevelopmentSettings.PREF_SHOW, true).apply();
- if (mDevHitToast != null) {
- mDevHitToast.cancel();
- }
- mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_on,
- Toast.LENGTH_LONG);
- mDevHitToast.show();
- } else if (mDevHitCountdown > 0
- && mDevHitCountdown < (TAPS_TO_BE_A_DEVELOPER-2)) {
- if (mDevHitToast != null) {
- mDevHitToast.cancel();
- }
- mDevHitToast = Toast.makeText(getActivity(), getResources().getQuantityString(
- R.plurals.show_dev_countdown, mDevHitCountdown, mDevHitCountdown),
- Toast.LENGTH_SHORT);
- mDevHitToast.show();
- }
- } else if (mDevHitCountdown < 0) {
- if (mDevHitToast != null) {
- mDevHitToast.cancel();
- }
- mDevHitToast = Toast.makeText(getActivity(), R.string.show_dev_already,
- Toast.LENGTH_LONG);
- mDevHitToast.show();
- }
- }
- return super.onPreferenceTreeClick(preferenceScreen, preference);
- }
System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
先来解释下参数的意思:
第一个参数是:原数组(要被拷贝的数组)
Google是这样写的,代表三击事件:
long[] mHits = new long[3];
第二个参数是:原数组拷贝位置的索引值
第三个参数是:目标数组
第四个参数是:目标数组接收值的索引位置
第五个参数是:拷贝的长度
这两句代码的组合意思就是:
- System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
- mHits[mHits.length-1] = SystemClock.uptimeMillis();
把数组的后面的值覆盖前面的值,最后一位用系统时间补上,若mHits[2] - mHits[0] < 500 则实现了一个三击事件,若要实现多击事件,修改数组长度即可
自己实现的三击事件:
- public void click(View v){
- System.arraycopy(mHits, 1, mHits, 0, mHits.length-1);
- mHits[mHits.length-1] = SystemClock.uptimeMillis();
- if(mHits[mHits.length-1] - mHits[0] < 500){
- Toast.makeText(getApplicationContext(), "多击完成!", 0).show();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。