使用系统权限静默卸载应用程序 [英] uninstall app silently with system privileges

查看:35
本文介绍了使用系统权限静默卸载应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我可以通过这篇文章静默安装应用

以编程方式安装/卸载 APK(PackageManager 与 Intents)

有效的示例应用

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

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

public ApplicationManager(Context context) 抛出 SecurityException, NoSuchMethodException {观察者 = 新的 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);卸载方法 = pm.getClass().getMethod("deletePackage", uninstalltypes);}public void uninstallPackage(String packagename) 抛出 IllegalArgumentException、IllegalAccessException、InvocationTargetException {卸载方法.调用(下午,新对象[] {包名,观察者,0});}public void installPackage(Uri apkFile) 抛出 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.IPPackageDeleteObserver, int)

参数没问题.似乎我应该指定 DeletePackageObserver 类而不是 InstallPackageObserver.但我不知道该怎么做(我没有这样的课程).

谢谢

解决方案

我是这样做的:

ApplicationManager.java(更改部分):

私有PackageInstallObserver观察者;私有 PackageDeleteObserver 观察者删除;私人包管理器下午;私有方法方法;私有方法卸载方法;类 PackageDeleteObserver 扩展了 IPackageDeleteObserver.Stub {public void packageDeleted(String packageName, int returnCode) 抛出 RemoteException {/*if (onInstalledPackaged != null) {onInstalledPackaged.packageInstalled(packageName, returnCode);}*/}}公共 ApplicationManager(上下文上下文)抛出 SecurityException,NoSuchMethodException {观察者 = 新的 PackageInstallObserver();观察者删除 = 新的 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);卸载方法 = pm.getClass().getMethod("deletePackage", uninstalltypes);}public void uninstallPackage(String packagename) 抛出 IllegalArgumentException、IllegalAccessException、InvocationTargetException {uninstallmethod.invoke(pm, new Object[] {packagename,observerdelete, 0});}

PackageDeleteObserver.java(在 android.content.pm 中):

package android.content.pm;公共接口 IPackageDeleteObserver 扩展 android.os.IInterface {公共抽象静态类 Stub 扩展 android.os.Binder 实现 android.content.pm.IPackageDeleteObserver {公共存根(){throw new RuntimeException("Stub!");}公共静态 android.content.pm.IPPackageDeleteObserver asInterface(android.os.IBinder obj) {throw new RuntimeException("Stub!");}公共 android.os.IBinder asBinder() {throw new RuntimeException("Stub!");}public boolean onTransact(int 代码,android.os.Parcel 数据,android.os.Parcel 回复,int 标志)抛出 android.os.RemoteException {throw new RuntimeException("Stub!");}}public abstract void packageDeleted(java.lang.String packageName, int returnCode)抛出 android.os.RemoteException;}

另外不要忘记添加清单权限:

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

My app have system privileges. It will be inside firmware, now it's located at /system/app

I was able to install apps silently with this post

install / uninstall APKs programmatically (PackageManager vs Intents)

example app that works

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

But I still can't uninstall apps the same way. I tried to use reflection like as in the installation example.

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

I have added uninstallPackage method and edited ApplicationManager method. Still cant get this working.

When I run it I get method not found (on the invoke "deletePackage" line).

Here is not working project with my changes: https://dl.dropbox.com/u/1928109/InstallInBackgroundSample.zip

Here is an description of function: http://www.androidjavadoc.com/1.0_r1_src/android/content/pm/PackageManager.html#deletePackage(java.lang.String, android.content.pm.IPackageDeleteObserver, int)

Parameters are ok. Seems like I should specify DeletePackageObserver class instead of InstallPackageObserver. But I don't know how to do that (I don't have such class).

Thanks

解决方案

Here is how I did it:

ApplicationManager.java (changed part):

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

Also dont forget to add permission to manifest:

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

Working sample project (apk need to be placed in "/system/app" path on device): http://www.mediafire.com/file/no4buw54ed6vuzo/DeleteInBackgroundSample.zip

这篇关于使用系统权限静默卸载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆