您的位置:首页 > 移动开发 > Android开发

Android设备恢复出厂设置的文件备份与恢复

2017-09-05 11:03 417 查看
问题描述:

    实现备份一个文件(包括诸如配置信息等内容)到Android设备的某个目录中,当Android设备恢复出厂设置后,可以从该目录中读取备份的文件,从而恢复Android设备之前的配置信息。这里说明一下,实现这个过程需要Android设备之前root过。

方案解决:

    首先,Android设备中system目录的文件在恢复出厂设置的情况下是不会被删除的,但是system默认是只读的,所以如果要实现上面的目的,程序需要获取root权限,将system目录修改成可读写的。大体解决思路如下:在某个目录中创建txt文件,创建完成后,移动该目录中的文件到system某个目录中,并删除原目录中的txt文件,以后读取恢复的时候从system相应目录中读取。这里在/cache/目录中创建,移动到/system/usr/目录,创建的文件名为net.txt,代码如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

public class MainActivity extends AppCompatActivity {

Button button1;
TextView tv;
Button button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
button2=(Button)findViewById(R.id.btn2);
button1=(Button)findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText("");
writeTxtFile();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setText(readTxtFile("/system/usr/net.txt"));
}
});
}

private void writeTxtFile() {
String filePath = "/cache/";
String fileName = "net.txt";

//有的设备cache目录可能是只读的,执行下面的shell命令授予读写权限
runRootCommand("chmod -R 777 /cache");
String msg="txt content\r\n哈哈哈哈\r\n你好啊\r\n1234";
writeTxtToFile(msg, filePath, fileName);
runCommand();//不可或缺,改变system目录的读写权限
}

// 将字符串写入到文本文件中
public void writeTxtToFile(String strcontent, String filePath, String fileName) {
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(filePath, fileName);

String strFilePath = filePath+fileName;
// 每次写入时,都换行写
String strContent = strcontent + "\r\n";
try {
File file = new File(strFilePath);
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + strFilePath);
file.getParentFile().mkdirs();
file.createNewFile();
}
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(file.length());
raf.write(strContent.getBytes());
raf.close();
Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "创建失败"+e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("TestFile", "Error on write File:" + e);
}
}

public void runRootCommand(String command) {

Process process = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
StringBuffer wifiConf = new StringBuffer();
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
dataInputStream = new DataInputStream(process.getInputStream());
dataOutputStream
.writeBytes(command+"\n");
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
InputStreamReader inputStreamReader = new InputStreamReader(
dataInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
wifiConf.append(line);
}
bufferedReader.close();
inputStreamReader.close();
process.waitFor();
Log.d("shell命令执行结果:",process.exitValue()+"");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (dataInputStream != null) {
dataInputStream.close();
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}

// 生成文件
public File makeFilePath(String filePath, String fileName) {
File file = null;
makeRootDirectory(filePath);
try {
file = new File(filePath + fileName);
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
return file;
}

// 生成文件夹
public static void makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
Log.i("error:", e+"");
}
}

public void runCommand(){
Process process = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
StringBuffer wifiConf = new StringBuffer();
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
dataInputStream = new DataInputStream(process.getInputStream());
//获取system目录的读写权限
dataOutputStream.writeBytes("mount -o remount /system\n");
dataOutputStream.writeBytes("chmod -R 777 /system/usr\n");
//将cache下的net.txt移动到system的usr目录
dataOutputStream.writeBytes("cat /cache/net.txt > /system/usr/net.txt\n");
//删除cache下的net.txt文件
dataOutputStream.writeBytes("rm -f /cache/net.txt\n");
//修改移动到system的usr目录下的net.txt的读写权限为最高
dataOutputStream.writeBytes("chmod 777 /system/usr/net.txt\n");
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
InputStreamReader inputStreamReader = new InputStreamReader(
dataInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
wifiConf.append(line);
}
bufferedReader.close();
inputStreamReader.close();
process.waitFor();
Log.d("shell命令执行结果:",process.exitValue()+"");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (dataInputStream != null) {
dataInputStream.close();
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public static String readTxtFile(String strFilePath)
{
String path = strFilePath;
String content = ""; //文件内容字符串
//打开文件
File file = new File(path);
//如果path是传递过来的参数,可以做一个非目录的判断
if (file.isDirectory()){
Log.d("TestFile", "The File doesn't not exist.");
}else {
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行读取
while (( line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();
}
}catch (java.io.FileNotFoundException e){
Log.d("TestFile", "The File doesn't not exist.");
}
catch (IOException e){
Log.d("TestFile", e.getMessage());
}
}
return content;
}
}

其中XML布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xuzhb.writefile.MainActivity">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="30sp"/>

<Button
android:id="@+id/btn1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background"
android:text="创建文件"
android:textSize="30sp"/>
<Button
android:id="@+id/btn2"
android:layout_below="@+id/btn1"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background"
android:text="读取文件"
android:textSize="30sp"/>
</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: