Android如何将第三方预装的AP安装在data/app下面,用户可以选择卸载,但是在用户恢复出厂设置后,会再次自动安装

大体思路是,将第三方应用保存在system/media/app目录下,在用户第一次启动手机或者恢复出厂设置的时候将这些apk文件拷贝到/data/app目录下面,PackageManagerService在系统启动的时候会自动扫描data/app目录下面的文件,然后自动安装。

具体步骤如下:

第一步,新加一个文件preinstall.txt来判断用户是在烧写手机和恢复出厂设置后第一次启动手机,还是用户正常重启手机。如果该文件在data/app目录存在,则是用户正常重启手机,否则就是用户恢复出厂设置或者第一次刷机后开机,这时就需要自动安装预装程序。注意此文件内容一定不能为空,否则shell脚本-s会认为该文件不存在

第二步,在system/core/rootdir/etc目录下新建shell脚本文件,如init.qcom.preinstall.sh,内容如下:

#!/system/bin/sh
# Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Code Aurora nor
#       the names of its contributors may be used to endorse or promote
#       products derived from this software without specific prior written
#       permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
cd /system/media/app

#android shell script: check if preinstall.txt is exist
if [ -s /data/app/preinstall.txt ]; then
    echo "don't need to copy preinstall files"
else
    # scan all apk files under system/media/app
    apklist="$(ls)"
    for apkfile in ${apklist}; do
        #copy all apk file to data/app
        dd if=${apkfile} of=/data/app/${apkfile}
        # change the permission
        chmod 666 /data/app/${apkfile}
    done
fi


第三步,在device/qcom/common/common.mk中新加一行

INIT += init.qcom.preinstall.sh


第四步,修改system/core/rootdir/Android.mk

新增内容如下

include $(CLEAR_VARS)
LOCAL_MODULE       := init.qcom.preinstall.sh	56
LOCAL_MODULE_TAGS  := optional eng	57
LOCAL_MODULE_CLASS := ETC	58
LOCAL_SRC_FILES    := etc/init.qcom.preinstall.sh	59
include $(BUILD_PREBUILT)


第五步,修改system/core/rootdir/init.rc文件,添加如下内容

chmod 0555 /system/etc/init.qcom.preinstall.sh

service preinstalld /system/bin/sh /system/etc/init.qcom.preinstall.sh

class main

group system


最后修改你的编译文件,将第三方软件打包如system/media/app目录



以上是在高通4.04平台上面实现方案


你可能感兴趣的:(用户可卸载)