Android 中静默安装实现详解编程语言

静默安装原理:

    1.需要获取root的操作权限

    2.通过命令式的方式直接进行安装APK。在使用 Android Studio debug安装的时候可以看到控制台上的命令

  
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.util.Log; 
  
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.IOException; 
import java.io.OutputStream; 
  
/** 
 * <p>名称:com.singno.VersionManager</p> 
 * <p>描述:</p> 
 * 
 
 *  APK版本管理器 
 *  版本检查,版本更新等 
 * 

*
* @author 鲍建明
* @version 2.1.0
* @date 2015/4/30/16:28
*/
public class VersionManager {

private static final String TAG = VersionManager.class.getName();

private Context context;

public VersionManager(Context context){
this.context = context;
}

/**
* 检查版本号是否相同
* @param versionCode
* @return
*/
public boolean isSameVersion(int versionCode){
return getCurrentVersion() != versionCode ? Boolean.FALSE : Boolean.TRUE;
}

/**
* 静默安装,安装之前必须要获取到ROOT权限
* 原理:1.先获取到ROOT权限
* 2.在通过命令的方式直接安装APK
* @return
*/
public boolean silenceInstall(File file){
Process process = null;
OutputStream out = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeBytes("chmod 777 " + file.getPath() + "/n");
dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " + file.getPath());
// 提交命令
dataOutputStream.flush();
int value = process.waitFor();
if( value == 0){
return Boolean.TRUE;
}
return Boolean.FALSE;

} catch (Exception e) {
e.printStackTrace();
return Boolean.FALSE;
}finally{
try {
if( dataOutputStream != null ){
dataOutputStream.close();
}
if( out != null ){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 普通的安装应用方式
* @param file 安装包文件
*/
public void installApk(File file){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + file.toString()), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(i);
}

/**
* 获取服务端中的版本号
* 这个自行完成
* @return
*/
public int getHttpVersion(){
return 0;
}

/**
* 获取当前APK的版本号
* @return 当前APK的版本号
*/
public int getCurrentVersion(){
try {
return this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "获取版本号失败");
return 0;
}
}

/**
* 下载APK
*/
public void downApk(){
new Thread(new DownApk()).start();
}

/**
* 显示下载进度提示框
*/
private void showDownloadDialog(){

}

/**
* 显示软件更新提示对话框
*/
private void showNoticeDialog(){

}

/**
* 下载APk的类
*/
class DownApk implements Runnable{

@Override
public void run() {

}
}

}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10386.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论