使用系统权限静默卸载应用程序

时间:2012-06-05 16:14:54

标签: android android-4.0-ice-cream-sandwich android-firmware

我的应用拥有系统权限。它将在内部固件中,现在它位于/ system / app

我能够使用这篇文章静默安装应用程序

install / uninstall APKs programmatically (PackageManager vs Intents)

有效的示例应用

http://paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/

但我仍然无法以相同的方式卸载应用程序。我尝试使用像安装示例中的反射。

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

以下是函数的说明:http://www.androidjavadoc.com/1.0_r1_src/android/content/pm/PackageManager.html#deletePackage(java.lang.String,android.content.pm.IPackageDeleteObserver,int)

参数还可以。好像我应该指定DeletePackageObserver类而不是InstallPackageObserver。但我不知道该怎么做(我没有这样的课)。

由于

4 个答案:

答案 0 :(得分:18)

我是这样做的:

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;
}

也不要忘记添加清单的权限:

<uses-permission android:name="android.permission.DELETE_PACKAGES"/>

工作示例项目(apk需要放在设备上的“/ system / app”路径中) http://www.mediafire.com/file/no4buw54ed6vuzo/DeleteInBackgroundSample.zip

答案 1 :(得分:7)

这是方法的定义方式:

   public abstract void deletePackage(
             String packageName, IPackageDeleteObserver observer, int flags);

要使用反射调用它,您需要以下内容:

Class<?>[] uninstalltypes = new Class[] {String.class, 
         IPackageDeleteObserver.class, int.class};
uninstallmethod = pm.getClass().getMethod("deletePackage", uninstalltypes);

注意观察者的类型。

答案 2 :(得分:4)

如果您的应用内置于系统映像中,并且您正在使用内部API,那么您也可以停止假装,就像您是第三方应用并链接SDK一样。构建完整平台.jar并直接使用这些API。无论如何,你想要这样做,因为这些是私有API,因此它们可以并且确实会发生变化。你想要反对实际声明它们的内容,所以如果它们确实发生了变化,你将在构建过程中发现它。

答案 3 :(得分:1)

在android 2.3.x中,接口IPackageDeleteObserver与方法packageDeleted不同。