Android在设置里面添加新功能的方法

时间:2023-01-04 00:02:53

1./usr/smdt/self6000/android/packages/apps/Settings/res/xml/device_info_settings.xml

中增加节点:

<!-- Detailed build version-->

        <Preferenceandroid:key="build_number"

               style="?android:preferenceInformationStyle"

               android:title="@string/build_number"

               android:summary="@string/device_info_default"/>

       

        <!-- Device test -->

        <Preference android:key="device_test"

                style="?android:preferenceInformationStyle"

                android:title="@string/device_test"

               android:summary="@string/device_func_test"/>


<!-- Laohua test -->

        <Preference android:key="laohua_test"

               style="?android:preferenceInformationStyle"

               android:title="@string/laohua_test"

               android:summary="@string/laohua_func_test"/>

 

<!—Device NO -->

        <Preference android:key="device_no"

               style="?android:preferenceInformationStyle"

               android:title="@string/device_no"

               android:summary="@string/device_esn_no"/>


2. /usr/smdt/self6000/android/packages/apps/Settings/res/values/strings.xml

增加

<stringname="kernel_version">Kernel version</string>

   <!-- About phone screen,  setting option name-->

   <stringname="build_number">Build number</string>

    <string name="device_test">"functiontest"</string>

    <stringname="device_func_test">"functiontest"</string>

    <stringname="laohua_test">"old test"</string>

    <stringname="laohua_func_test">"old test"</string>

    <string name="device_no">"devicenumber"</string>

   <string name="device_esn_no">"device esn no"</string>


3./usr/smdt/self6000/android/packages/apps/Settings/res/values-zh-rCN/strings.xml

增加:

   <stringname="kernel_version"msgid="9192574954196167602">"内核版本"</string>

   <stringname="build_number"msgid="3075795840572241758">"版本号"</string>

    <stringname="device_test">"功能测试"</string>

    <string name="device_func_test">"设备功能测试"</string>

    <string name="laohua_test">"老化测试"</string>

    <string name="laohua_func_test">"老化测试"</string>

    <string name="device_no">"设备编号"</string>

    <string name="device_esn_no">"设备编号"</string>


4./usr/smdt/self6000/android/packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java

修改:

   privatestaticfinalStringKEY_EQUIPMENT_ID = "fcc_equipment_id";

   privatestaticfinalStringPROPERTY_EQUIPMENT_ID ="ro.ril.fccid";

   privatestatic final StringKEY_DEVICE_TEST ="device_test";

   privatestatic final StringKEY_LAOHUA_TEST = "laohua_test";

   privatestatic final StringKEY_DEVICE_NO = "device_no";

 

    staticfinalintTAPS_TO_BE_A_DEVELOPER = 7;

onCreate方法增加:

       setStringSummary(KEY_BUILD_NUMBER,Build.DISPLAY);

        findPreference(KEY_BUILD_NUMBER).setEnabled(true);

        findPreference(KEY_DEVICE_TEST).setEnabled(true);

        findPreference(KEY_LAOHUA_TEST).setEnabled(true);

        findPreference(KEY_DEVICE_NO).setEnabled(true);

setStringSummary(KEY_DEVICE_NO,getCPUSerial());

 findPreference(KEY_KERNEL_VERSION).setSummary(getFormattedKernelVersion());

onPreferenceTreeClick方法:

   publicbooleanonPreferenceTreeClick(PreferenceScreenpreferenceScreen, Preferencepreference) {

      

        Log.i(LOG_TAG,"=========="+preference.getKey());

    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 (Exceptione) {

                    Log.e(LOG_TAG,"Unable to start activity " +intent.toString());

                }

            }

        } elseif (preference.getKey().equals(KEY_BUILD_NUMBER)){

            if (mDevHitCountdown > 0) {

                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();

                } elseif (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();

                }

            } elseif (mDevHitCountdown< 0) {

                if (mDevHitToast !=null){

                    mDevHitToast.cancel();

                }

                mDevHitToast = Toast.makeText(getActivity(),R.string.show_dev_already,

                        Toast.LENGTH_LONG);

                mDevHitToast.show();

            }

        }elseif(preference.getKey().equals(KEY_DEVICE_TEST)){

     doStartApplicationWithPackageName("com.smdt.test.basic");

       }elseif(preference.getKey().equals(KEY_LAOHUA_TEST)){

     doStartApplicationWithPackageName("com.into.stability");}

 

        returnsuper.onPreferenceTreeClick(preferenceScreen,preference);

   }

 

增加doStartApplicationWithPackageName方法

privatevoiddoStartApplicationWithPackageName(Stringpackagename) {

      

       //通过包名获取此APP详细信息,包括Activitiesservicesversioncodename等等 

       PackageInfo packageinfo = null

       try

           packageinfo =getPackageManager().getPackageInfo(packagename, 0); 

       } catch (NameNotFoundExceptione) { 

           e.printStackTrace(); 

       } 

       if (packageinfo ==null){

           return

       } 

    

       //创建一个类别为CATEGORY_LAUNCHER的该包名的Intent 

       Intent resolveIntent = new Intent(Intent.ACTION_MAIN,null); 

       resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); 

       resolveIntent.setPackage(packageinfo.packageName); 

    

       //通过getPackageManager()queryIntentActivities方法遍历 

       List<ResolveInfo> resolveinfoList =getPackageManager() 

               .queryIntentActivities(resolveIntent, 0); 

    

       ResolveInfo resolveinfo = resolveinfoList.iterator().next(); 

       if (resolveinfo !=null){ 

           // packagename = 参数packname 

           String packageName = resolveinfo.activityInfo.packageName

           // 这个就是我们要找的该APPLAUNCHERActivity[组织形式:packagename.mainActivityname] 

           String className = resolveinfo.activityInfo.name

           // LAUNCHER Intent 

           Intent intent = new Intent(Intent.ACTION_MAIN); 

           intent.addCategory(Intent.CATEGORY_LAUNCHER);

           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    

           // 设置ComponentName参数1:packagename参数2:MainActivity路径 

           ComponentName cn = new ComponentName(packageName,className); 

    

           intent.setComponent(cn); 

           startActivity(intent); 

       } 

   }

增加获取cpu序列号的方法:

 

/**

     * 获取CPU序列号

     *

     * @returnCPU序列号(16)

     * 读取失败为"0000000000000000"

     */

    publicstaticString getCPUSerial() {

  String str= "",strCPU ="",cpuAddress ="0000000000000000";

  try{

   //读取CPU信息

   Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");

   InputStreamReader ir = new InputStreamReader(pp.getInputStream());

   LineNumberReader input = new LineNumberReader(ir);

   //查找CPU序列号

   for (inti = 1;i < 100;i++) {

     str =input.readLine();

     if (str !=null){

        //查找到序列号所在行

        if (str.indexOf("Serial")> -1) {

          //提取序列号

          strCPU = str.substring(str.indexOf(":") + 1,

          str.length());

          //去空格

          cpuAddress = strCPU.trim();

          break;

        }

     }else{

        //文件结尾

        break;

     }

   }

  } catch(IOExceptionex) {

   //赋予默认值

   ex.printStackTrace();

  }

  returncpuAddress;

    }