1、将文件复制到cache分区
private boolean copyFileToCache() {
//updateFilePath 本地升级文件的绝对路径
LogUtil.d("copyFileToCache start updateFilePath : " + updateFilePath);
File oldFile = new File(updateFilePath);
if (oldFile.exists()) {
try {
InputStream inStream = new FileInputStream(oldFile);
FileOutputStream fos = new FileOutputStream("/cache/update.zip");
byte[] buf = new byte[1024];
int length = 0;
while ((length = inStream.read(buf)) != -1) {
fos.write(buf, 0, length);
}
inStream.close();
fos.close();
updateFilePath = "/cache/update.zip";//替换为cache分区中升级包的绝对路径
LogUtil.d("copyFileToCache end updateFilePath : " + updateFilePath);
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
2、调用recovery接口,验证签名,进行升级
try {
LogUtil.d("RecoverySystem delta install file:" + updateFilePath);
RecoverySystem.ProgressListener mPro = new RecoverySystem.ProgressListener() {
@Override
public void onProgress(int progress) {
LogUtil.d("verify progress :" + progress);
}
};
try {
RecoverySystem.verifyPackage(new File(updateFilePath), mPro , null);
} catch (GeneralSecurityException e) {
e.printStackTrace();
return false;
}
} catch (IOException e) {
LogUtil.d("RecoverySystem :" + e.toString());
e.printStackTrace();
}
try {
RecoverySystem.installPackage(InstallActivity.this, new File(updateFilePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3、需要的权限:
<uses-permission android:name="android.permission.MASTER_CLEAR" />
<uses-permission android:name="android.permission.REBOOT" />
<uses-permission android:name="android.permission.ACCESS_CACHE_FILESYSTEM" />
<uses-permission android:name="android.permission.DELETE_CACHE_FILES" />
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.RECOVERY" />