android --静默安装

标签: androidroot静默安装apk安装源码
1075人阅读 评论(0) 收藏 举报
分类:

【此篇文章为转载文章】

最近需要实现Android应用的静默安装,在网上看了不少帖子,最后在root权限下实现对应用的静默安装和卸载,现在就整个实现的过程做一个总结。

一.第一种方案
第一种方案参考了源码中/packages/apps/PackageInstaller的实现方式,实现的主要代码如下:
 
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.pm.PackageInfo;
importandroid.content.pm.PackageManager;
importandroid.content.pm.IPackageInstallObserver;
importandroid.content.pm.PackageManager.NameNotFoundException;
importandroid.content.pm.PackageParser;
importandroid.net.Uri;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.util.DisplayMetrics;
importandroid.util.Log;
importandroid.os.FileUtils;
 
publicclass MyPackageInstaller {
privatestatic final String PACKAGE_NAME = "test.installservice";
privatefinal int INSTALL_COMPLETE = 1;
privateContext context;
Uri mPackageURI;
privatePackageParser.Package mPkgInfo;
 
privateHandler mHandler = newHandler() {
publicvoid handleMessage(Message msg) {
switch(msg.what) {
caseINSTALL_COMPLETE:
// finish the activity posting result
//setResultAndFinish(msg.arg1);
break;
default:
break;
}
}
};
 
voidsetResultAndFinish(intretCode) {
// Intent data = new Intent();
// setResult(retCode);
// finish();
}
 
publicMyPackageInstaller(Context c) {
this.context = c;
}
 
publicvoid installPackage() {
intinstallFlags = 0;
PackageManager pm = context.getPackageManager();
try{
PackageInfo pi = pm.getPackageInfo(
mPkgInfo.applicationInfo.packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if(pi != null) {
// installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
installFlags |= 2;
}
}catch(NameNotFoundException e) {
}
 
String array[] = null;
try{
Runtime.getRuntime().exec("chmod 777 /data/data/" + PACKAGE_NAME);
Runtime.getRuntime().exec(
"chmod 777 /data/data/" + PACKAGE_NAME + "/files");
array = this.mPackageURI.toString().split("/");
System.out.println("array[last]->"+ array[array.length - 1]);
Runtime.getRuntime().exec(
"chmod 777 /data/data/" + PACKAGE_NAME + "/files/"
+ array[array.length - 1]);
}catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
PackageInstallObserver observer = newPackageInstallObserver();
pm.installPackage(mPackageURI, observer, installFlags, null);
// context.deleteFile(array[array.length-1]);
}
 
classPackageInstallObserver extendsIPackageInstallObserver.Stub {
publicvoid packageInstalled(String packageName, intreturnCode) {
Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
msg.arg1 = returnCode;
mHandler.sendMessage(msg);
}
}
 
privateFile createTempPackageFile(String filePath) {
File tmpPackageFile;
inti = filePath.lastIndexOf("/");
String tmpFileName;
if(i != -1) {
tmpFileName = filePath.substring(i + 1);
}else{
tmpFileName = filePath;
}
FileOutputStream fos;
try{
// MODE_WORLD_READABLE=1
fos = context.openFileOutput(tmpFileName, 1);
}catch(FileNotFoundException e1) {
Log.e("Installer","Error opening file " + tmpFileName);
returnnull;
}
try{
fos.close();
}catch(IOException e) {
Log.e("Installer","Error opening file " + tmpFileName);
returnnull;
}
tmpPackageFile = context.getFileStreamPath(tmpFileName);
File srcPackageFile = newFile(filePath);
if(!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) {
returnnull;
}
returntmpPackageFile;
}
 
publicvoid makeTempCopyAndInstall(Uri mPackageURI) {
mPkgInfo = getPackageInfo(mPackageURI);
System.out.println("package="+ mPkgInfo.applicationInfo.packageName);
System.out.println("copy file=" + mPackageURI.getPath());
File mTmpFile = createTempPackageFile(mPackageURI.getPath());
if(mTmpFile == null) {
// display a dialog
Log.e("Installer",
"Error copying file locally. Failed Installation");
// showDialogInner(DLG_OUT_OF_SPACE);
return;
}
this.mPackageURI = Uri.parse("file://"+ mTmpFile.getPath());
}
 
publicPackageParser.Package getPackageInfo(Uri packageURI) {
finalString archiveFilePath = packageURI.getPath();
PackageParser packageParser = newPackageParser(archiveFilePath);
File sourceFile = newFile(archiveFilePath);
DisplayMetrics metrics = newDisplayMetrics();
metrics.setToDefaults();
returnpackageParser.parsePackage(sourceFile, archiveFilePath, metrics,
0);
}
}


在程序中的调用方式:this为Context,path为安装包的绝对路径
 
MyPackageInstaller mpi = newMyPackageInstaller(this);
mpi.makeTempCopyAndInstall(Uri.parse(path));
mpi.installPackage();



这种方式需要在源码下面编译apk,并将apk放入/system/app目录下面。

二.通过shell命令实现
首先,在java中实现安装和卸载apk的命令
 
publicclass PackageInstaller {
 
publicvoid unInstallApp(String packageName){
try{
Runtime.getRuntime().exec("pm uninstall "+packageName);
}catch(IOException e) {
e.printStackTrace();
}
}
 
publicvoid installApp(String appPath){
try{
Runtime.getRuntime().exec("pm install "+appPath);
}catch(IOException e) {
e.printStackTrace();
}
}
 
publicvoid reInstallApp(String appPath){
try{
Runtime.getRuntime().exec("pm install -r "+appPath);
}catch(IOException e) {
e.printStackTrace();
}
}
 
}

 

然后再源码环境下将该java程序编译为jar包
2.将编译好的jar包放入程序的assets目录下面,通过以下代码在程序中将该jar文件拷贝到/data/data/package/files/目录下面
 
try{
AssetManager assetManager = context.getResources().getAssets();
InputStream in = assetManager.open(JAR_NAME);
if(in == null) {
return;
}
intlength = in.available();
bytefileByte[] = newbyte[length];
in.read(fileByte,0, fileByte.length);
in.close();
OutputStream out = context.openFileOutput(JAR_NAME,
Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
out.write(fileByte);
out.close();
}catch(Exception e) {
e.printStackTrace();
}


在有root权限的情况下,可以在shell中执行该jar包来进行安装和卸载:
 
String exportClassPath = "export CLASSPATH=/data/data/"
+ context.getPackageName() + "/files/installpackagejar.jar";

 
String INSTALL_ACTION_CMD = " exec app_process /system/bin packageName.StartMain install ";


 
publicboolean installApp(String path) {
File temp = newFile(path);
if(!temp.exists())
returnfalse;
String cmd[] = { exportClassPath, INSTALL_ACTION_CMD + path };
try{
consoleExec(cmd);
}catch(IOException e) {
e.printStackTrace();
returnfalse;
}
returntrue;
}


 
privatevoid consoleExec(String[] cmd) throwsIOException {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = newDataOutputStream(process.getOutputStream());
for(inti = 0; i < cmd.length; i++) {
os.writeBytes(cmd<i> + "\n");
}
os.writeBytes("exit\n");
os.flush();
os.close();
}

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:872073次
    • 积分:9294
    • 等级:
    • 排名:第1958名
    • 原创:124篇
    • 转载:96篇
    • 译文:25篇
    • 评论:91条
    联系方式

    Author: KARL-Dujinyang



    Offer QQ: 1875125470


    BAT QQ: 309933706



    2011 Q群: 37*******


    2012 Q群: 20*******


    2013 Q群: 58*******


    2014 Q群: 39*******


    2015 Q群: 41*******


    2016 Q群: 246231638



    博客专栏