卸载apk默默无异常---(java.lang.AbstractMethodError:抽象方法未实现)

时间:2012-09-20 02:50:15

标签: android packages uninstall

我正在开发一个支持默默卸载其他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);
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

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

答案 2 :(得分: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
        }
    }

}