package com.duke.installtest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
public class MyActivity extends Activity {
private Button InstallBtn;
private EditText PathEt;
public String ApkPath;
public String TAG="Install!!!";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
InstallBtn=(Button)findViewById(R.id.button);
PathEt=(EditText)findViewById(R.id.editText);
InstallBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(PathEt.getText().toString().equals("")) return;
String Filename=PathEt.getText().toString();
ApkPath=Environment.getExternalStorageDirectory()+"/"+Filename;
Log.e(TAG,ApkPath);
Boolean result=slientInstall(ApkPath);
Log.e(TAG,result.toString());
}
});
//upgradeRootPermission(getPackageCodePath());
}
public static boolean upgradeRootPermission(String pkgCodePath) {
Process process = null;
DataOutputStream os = null;
try {
String cmd="chmod 777 " + pkgCodePath;
process = Runtime.getRuntime().exec("su"); //切换到root帐号
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(cmd + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
return true;
}
/**
* 系统权限安装 Path为要安装的APK的路径
* @param Path
* @return
*/
private String install(String Path)
{
String[] args = {
"pm", "install", "-r", Path
};
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1)
{
baos.write(read);
}
inIs = process.getInputStream();
while ((read = inIs.read()) != -1)
{
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
} catch (Exception e)
{
e.printStackTrace();
// TODO: handle exception
}
return result;
}
/**
* 静默安装 Path为APK路径
* @param Path
* @return
*/
public boolean slientInstall(String Path) {
boolean result = false;
Process process = null;
OutputStream out = null;
try {
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeBytes("chmod 777 " + Path + "\n");
dataOutputStream.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r " +
Path);
// 提交命令
dataOutputStream.flush();
// 关闭流操作
dataOutputStream.close();
out.close();
int value = process.waitFor();
// 代表成功
if (value == 0) {
result = true;
} else if (value == 1) { // 失败
result = false;
} else { // 未知情况
result = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 静默安装 Path为要安装的APK的路径
* @param Path
* @return
*/
public void NormalInstall(String Path){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse(Path),"application/vnd.android.package-archive");
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1、正常安装——有安装界面,由系统自带的packageinstaller.apk应用处理安装及卸载过程的界面。 2、系统权限下静默安装——无安装界面,其实也是Google Play的安装方式,当然也是有实现方法的。 3、ROOT下完全静默安装——无安装界面,目前主流的第三方应用市场安装的方式,也可通过终端命令实现
资源推荐
资源详情
资源评论
收起资源包目录
Android应用安装APK方法汇总源码 (113个子文件)
resources-debug.ap_ 50KB
resources-release.ap_ 50KB
resources-debug-test.ap_ 694B
app-debug.apk 57KB
app-debug-unaligned.apk 57KB
app-release-unaligned.apk 56KB
gradlew.bat 2KB
fileSnapshots.bin 64KB
taskArtifacts.bin 48KB
fileHashes.bin 23KB
outputFileStates.bin 20KB
MyActivity.class 5KB
MyActivity.class 3KB
MyActivity$1.class 2KB
MyActivity$1.class 2KB
BuildConfig.class 832B
BuildConfig.class 697B
R.class 697B
R.class 697B
R$string.class 478B
R$string.class 478B
R$dimen.class 467B
R$dimen.class 467B
R$id.class 461B
R$id.class 461B
R$drawable.class 413B
R$drawable.class 413B
R$layout.class 407B
R$layout.class 407B
R$style.class 401B
R$style.class 401B
R$menu.class 392B
R$menu.class 392B
R$attr.class 346B
R$attr.class 346B
hololike.css 3KB
classes.dex 8KB
classes.dex 6KB
.gitignore 87B
.gitignore 8B
build.gradle 554B
build.gradle 456B
settings.gradle 16B
gradlew 5KB
lint-results-release-fatal.html 903B
app.iml 6KB
InstallTest.iml 847B
gradle-wrapper.jar 49KB
MyActivity.java 6KB
R.java 1KB
R.java 1KB
BuildConfig.java 601B
BuildConfig.java 586B
BuildConfig.java 565B
ApplicationTest.java 363B
cache.properties.lock 17B
.name 11B
ic_launcher.png 19KB
ic_launcher.png 18KB
ic_launcher.png 18KB
ic_launcher.png 14KB
ic_launcher.png 14KB
ic_launcher.png 14KB
ic_launcher.png 9KB
ic_launcher.png 9KB
ic_launcher.png 9KB
ic_launcher.png 5KB
ic_launcher.png 5KB
ic_launcher.png 5KB
proguard-rules.pro 670B
gradle.properties 870B
local.properties 435B
gradle-wrapper.properties 229B
cache.properties 31B
dependency.store 5B
dependency.store 5B
dependency.store 5B
workspace.xml 56KB
merger.xml 2KB
merger.xml 2KB
AndroidManifest.xml 1KB
AndroidManifest.xml 1KB
AndroidManifest.xml 1KB
activity_my.xml 1KB
activity_my.xml 1KB
activity_my.xml 1KB
values.xml 796B
values.xml 796B
Project_Default.xml 746B
compiler.xml 734B
AndroidManifest.xml 651B
gradle.xml 512B
my.xml 416B
my.xml 416B
misc.xml 406B
modules.xml 371B
dimens.xml 364B
merger.xml 363B
my.xml 336B
merger.xml 274B
共 113 条
- 1
- 2
资源评论
- zw1578776722016-07-29资源很好,就是有点贵!
- 雪碧口味的可乐2017-10-12太贵了,还不是我想要的那种!!!!
弈风丶
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功