Silent Installation principle:
1. Need to get root operation permissions
2. Install the apk directly by using the command-style method. You can see commands on the console when using the Android Studio Debug installation
Then go directly to the code:
/**<p> Project name:singno</p> * <p> package name:com.singno</p> * <p> File name:versionmanager.java</p> * <p> version information: 2.1.0</p> * <p> Date: 2015/4/30/16:28.</p> * Copyright (c) 2015singno-All rights reserved */package com.singno;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> Name:com.singno.versionmanager</p> * <p> Description: </P>&NBSP;*&NBSP;<PRE>&NBSP;*&NBSP;&NBSP;APK version Manager * version check, version update, etc. * </ pre> * * @author Bao Jianming * @version 2.1.0 * @date 2015/4/30/ 16:28 */public class versionmanager { &nbsP; private static final string tag = versionmanager.class.getname (); private context context; public versionmanager (Context context) { this.context = context; } /** * Check if the version number is the same * @param versionCode * @return */ public boolean issameversion (Int versioncode) { return getcurrentversion () != versionCode ? boolean.false : boolean.true; } /** * silent installation, you must obtain root privileges * rationale before installing: 1. Get root permission First * 2. In passingCommand the way to install apk * directly @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 ()); // Submit Command 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 ) { &nbsP;out.close (); } } catch (IOException e ) { E.printstacktrace (); } } } /** * General installation Application * @param file installation package Files */ 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); } /** * get the version number in the server * this self-completed * @return */ public int gethttpversion () { return 0; } /** * get the version number of the current APK * @return Version number of the current APK */ public Int getcurrentversion () { try { return this.context.getpackagemanager (). Getpackageinfo (This.context.getPackageName (), &Nbsp;0) .versioncode; } catch ( Packagemanager.namenotfoundexception e) { e.printstacktrace (); LOG.E (tag, "Get version number failed"); return 0; } } /** * Download apk */ public void downapk () { new thread (NEW&NBSP;DOWNAPK ()). Start ( ); } /** * Show Download progress alert box */ private void showdownloaddialog () { } /** * display the Software Update prompt dialog box */ private void shownoticedialog () { } /** * Download APK class */ class DownApk implements runnable{ @Override public void run () { } }}
Silent installation in Android