gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 19:12:00 24 4
gpt4 key购买 nike

我的应用有系统权限。它将在固件中,现在位于/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。但我不知道该怎么做(我没有这样的类(class))。

谢谢

最佳答案

这是我的做法:

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

另外不要忘记添加 list 权限:

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

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

关于android - 使用系统权限静默卸载应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com