android 9 预装可卸载的app,只安装一次

之前修改代码做了可卸载的预装app,但客户要求,只安装一次,后面就不管了,卸载了也不需要再次安装。

需求的节点在于,预装后apk文件还存在,重新启动后会再次安装。

解决的思路:

系统启动后检查是否已经安装了app,同时检查apk文件是否存在,如果满足条件,删之。

1、预装可卸载的app

https://blog.csdn.net/cau_eric/article/details/104004350

2、启动后监听 ACTION_BOOT_COMPLETED 广播

https://blog.csdn.net/cau_eric/article/details/89393779 

 我在源码中找了,setting里面有这种receiver,

a、修改 

\packages\apps\Settings\AndroidManifest.xml

添加:


    
         
    

b、增加Receiver 文件:

public class Mile8Receiver extends BroadcastReceiver {
    private static final String TAG = "Mile8Receiver";
	private final String apk_1 = "/vendor/priv-app/acquisitor_app-release-3.4.0.10-V69.apk";
    private final String apk_2 = "/vendor/priv-app/com.google.android.inputmethod.pinyin.apk";
    private final String apk_3 = "/vendor/priv-app/ES_File_Explorer.apk";
	private final String packageName_1 = "com.nl.android.ecgviewer";
    private final String packageName_2 = "com.google.android.inputmethod.pinyin";
    private final String packageName_3 = "com.estrongs.android.pop";
	private List packages;

    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Log.d(TAG,"=Mile8Receiver=>>ACTION_BOOT_COMPLETED");			
			execSuCmd("mount -o remount -w /vendor");
			packages = getNullSystemPackageNames(context);
			if (isFileExist(apk_1) && isPackageInstalled(packageName_1)){
            	Log.d(TAG,"=Mile8Receiver=apk1=>>");
				execSuCmd("rm -f " + apk_1);
        	}
			if (isFileExist(apk_2) && isPackageInstalled(packageName_2)){
            	Log.d(TAG,"=Mile8Receiver=apk2=>>");
				execSuCmd("rm -f " + apk_2);
        	}
			if (isFileExist(apk_3) && isPackageInstalled(packageName_3)){
            	Log.d(TAG,"=Mile8Receiver=apk3=>>");
				execSuCmd("rm -f " + apk_3);
        	}
		
        }
    }
...
}

 c、检查app是否已安装:

private boolean isPackageInstalled(String packageName_input){
        for (String packageName : packages){
            if (packageName.equals(packageName_input)){
                return true;
            }
        }
        return false;
    }

获取第三方应用: https://blog.csdn.net/cau_eric/article/details/100572601

d、检查apk文件是否存在:

private boolean isFileExist(String file){
        return (new File(file)).exists();
    }

基本完工了,但是还有一个问题,删除文件的时候提示read-only file system错误。

解决办法:https://blog.csdn.net/cau_eric/article/details/104757888

e、su系统执行命令:

https://blog.csdn.net/cau_eric/article/details/98969975

 

OK!测试通过

你可能感兴趣的:(android1)