以编程方式将APK推送到系统/应用程序

时间:2013-12-19 10:18:56

标签: android adb root

我想以编程方式将APK推送到root设备上的系统/应用程序。我尝试了以下代码,但push似乎不是Android上的命令。如何修改它才能工作?

  public static void copyApkToSys(){
    Process process;
    try {
        process = Runtime.getRuntime().exec("su");
        DataOutputStream out = new DataOutputStream(process.getOutputStream());
        out.writeBytes("mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system\n");
        out.writeBytes("push /data/shareData/a1.apk /system/app\n");
        out.writeBytes("mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system\n");
        out.writeBytes("exit\n");  
        out.flush();
        process.waitFor();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

如果我使用以下代码,则仅在重启设备后才会显示该应用程序:

public static void copyApkToSys(){
    Process process;
    try {
        process = Runtime.getRuntime().exec("su");
        DataOutputStream out = new DataOutputStream(process.getOutputStream());
        out.writeBytes("mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system\n");
        out.writeBytes("cat /data/shareData/a1.apk > /system/app/a1.apk\n");
        out.writeBytes("mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system\n");
        out.writeBytes("exit\n");  
        out.flush();
        process.waitFor();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

编辑:如果我使用cat,那么请使用以下代码,但我无法启动我的活动

public static boolean startApk(String packageName, String activityName) {
    boolean isSuccess = false;

    String cmd = "am start -n " + packageName + "/" + activityName + " \n";
    try {
        Process process = Runtime.getRuntime().exec(cmd);

        isSuccess = waitForProcess(process);
    } catch (IOException e) {

        e.printStackTrace();
    } 
    return isSuccess;
}

似乎pushcat之间存在差异,因为通过adb推送应用会产生我想要的结果。我该如何以编程方式执行此操作?

1 个答案:

答案 0 :(得分:-1)

尝试使用命令“mv -R”,就像Linux命令

一样