Android 中靜默安裝實現

me87re 9年前發布 | 5K 次閱讀 Java

靜默安裝原理:

    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>
  • <pre>
  • APK版本管理器
  • 版本檢查,版本更新等
  • </pre> *
  • @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() {

    }
}

}</pre>

 本文由用戶 me87re 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!