Forum Xamarin Xamarin.Android

C# equivalent of this Java code in Xamarin

Hi all,

I'm trying to implement the Java code from this blog-post:

PackageManager pm = getPackageManager();

Method getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);

getPackageSizeInfo.invoke(pm, "", new IPackageStatsObserver.Stub() {

    public void onGetStatsCompleted(PackageStats pStats, boolean succeeded)
        throws RemoteException {

        Log.i(TAG, "codeSize: " + pStats.codeSize);

I've succesfully gotten the method using Class.GetMethod, Class.FromType and Class.ForName methods.

var getPackageSizeInfo = context.PackageManager.Class.GetMethod("getPackageSizeInfo",
    Java.Lang.Class.FromType(typeof (Java.Lang.String)),

var ipso = new MyPackageStatsObserver();

var info = getPackageSizeInfo.Invoke(context.PackageManager, new Java.Lang.String(p.PackageName), ipso);

However I'm stuck at the IPackageStatsObserver.Stub part. This is a static abstract class which is part of the IPackageStatsObserver Java interface. How can I create an implementation of this class? I've tried something along the lines of:

[Register("android/content/pm/IPackageStatsObserver", DoNotGenerateAcw = true)]
public interface IPackageStatsObserver : IJavaObject

[Register("android/content/pm/IPackageStatsObserver$Stub", DoNotGenerateAcw = true)]
public abstract class PackageStatsObserverStub : Object, IPackageStatsObserver

public class MyPackageStatsObserver : PackageStatsObserverStub
    [Register("onGetStatsCompleted", "(Landroid/content/pm/PackageStats;Ljava/lang/String;)V", "")]
    public void OnGetStatsCompleted(PackageStats pStats, bool succeeded)

But this produces the following compile error: error: package does not exist

The generated Java-code seems legit:

public class MyPackageStatsObserver


  • DevillersDevillers USMember

    Managed to get a working solution with generated code from a Android Interface Definition Language based file of IPackageStatsObserver wrapped with a IInvocationHandler-based proxy.

Sign In or Register to comment.