private void install(File file) {
Uri data = Uri.fromFile(file);
String type = "application/vnd.android.package-archive";
File sourceFile = new File(data.getPath());
try {
final String archiveFilePath = sourceFile.getAbsolutePath();
Class<?> claPackageParser = Class.forName("android.content.pm.PackageParser");
Constructor<?> c = claPackageParser.getConstructor(String.class);
c.setAccessible(true);
Object packageParser = c.newInstance(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
Class<?> claPackage = Class.forName("android.content.pm.PackageParser$Package");
Method parsePackage = claPackageParser.getDeclaredMethod("parsePackage", File.class,
String.class, DisplayMetrics.class, int.class);
parsePackage.setAccessible(true);
Object mPkgInfo = parsePackage.invoke(packageParser, sourceFile, archiveFilePath,
metrics, 0);
Field field = mPkgInfo.getClass().getField("applicationInfo");
field.setAccessible(true);
ApplicationInfo mAppInfo = (ApplicationInfo)field.get(mPkgInfo);
String scheme = data.getScheme();
PackageManager pm = getPackageManager();
{
int installFlags = 0;
try {
PackageInfo pi = pm.getPackageInfo(mAppInfo.packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null) {
installFlags |= 0x00000002;
}
} catch (NameNotFoundException e) {
}
// PackageUtil.AppSnippet as = PackageUtil.getAppSnippet(this,
// mAppInfo, sourceFile);
// mLabel = as.label;
// PackageUtil.initSnippetForNewApp(this, as, R.id.app_snippet);
String installerPackageName = "com.jike.android.renrenso";
// PackageInstallObserver observer = new
// PackageInstallObserver();
Class<PackageManager> clazz = PackageManager.class;
Method method = clazz.getDeclaredMethod("installPackage", android.net.Uri.class,
Class.forName("android.content.pm.IPackageInstallObserver"), int.class,
String.class);
textView.append("\n333333333");
method.setAccessible(true);
textView.append("\npm:" + pm);
PackageInstallObserver observer = new PackageInstallObserver();
method.invoke(pm, data, observer, installFlags, installerPackageName);
}
} catch (Exception e) {
e.printStackTrace();
textView.append("\neeee:" + e.getMessage());
StackTraceElement[] ss = e.getStackTrace();
for (StackTraceElement s : ss) {
textView.append("\n" + s);
}
}
}
class PackageInstallObserver extends android.content.pm.IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
Toast.makeText(TestInstallActivity.this, "fdsaf", Toast.LENGTH_LONG).show();
}
}
Uri data = Uri.fromFile(file);
String type = "application/vnd.android.package-archive";
File sourceFile = new File(data.getPath());
try {
final String archiveFilePath = sourceFile.getAbsolutePath();
Class<?> claPackageParser = Class.forName("android.content.pm.PackageParser");
Constructor<?> c = claPackageParser.getConstructor(String.class);
c.setAccessible(true);
Object packageParser = c.newInstance(archiveFilePath);
DisplayMetrics metrics = new DisplayMetrics();
metrics.setToDefaults();
Class<?> claPackage = Class.forName("android.content.pm.PackageParser$Package");
Method parsePackage = claPackageParser.getDeclaredMethod("parsePackage", File.class,
String.class, DisplayMetrics.class, int.class);
parsePackage.setAccessible(true);
Object mPkgInfo = parsePackage.invoke(packageParser, sourceFile, archiveFilePath,
metrics, 0);
Field field = mPkgInfo.getClass().getField("applicationInfo");
field.setAccessible(true);
ApplicationInfo mAppInfo = (ApplicationInfo)field.get(mPkgInfo);
String scheme = data.getScheme();
PackageManager pm = getPackageManager();
{
int installFlags = 0;
try {
PackageInfo pi = pm.getPackageInfo(mAppInfo.packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null) {
installFlags |= 0x00000002;
}
} catch (NameNotFoundException e) {
}
// PackageUtil.AppSnippet as = PackageUtil.getAppSnippet(this,
// mAppInfo, sourceFile);
// mLabel = as.label;
// PackageUtil.initSnippetForNewApp(this, as, R.id.app_snippet);
String installerPackageName = "com.jike.android.renrenso";
// PackageInstallObserver observer = new
// PackageInstallObserver();
Class<PackageManager> clazz = PackageManager.class;
Method method = clazz.getDeclaredMethod("installPackage", android.net.Uri.class,
Class.forName("android.content.pm.IPackageInstallObserver"), int.class,
String.class);
textView.append("\n333333333");
method.setAccessible(true);
textView.append("\npm:" + pm);
PackageInstallObserver observer = new PackageInstallObserver();
method.invoke(pm, data, observer, installFlags, installerPackageName);
}
} catch (Exception e) {
e.printStackTrace();
textView.append("\neeee:" + e.getMessage());
StackTraceElement[] ss = e.getStackTrace();
for (StackTraceElement s : ss) {
textView.append("\n" + s);
}
}
}
class PackageInstallObserver extends android.content.pm.IPackageInstallObserver.Stub {
public void packageInstalled(String packageName, int returnCode) {
Toast.makeText(TestInstallActivity.this, "fdsaf", Toast.LENGTH_LONG).show();
}
}