android安装应用为系统应用

package com.sjf.cats;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private static final String DEFAULT_PATH = new File(Environment.getExternalStorageDirectory(),"wfapp.apk").getAbsolutePath();
    private EditText mPath;
    private EditText mPathSo;
    private EditText mPathSo64;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button open = (Button) findViewById(R.id.copyAppToSystem);

        mPath = (EditText) findViewById(R.id.path);
        mPathSo = (EditText) findViewById(R.id.path_so);
        mPathSo64 = (EditText) findViewById(R.id.path_so_64);
        
        mPath.setText(DEFAULT_PATH);

        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = mPath.getText().toString().trim();
                if (TextUtils.isEmpty(path)) {
                    path = DEFAULT_PATH;
                }
                File apk = new File(path);
                String apkPath = null;
                String soPath = null;
                String so64Path = null;

                System.out.println(apk.getName() + " , " + apk.exists());
                boolean canCopy = false;
                if (apk.exists()) {
                    apkPath = apk.getAbsolutePath();
                    canCopy = true;
                }
                File pathSo = new File(mPathSo.getText().toString().trim());
                if (pathSo.exists()) {
                    soPath = pathSo.getAbsolutePath();
                    canCopy = true;
                }

                File pathSo64 = new File(mPathSo64.getText().toString().trim());
                if (pathSo64.exists()) {
                    so64Path = pathSo64.getAbsolutePath();
                    canCopy = true;
                }

                if (canCopy) {
                    copyAppToSystem(apkPath, apk.getName(), soPath, pathSo.getName(), so64Path, pathSo64.getName());
                } else {
                    Toast.makeText(getApplicationContext(), apk.getAbsolutePath() + " 不存在,请重新输入!", Toast.LENGTH_LONG).show();
                }
            }
        });
//        Button close = (Button) findViewById(R.id.copyAppToSystem);
//        close.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                RunAsRooter();
//            }
//        });
        Button reboot = (Button) findViewById(R.id.reboot);
        reboot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execCmd("reboot");
            }
        });
    }

    private void RunAsRooter() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void copyAppToSystem(String path, String apkName, String libPath, String libName,String lib64Path, String lib64Name) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system\n");
            if (!TextUtils.isEmpty(path)) {
                os.writeBytes("cat " + path + " > /system/app/" + apkName + "\n");
                os.writeBytes("chmod 777 /system/app/" + apkName + "\n");
            }
            if (!TextUtils.isEmpty(libPath)) {
                os.writeBytes("cat " + libPath + " > /system/lib/" + libName + "\n");
                os.writeBytes("chmod 777 /system/lib/" + libName + "\n");
            }
            if (!TextUtils.isEmpty(lib64Path)) {
                os.writeBytes("cat " + lib64Path + " > /system/lib64/" + lib64Name + "\n");
                os.writeBytes("chmod 777 /system/lib64/" + lib64Name + "\n");
            }
//            os.writeBytes("cat /sdcard/amyapp.apk > /system/app/amyapp.apk\n");
            os.writeBytes("mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Log.i("zjt", "CopyAppToSystem  is  finish ");
    }

    /*
    * 执行命令
    * @param command
    * 1、获取root权限 "chmod 777 "+getPackageCodePath()
    * 2、关机 reboot -p
    * 3、重启 reboot
    *
    * execShellCmd("input keyevent 3");//home
    * execShellCmd("input text  'helloworld!' ");
    * execShellCmd("input tap 168 252");
    * execShellCmd("input swipe 100 250 200 280");
    * */
    public static boolean execCmd(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text=" 要复制的apk路径(如:/sdcard/sjf/abc.apk) \n 默认在/sdcard/amyapp.apk" />

    <EditText
        android:id="@+id/path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入要复制的apk路径" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text=" 要复制的到lib的so路径(如:/sdcard/sjf/abc.so) \n 默认在/sdcard/amyapp.so" />

    <EditText
        android:id="@+id/path_so"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入要复制到lib的so路径" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text=" 要复制的到lib64的so路径(如:/sdcard/sjf/abc.so) \n 默认在/sdcard/amyapp.so" />

    <EditText
        android:id="@+id/path_so_64"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入要复制到lib64的so路径" />

    <Button
        android:id="@+id/copyAppToSystem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="复制应用到系统" />

    <Button
        android:id="@+id/reboot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="重启系统" />

</LinearLayout>

 

posted @ 2019-01-04 18:39  戴帽的和尚  阅读(620)  评论(0)    收藏  举报