Created
May 18, 2015 11:59
-
-
Save Jiezhi/b70b0b5ddb4616c42196 to your computer and use it in GitHub Desktop.
install apk just in silence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Don't forget to add <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> in AndroidManifest.xml | |
* I bind this function with Button | |
* referenced with http://www.trinea.cn/android/android-install-silent/ | |
* @param view | |
*/ | |
public void installAPK(View view) { | |
// assume there is a apk file in /sdcard/ path | |
String filePath = "/sdcard/RootExplorer_99.apk"; | |
String[] args = {"pm", "install", "-r", filePath}; | |
ProcessBuilder processBuilder = new ProcessBuilder(args); | |
Process process = null; | |
BufferedReader successResult = null; | |
BufferedReader errorResult = null; | |
StringBuilder successMsg = new StringBuilder(); | |
StringBuilder errorMsg = new StringBuilder(); | |
try { | |
process = processBuilder.start(); | |
successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); | |
String s; | |
while ((s = successResult.readLine()) != null) { | |
successMsg.append(s); | |
} | |
while ((s = errorResult.readLine()) != null) { | |
errorMsg.append(s); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
try { | |
if (successResult != null) | |
successResult.close(); | |
if (errorResult != null) | |
errorResult.close(); | |
if (process != null) | |
process.destroy(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (successMsg.toString().contains("success") || successMsg.toString().contains("Success")){ | |
Toast.makeText(getApplication(), "Install APK success:)", Toast.LENGTH_LONG).show(); | |
} | |
Log.d("SilentInstall", "success msg:" + successMsg); | |
Log.d("SilentInstall", "error msg:" + errorMsg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment