`
ouyangfeng521
  • 浏览: 243126 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

androi 静默安装APK

 
阅读更多

静默安装前 手机 必须 root

 

以下是代码 、

package com.test.root.install;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.xml.sax.InputSource;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;

public class RootInstallApkActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.install).setOnClickListener(this);
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 0:
                findViewById(R.id.install).setEnabled(true);
                break;
            case 1:
                findViewById(R.id.install).setEnabled(false);
                break;
            default:
                break;
            }
        }
    };

    public void install() {

        new Thread() {
            public void run() {
                try {
                    handler.sendEmptyMessage(1);
                    Process process = Runtime.getRuntime().exec("su"); // 得到root 权限
                    OutputStream out = process.getOutputStream();
                    // 向进程里 写入命令
                    out.write(("cp /sdcard/CNApp/test.apk /data/local/tmp" + "\n").getBytes()); // 先把sdcard里的apk copy到这里目录
                    out.write(("pm install -r /data/local/tmp/test.apk" + "\n").getBytes());// 调用安装
                    out.flush();
                    out.close();
                    InputStream in = process.getInputStream();
                    int len = 0;
                    byte[] bs = new byte[256];
                    while (-1 != (len = in.read(bs))) {
                        System.out.println(new String(bs, 0, len));
                    }
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    handler.sendEmptyMessage(0);
                }
            }
        }.start();

    }

    /**
     * (non-Javadoc)
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        install();
    }
}
分享到:
评论
4 楼 chenjia66804610 2013-05-23  
你好,我也是下载了你的程序,运行不报错,但是应用没有安装到系统里面,是怎么回事呢
3 楼 yyhat 2013-05-16  
静默安装不成功,不要拷贝,下面的测试是可以的
public boolean installFile(File path, Context context) {
boolean result = false;
Process process = null;
OutputStream out = null;
InputStream in = null;
String state = null;
try {
// 请求root
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();

// 调用安装,将文件写入到process里面
out.write(("pm install -r " + path + "\n").getBytes());

// 这里拿到输出流,开始安装操作
in = process.getInputStream();
int len = 0;
byte[] bs = new byte[256];
while (-1 != (len = in.read(bs))) {
state = new String(bs, 0, len);
if (state.equals("Success\n")) {
// 安装成功后的操作
result = true;
Log.e("成功", state);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
2 楼 ouyangfeng521 2013-03-04  
robertsun 写道
你好,我下载了你的程序,程序运行没有出错,但是要安装的程序没有安装到系统里,是怎么回事呢?谢谢。


手机有没有ROOT
1 楼 robertsun 2013-01-14  
你好,我下载了你的程序,程序运行没有出错,但是要安装的程序没有安装到系统里,是怎么回事呢?谢谢。

相关推荐

Global site tag (gtag.js) - Google Analytics