android中程式数据库的拷贝小工具

想要拿到android中程式的数据库中的数据,可是pad没有破解,想到写一个小工具来实现他。

public class HcdFileBackupActivity extends Activity {

    private Context context;
    private EditText pathFrom;
    private EditText pathTo;
//    String pathFromStr;
//    String pathToStr;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;

        Button copyBtn = (Button) this.findViewById(R.id.button1);

        pathFrom = (EditText) this.findViewById(R.id.editTextFrom);
        pathTo = (EditText) this.findViewById(R.id.editTextTo);

        copyBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.e("onclick", "start");
                // TODO Auto-generated method stub
                String pathFromStr = pathFrom.getText().toString().trim();
                if (pathFromStr.endsWith("/")) {
                    pathFromStr = pathFromStr.substring(0, pathFromStr.length() - 1);
                }
                File pathFromFile = new File(pathFromStr);
                
                String pathToStr = pathTo.getText().toString();
                if (pathToStr.endsWith("/")) {
                    pathToStr = pathToStr.substring(0, pathToStr.length() - 1);
                }

                File pathToFile = new File(pathToStr);
                if (!pathFromFile.exists()) {
                    Toast.makeText(context, "source path : " + pathFromFile + "is not exists!",
                            Toast.LENGTH_LONG).show();
                    return;
                }
                if (!pathToFile.exists()) {
                    try {
                        pathToFile.mkdirs();
                    } catch (Exception e) {
                        Toast.makeText(context, "target path : " + pathFromFile
                                + "is Unavailable!", Toast.LENGTH_LONG).show();
                        return;
                    }
                }
                // 获取源文件夹当前下的文件或目录
                File[] file = pathFromFile.listFiles();
                for (int i = 0; i < file.length; i++) {
                    if (file[i].isFile()) {
                        // 复制文件
                        copyFile(file[i], new File(pathToStr + File.separator
                                + file[i].getName()));
                    }
                    if (file[i].isDirectory()) {
                        // 复制目录
                        String sourceDir = pathFromStr + File.separator
                                + file[i].getName();
                        String targetDir = pathToStr + File.separator
                                + file[i].getName();
                        copyDirectiory(sourceDir, targetDir);
                    }
                }
                Log.e("onclick", "finish");
                new AlertDialog.Builder(context)
                        .setTitle(R.string.dialogTitle)
                        .setMessage(R.string.dialogMessage)
                        .setPositiveButton(R.string.finish,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                        dialog.dismiss();
                                    }
                                }).create().show();
            }
        });
    }

    // 复制文件
    private void copyFile(File sourceFile, File targetFile) {
        try {
            // 新建文件输入流并对它进行缓冲
            FileInputStream input = new FileInputStream(sourceFile);
            BufferedInputStream inBuff = new BufferedInputStream(input);

            // 新建文件输出流并对它进行缓冲
            FileOutputStream output = new FileOutputStream(targetFile);
            BufferedOutputStream outBuff = new BufferedOutputStream(output);

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();

            // 关闭流
            inBuff.close();
            outBuff.close();
            output.close();
            input.close();
        } catch (Exception e) {
            Toast.makeText(context,
                    "Failed to copy file : " + sourceFile.getName(),
                    Toast.LENGTH_LONG).show();
        }
    }

    // 复制文件夹
    private void copyDirectiory(String sourceDir, String targetDir) {
        // 新建目标目录
        (new File(targetDir)).mkdirs();
        // 获取源文件夹当前下的文件或目录
        File[] file = (new File(sourceDir)).listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isFile()) {
                // 源文件
                File sourceFile = file[i];
                // 目标文件
                File targetFile = new File(
                        new File(targetDir).getAbsolutePath() + File.separator
                                + file[i].getName());
                copyFile(sourceFile, targetFile);
            }
            if (file[i].isDirectory()) {
                // 准备复制的源文件夹
                String dir1 = sourceDir + "/" + file[i].getName();
                // 准备复制的目标文件夹
                String dir2 = targetDir + "/" + file[i].getName();
                copyDirectiory(dir1, dir2);
            }
        }
    }

}

有一点特别提醒的是要在AndroidManifest.xml中manifest标签下添加android:sharedUserId="com.share.xxxx"
这个要和你要取的数据库所在apk的定义相同,才会有权限访问到数据库文件夹
一般数据库文件在/data/data/com.xxxxx.home/databases中。
网上有说,如果不是用自己程式的sharedUserId而是用系统的android:sharedUserId="android.uid.system"也可以,
不过查了一下,好像需要在Android系統源碼的環境下用make來編譯。
===============================================================================
使用 android:sharedUserId 的注意事項 - 修改系統時間為例

參考: http://www.cnmsdn.com/html/201004/1270791500ID3384.html
Android中如何修改系統時間(應用程式獲得系統許可權)

在 android 的API中有提供 SystemClock.setCurrentTimeMillis()函數來修改系統時間,可惜無論你怎麼調用這個函數都是沒用的,無論模擬器還是真機,在logcat中總會得到"Unable to open alarm driver: Permission denied "。這個函數需要root許可權或者運行與系統進程中才可以用。
第一個方法簡單點,不過需要在Android系統源碼的環境下用make來編譯:
1. 在應用程式的AndroidManifest.xml中的manifest節點中加入
android:sharedUserId="android.uid.system"這個屬性。
2. 修改Android.mk文件,加入LOCAL_CERTIFICATE := platform這一行
3. 使用mm命令來編譯,生成的apk就有修改系統時間的許可權了。

第二個方法麻煩點,不過不用開虛擬機器跑到源碼環境下用make來編譯:
1. 在應用程式的AndroidManifest.xml中加入android:sharedUserId="android.uid.system"這個屬性。
2. 使用eclipse編譯出apk檔,但是這個apk檔是不能用的。
3. 用壓縮軟體打開apk檔,刪掉META-INF目錄下的CERT.SF和CERT.RSA兩個文件。
4. 使用目標系統的platform金鑰來重新給apk檔簽名。這步比較麻煩,首先找到金鑰檔,在我的Android源碼目錄中的位置是"build/target/product/security",下面的platform.pk8和platform.x509.pem兩個文件。
然後用Android提供的Signapk工具來簽名,signapk的原始程式碼是在"build/tools/signapk"下,用法為"signapk platform.x509.pem platform.pk8 input.apk output.apk",
檔案名最好使用絕對路徑防止找不到,也可以修改原始程式碼直接使用。
這樣最後得到的apk和第一個方法是一樣的。

最後解釋一下原理,首先加入android:sharedUserId="android.uid.system"這個屬性。通過Shared User id,擁有同一個User id的多個APK可以配置成運行在同一個進程中。那麼把程式的UID配成android.uid.system,也就是要讓程式運行在系統進程中,這樣就有許可權來修改系統時間了。

只是加入UID還不夠,如果這時候安裝APK的話發現無法安裝,提示簽名不符,原因是程式想要運行在系統進程中還要有目標系統的platform key,就是上面第二個方法提到的platform.pk8和platform.x509.pem兩個文件。用這兩個key簽名後apk才真正可以放入系統進程中。第一個方法中加入LOCAL_CERTIFICATE := platform其實就是用這兩個key來簽名。

這也有一個問題,就是這樣生成的程式apk只有在原始的Android系統或者是自己編譯的系統中才可以用,因為這樣的系統才可以拿到 platform.pk8和platform.x509.pem兩個文件。要是別家公司做的Android上連安裝都安裝不了。試試原始的Android 中的key來簽名,程式在模擬器上運行OK,不過放到G3上安裝直接提示"Package ... has no signatures that match those in shared user android.uid.system",這樣也是保護了系統的安全。

最後,這個android:sharedUserId屬性不只可以把apk放到系統進程中,也可以配置多個APK運行在一個進程中,這樣可以共用資料,應該會很有用的。

sharedUserId的名字必須從一開始開發就必須確定,且不能變更,以免發生當要升級版本後,存取權限的問題。參考:Issue #1227 bites you if you add sharedUserId to your application.

posted @ 2012-07-20 17:12  日光之下无新事  阅读(257)  评论(0编辑  收藏  举报