我的应用有系统权限。 它将在固件内部,现在它位于/ system / app
我能用这篇文章静静地安装应用程序
以编程方式安装/卸载APK(PackageManager vs Intents)
有用的示例应用程序
http://paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/
但我仍然无法以相同的方式卸载应用程序。 我尝试使用像安装示例中的reflection。
public ApplicationManager(Context context) throws SecurityException, NoSuchMethodException { observer = new PackageInstallObserver(); pm = context.getPackageManager(); Class[] types = new Class[] {Uri.class, IPackageInstallObserver.class, int.class, String.class}; Class[] uninstalltypes = new Class[] {String.class, IPackageInstallObserver.class, int.class}; method = pm.getClass().getMethod("installPackage", types); uninstallmethod = pm.getClass().getMethod("deletePackage", uninstalltypes); } public void uninstallPackage(String packagename) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { uninstallmethod.invoke(pm, new Object[] {packagename, observer, 0}); } public void installPackage(Uri apkFile) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { method.invoke(pm, new Object[] {apkFile, observer, INSTALL_REPLACE_EXISTING, null}); }
我添加了uninstallPackage方法并编辑了ApplicationManager方法。 仍然无法让这个工作。
当我运行它时,我找不到方法(在调用“deletePackage”行)。
这里不适用于我的更改: https : //dl.dropbox.com/u/1928109/InstallInBackgroundSample.zip
以下是function说明: http ://www.androidjavadoc.com/1.0_r1_src/android/content/pm/PackageManager.html#deletePackage(java.lang.String,android.content.pm.IPackageDeleteObserver,int)
参数还可以。 好像我应该指定DeletePackageObserver类而不是InstallPackageObserver。 但我不知道该怎么做(我没有这样的课)。
谢谢
我是这样做的:
ApplicationManager.java(已更改部分):
private PackageInstallObserver observer; private PackageDeleteObserver observerdelete; private PackageManager pm; private Method method; private Method uninstallmethod; class PackageDeleteObserver extends IPackageDeleteObserver.Stub { public void packageDeleted(String packageName, int returnCode) throws RemoteException { /*if (onInstalledPackaged != null) { onInstalledPackaged.packageInstalled(packageName, returnCode); }*/ } } public ApplicationManager(Context context) throws SecurityException, NoSuchMethodException { observer = new PackageInstallObserver(); observerdelete = new PackageDeleteObserver(); pm = context.getPackageManager(); Class>[] types = new Class[] {Uri.class, IPackageInstallObserver.class, int.class, String.class}; Class>[] uninstalltypes = new Class[] {String.class, IPackageDeleteObserver.class, int.class}; method = pm.getClass().getMethod("installPackage", types); uninstallmethod = pm.getClass().getMethod("deletePackage", uninstalltypes); } public void uninstallPackage(String packagename) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { uninstallmethod.invoke(pm, new Object[] {packagename, observerdelete, 0}); }
PackageDeleteObserver.java(在android.content.pm中):
package android.content.pm; public interface IPackageDeleteObserver extends android.os.IInterface { public abstract static class Stub extends android.os.Binder implements android.content.pm.IPackageDeleteObserver { public Stub() { throw new RuntimeException("Stub!"); } public static android.content.pm.IPackageDeleteObserver asInterface(android.os.IBinder obj) { throw new RuntimeException("Stub!"); } public android.os.IBinder asBinder() { throw new RuntimeException("Stub!"); } public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { throw new RuntimeException("Stub!"); } } public abstract void packageDeleted(java.lang.String packageName, int returnCode) throws android.os.RemoteException; }
也不要忘记添加清单的权限 :
工作示例项目 (apk需要放在设备上的“/ system / app”路径中) : http : //www.mediafire.com/file/no4buw54ed6vuzo/DeleteInBackgroundSample.zip
这是方法的定义方式:
public abstract void deletePackage( String packageName, IPackageDeleteObserver observer, int flags);
要使用reflection调用它,您需要以下内容:
Class>[] uninstalltypes = new Class[] {String.class, IPackageDeleteObserver.class, int.class}; uninstallmethod = pm.getClass().getMethod("deletePackage", uninstalltypes);
注意观察者的types。
如果您将应用程序内置到系统映像中,并且您正在使用内部API,那么您也可以停止假装,就像您是第三方应用程序并链接到SDK一样。 构建完整平台.jar并直接使用这些API。 无论如何,您想要这样做,因为这些是私有API,因此它们可以并且确实会发生变化。 你想要反对实际声明它们的内容,所以如果它们确实改变了,你将在构建期间捕获它。
在android 2.3.x中,接口IPackageDeleteObserver与方法packageDeleted不同。