1.首先要添加权限,在AndroidManifest.xml添加如下权限:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
2.在launcher的启动Activity的onCreate()方法中添加如下内容:
if (hasApk("com.tencent.android.qqdownloader")) {// install apk,
//安装了apk
}else { //no install apk
ThreadPoolManager.getInstance().execute(new Runnable() {
@Override
public void run() {
String oldPath = "/oem/etc/app/test.apk"; //原apk 所在位置
String newPath = getApplicationContext().getFilesDir().getAbsolutePath() + "/test.apk"; // 安装apk所在位置
//String newPath = "/data/data/com.example.contentprovidertest/files/test.apk";
deleteExistsFile(newPath);
copyFile(oldPath , newPath);
boolean installApp = installApp(newPath,getPackageName());
deleteExistsFile(newPath);
}
});
}
private void deleteExistsFile(String path){