App Backup and Recovery research

Source: Internet
Author: User

The recent plan to make a small app backup and recovery application, as the name implies, must include the app backup and app recovery two parts. The backup part consists of two parts: backup and data that can be divided into apk. It is important to note that both backup and restore require a bunch of permissions, especially when recovering.

Backup of the app

First look at the following directories:

System/app system comes with an app apk directory that cannot be deleted.
Data/app User program installs the directory, has the delete permission. Copy the apk file to this directory during installation.
Data/data Store the data for the application.

Data/dalvik-cache installs the Dex file in the APK into the Dalvik-cache directory (the Dex file is the executable file for the Dalvik virtual machine, which is approximately one-fourth of the original apk file size).

The backup principle of the app is to save the APK and data in System/app, Data/app, Data/data, and save it to the SD card and upload it to the network. In this case we can write code, the following code is just my own writing to test, not rigorous, the copy may have a problem.

Backup of apkpublic 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";

}

Backup of Datapublic 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 Recovery

App recovery is a little bit tangled, because this involves the installation of the APK, and APK installation methods are many, you need to choose according to their own circumstances.

situation one

If you're doing a system-level application, consider using a reflection mechanism, but it's best to use the system API directly, because Packagemanager's installpackage function is hidden, So you need to introduce Classes.jar when you build with Eclipse. The code is as follows:

public void InstallPackage (Uri apkfileuri) throws Exception
{
if (Apkfileuri! = null)
{
Use the radiation mechanism to get the hidden function of Packagemanager class InstallPackage
Packagemanager pm = Context.getpackagemanager (); Get PM Object
Try
{
Get the hidden function by reflection mechanism
Method installpackage = Pm.getclass ()
. Getdeclaredmethod ("InstallPackage",
Uri.class,
Ipackageinstallobserver.class,
Int.class,
String.class);

Call the function and assign it a parameter, callback the function of the Pkgsizeobserver class upon completion of the call process
Installpackage.invoke (PM,
Apkfileuri,
New Pkginstallobserver (),
Install_replace_existing,
NULL);



Directly through the 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 had Android.permiss Ion. 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; Throw exception
}
}
}


Ipackageinstallobserver is the callback for the installation result, which listens for the results of the installation.
Class Pkginstallobserver extends Ipackageinstallobserver.stub
{
public void packageinstalled (String packagename, int returncode)
Throws RemoteException
{
System.out.println ("packageinstalled-->>" + PackageName);
}

}

Scenario Two

If there is a common application there are two options.

The first is to perform a silent installation of the command by obtaining system permissions, as follows:
public void Installapp ()
{
File dir = new file (backuppath);
File File = Dir.listfiles () [2];
String Apkpath = File.getabsolutepath ();
String result = Execrootcmd ("PM install-r" + Apkpath);
}


Execute the command and output the result
public static string Execrootcmd (string cmd)
{
String result = "";
DataOutputStream dos = null;
DataInputStream dis = null;

Try
{
The root-handled Android system has the SU command
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;

}

The second most common installation is to give the installation task to the system application to do, as follows:

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

}

permission to be dropped<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.INSTALL_PACKAGES"/>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.