標籤:android 應用 app 備份
最近打算搞一個app備份與恢複的小應用,顧名思義,必然包括app的備份和app的恢複兩部分。備份部分由可以分為apk的備份與data的備份兩部分。需要注意的是備份與恢複都需要一堆的許可權,尤其是恢複的時候。
app的備份
首先看看下面幾個目錄:
system/app 系統內建的應用程式apk目錄,無法刪除。
data/app 使用者程式安裝的目錄,有刪除許可權。安裝時把apk檔案複製到此目錄。
data/data 存放應用程式的資料。
data/dalvik-cache 將apk中的dex檔案安裝到dalvik-cache目錄下(dex檔案是dalvik虛擬機器的可執行檔,其大小約為原始apk檔案大小的四分之一)。
app的備份原理就是把system/app、data/app、data/data中的apk和資料存起來,可以存到sd卡也可以上傳到網路。既然如此我們就可寫代碼了,下面代碼只是我自己寫來測試用的,不嚴謹,拷貝可能有問題。
apk的備份 public String backupAppPackage(String packageInPath, String packageName)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
File in = new File(packageInPath);
File out = new File(backupPath + packageName + ".apk");
out.createNewFile();
fis = new FileInputStream(in);
fos = new FileOutputStream(out);
int count;
byte[] buffer = new byte[256 * 1024];
while ((count = fis.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
}
catch (IOException e)
{
e.printStackTrace();
return "IOException";
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (fos != null)
{
try
{
fos.flush();
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return "OK";
}
data的備份 public String backupAppData(String dataInPath, String packageName)
{
FileInputStream fis = null;
FileOutputStream fos = null;
try
{
File in = new File(dataInPath);
File out = new File(backupPath + packageName);
out.createNewFile();
fis = new FileInputStream(in);
fos = new FileOutputStream(out);
int count;
byte[] buffer = new byte[256 * 1024];
while ((count = fis.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
}
catch (IOException e)
{
e.printStackTrace();
return "IOException";
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (fos != null)
{
try
{
fos.flush();
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return "OK";
}
app恢複
app恢複的時候就稍微有點糾結了,因為這就牽扯到apk的安裝,而apk的安裝的方法很多,需要根據自己的情況選擇。
情況一
如果你搞的是一個系統級應用,可以考慮使用反射機制,但是最好是直接使用系統API,由於PackageManager的installPackage函數是隱藏的,所以在用eclipse開發編譯的時候需要引入classes.jar。代碼如下:
public void installPackage(Uri apkFileUri) throws Exception
{
if (apkFileUri != null)
{
//使用放射機製得到PackageManager類的隱藏函數installPackage
PackageManager pm = context.getPackageManager(); //得到pm對象
try
{
//通過反射機制獲得該隱藏函數
// Method installPackage = pm.getClass()
// .getDeclaredMethod("installPackage",
// Uri.class,
// IPackageInstallObserver.class,
// int.class,
// String.class);
// //調用該函數,並且給其分配參數 ,待調用流程完成後會回調PkgSizeObserver類的函數
// installPackage.invoke(pm,
// apkFileUri,
// new PkgInstallObserver(),
// INSTALL_REPLACE_EXISTING,
// null);
//直接通過API
pm.installPackage(apkFileUri,
new PkgInstallObserver(),
INSTALL_REPLACE_EXISTING,
null);
//W/System.err(24923): Caused by: java.lang.SecurityException: Neither user 10053 nor current process has android.permission.INSTALL_PACKAGES.
System.out.println("invoke-->>>");
}
catch (Exception ex)
{
System.out.println("Exception-->>>");
ex.printStackTrace();
//Neither user 10053 nor current process has android.permission.GET_PACKAGE_SIZE.
throw ex; // 拋出異常
}
}
}
IPackageInstallObserver是安裝結果的回調,通過它監聽安裝的結果。
class PkgInstallObserver extends IPackageInstallObserver.Stub
{
public void packageInstalled(String packageName, int returnCode)
throws RemoteException
{
System.out.println("packageInstalled-->>" + packageName);
}
}
情況二
如果搞的是一個普通應用則還有兩種選擇方案。
第一種是通過擷取系統許可權,執行命令靜默安裝,如下:
public void installApp()
{
File dir = new File(backupPath);
File file = dir.listFiles()[2];
String apkPath = file.getAbsolutePath();
String result = execRootCmd("pm install -r " + apkPath);
}
// 執行命令並且輸出結果
public static String execRootCmd(String cmd)
{
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;
try
{
// 經過Root處理的android系統即有su命令
Process p = Runtime.getRuntime().exec("su");
dos = new DataOutputStream(p.getOutputStream());
dis = new DataInputStream(p.getInputStream());
Log.i(TAG, cmd);
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
String line = null;
while ((line = dis.readLine()) != null)
{
Log.d("result", line);
result += line;
}
p.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (dos != null)
{
try
{
dos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (dis != null)
{
try
{
dis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return result;
}
第二種最普通的安裝是把安裝的任務交給系統應用去做,如下:
public static void install(Context context, String filePath) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
需要滴許可權 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />