2

我正在开发一个支持静默卸载其他 apk 的应用程序。我的设备已植根,应用程序位于 /system/app 下,通过反射我可以使用 deletePackage 方法来实现这一点。但是在deletePackage方法的实现中,出现了异常。异常信息如下:

09-20 10:16:02.242: E/JavaBinder(1943): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
09-20 10:16:02.242: E/JavaBinder(1943): java.lang.AbstractMethodError: abstract method not implemented
09-20 10:16:02.242: E/JavaBinder(1943):     at android.content.pm.IPackageDeleteObserver$Stub.packageDeleted(IPackageDeleteObserver.java)
09-20 10:16:02.242: E/JavaBinder(1943):     at android.content.pm.IPackageDeleteObserver$Stub.onTransact(IPackageDeleteObserver.java:57)
09-20 10:16:02.242: E/JavaBinder(1943):     at android.os.Binder.execTransact(Binder.java:338)
09-20 10:16:02.242: E/JavaBinder(1943):     at dalvik.system.NativeStart.run(Native Method)
09-20 10:16:02.242: W/dalvikvm(1943): threadid=9: thread exiting with uncaught exception (group=0x40a461f8)
09-20 10:16:02.242: E/AndroidRuntime(1943): FATAL EXCEPTION: Binder Thread #1
09-20 10:16:02.242: E/AndroidRuntime(1943): java.lang.AbstractMethodError: abstract method not implemented
09-20 10:16:02.242: E/AndroidRuntime(1943):     at android.content.pm.IPackageDeleteObserver$Stub.packageDeleted(IPackageDeleteObserver.java)
09-20 10:16:02.242: E/AndroidRuntime(1943):     at android.content.pm.IPackageDeleteObserver$Stub.onTransact(IPackageDeleteObserver.java:57)
09-20 10:16:02.242: E/AndroidRuntime(1943):     at android.os.Binder.execTransact(Binder.java:338)
09-20 10:16:02.242: E/AndroidRuntime(1943):     at dalvik.system.NativeStart.run(Native Method)

相关代码:

public void onBackgroundUninstallClick(View v) {
    PackageDeleteObserver observer = new PackageDeleteObserver();
    if (mPackageManager != null/* && mUninstallPackage != null*/) {
        try {
            mPackageManager.deletePackage("com.example.android.imagedownloader", observer, 0);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } 
}

class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
    public void packageDeleted(boolean succeeded) throws RemoteException {
        Log.i(TAG, "packageDeleted");
        Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
        msg.arg1 = succeeded ? SUCCEEDED : FAILED;
        mHandler.sendMessage(msg);
    }
}

IPackageDeleteObserver.aidl code : 

package android.content.pm;

interface IPackageDeleteObserver {
    void packageDeleted(boolean flag);
}
4

3 回答 3

2

我发现该应用程序在 sdk 2.3.5 但 4.0 上运行良好。也许在 sdk 4.0 之后方法 packageDeleted 发生了变化。任何人都可以在 sdk 4.0 中提供 IPackageDeleteObserver.aidl 的源代码。

于 2012-09-20T08:16:56.157 回答
2
java.lang.AbstractMethodError: abstract method not implemented

某处缺少方法实现。

void packageDeleted(boolean flag);

与签名不符

public void packageDeleted(boolean succeeded) throws RemoteException

因为throws RemoteException

改成

interface IPackageDeleteObserver {
    void packageDeleted(boolean flag) throws RemoteException;
}
于 2012-09-20T02:54:23.950 回答
0

我在 android 2.3.3 和 android 4.0.3 模拟器上都试过了,而且效果很好。请注意,我已经删除了 @Override 注释。

我看到一些答案指示接口或抽象类来决定使用哪一个,具体取决于 SDK 版本,但这似乎也有效。

class PackageDeleteObserver extends IPackageDeleteObserver.Stub {

    PackageDeleteObserver( ) {
        // nothing to do here
    }

    // android > 4
    public void packageDeleted( String name, int status ) throws RemoteException {
        synchronized (this) {
            this.notifyAll();
            // do your thing
        }
    }

    // android < 4
    public void packageDeleted( boolean status ) throws RemoteException {
        synchronized (this) {
            this.notifyAll();
            // do your thing
        }
    }

}
于 2013-08-27T19:57:14.460 回答