android - uninstall app silently with system privileges -
my app have system privileges. inside firmware, it's located @ /system/app
i able install apps silently post
install / uninstall apks programmatically (packagemanager vs intents)
example app works
http://paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/
but still can't uninstall apps same way. tried use reflection in 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 , edited applicationmanager method. still cant working.
when run method not found (on invoke "deletepackage" line).
here not working project changes: https://dl.dropbox.com/u/1928109/installinbackgroundsample.zip
here 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 ok. seems should specify deletepackageobserver class instead of installpackageobserver. don't know how (i don't have such class).
thanks
here how 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 add permission manifest:
<uses-permission android:name="android.permission.delete_packages"/>
working sample project (apk need placed in "/system/app" path on device): http://www.mediafire.com/file/no4buw54ed6vuzo/deleteinbackgroundsample.zip
Comments
Post a Comment