Example usage for android.content ServiceConnection ServiceConnection

List of usage examples for android.content ServiceConnection ServiceConnection

Introduction

In this page you can find the example usage for android.content ServiceConnection ServiceConnection.

Prototype

ServiceConnection

Source Link

Usage

From source file:edu.asu.cse535.assignment3.MainActivity.java

public void classifyActivity(View v) {
    final Handler testHandler = new Handler() {
        @Override/*from ww  w.  ja va2  s .c om*/
        public void handleMessage(Message msg) {
            Log.w(this.getClass().getSimpleName(), "Message received and stopping service");

            Bundle b = msg.getData();
            ActivityData activityData = (ActivityData) b.getSerializable("ActivityData");

            notifyCompletion();
            stopService(testIntent);
            unbindService(testServiceConnection);

            AndroidLibsvmClassifier androidLibsvmClassifier = new AndroidLibsvmClassifier();
            String activity = androidLibsvmClassifier.classify(activityData);
            toastActivity(activity);
        }
    };

    testIntent = new Intent(MainActivity.this.getBaseContext(), AccIntentService.class);
    testIntent.putExtra("activity", Constants.CLASSIFY);
    testServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            AccIntentService testAaccIntentService = ((AccIntentService.LocalBinder) service).getInstance();
            testAaccIntentService.setHandler(testHandler);
            Log.w(this.getClass().getSimpleName(), "Test activity is connected to service");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    startService(testIntent);

    bindService(testIntent, testServiceConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.annahid.libs.artenus.internal.unified.IabHelper.java

/**
 * Starts the setup process. This will start up the setup process asynchronously.
 * You will be notified through the listener when the setup process is complete.
 * This method is safe to call from a UI thread.
 *
 * @param listener The listener to notify when the setup process is complete.
 *///from  w  w  w.j a  va  2  s .co  m
public void startSetup(final OnIabSetupFinishedListener listener) {
    // If already set up, can't do it again.
    checkNotDisposed();
    if (mSetupDone)
        throw new IllegalStateException("IAB helper is already set up.");

    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mDisposed)
                return;
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(
                                new IabResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);

                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    logDebug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent(getServiceAction());
    serviceIntent.setPackage(getServiceNamespace());
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}

From source file:org.kontalk.billing.GoogleBillingService.java

/**
 * Starts the setup process. This will start up the setup process asynchronously.
 * You will be notified through the listener when the setup process is complete.
 * This method is safe to call from a UI thread.
 *
 * @param listener The listener to notify when the setup process is complete.
 *///www.  j ava 2 s.c  o m
public void startSetup(final OnBillingSetupFinishedListener listener) {
    // If already set up, can't do it again.
    checkNotDisposed();
    if (mSetupDone)
        throw new IllegalStateException("IAB helper is already set up.");

    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mDisposed)
                return;
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onSetupFinished(
                                new BillingResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);

                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    logDebug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onSetupFinished(new BillingResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onSetupFinished(new BillingResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");

    List<ResolveInfo> services = mContext.getPackageManager().queryIntentServices(serviceIntent, 0);
    if (services != null && !services.isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onSetupFinished(new BillingResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}

From source file:com.hexypixel.hexyplugin.IabHelper.java

public void startSetup(final OnIabSetupFinishedListener listener, int market) {
    // If already set up, can't do it again.
    checkNotDisposed();// w  w  w  .ja va 2s.  com
    if (mSetupDone)
        throw new IllegalStateException("IAB helper is already set up.");

    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mDisposed)
                return;
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(
                                new IabResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);

                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    logDebug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent(getMarketAction(market));
    serviceIntent.setPackage(getMarketNamespace(market));
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}

From source file:com.jiusg.mainscreenshow.ui.MSS.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    //      boolean findMethod = findActionBarTabsShowAtBottom();
    //      if (!findMethod) { // ?ActionBar?TabHost
    //         getWindow().setUiOptions(0);
    //      }/*from ww w  .j a v  a  2 s  .com*/
    //
    //      if(C.ISMEIZU){
    //         getWindow().setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
    //         setTheme(R.style.AppTheme_Meizu);
    //      }

    //      setContentView(R.layout.tab_content);

    setActionBarLayout(R.layout.actionbar_mss);
    setContentView(R.layout.activity_mss);
    sp_userinfo = getSharedPreferences("userinfo", 0);

    viewPager = (ViewPager) findViewById(R.id.mss_vp);
    setting = (ImageButton) findViewById(R.id.actionbar_setting);
    title = (TextView) findViewById(R.id.actionbar_title);
    event = (ImageButton) findViewById(R.id.actionbar_event);
    preview = (ImageButton) findViewById(R.id.actionbar_preview);

    setting.setOnClickListener(this);
    event.setOnClickListener(this);
    preview.setOnClickListener(this);

    list = new ArrayList<android.support.v4.app.Fragment>();
    mssEvent = new MSSEventFragment();
    mssPreview = new MssPreviewFragment();
    list.add(mssEvent);
    list.add(mssPreview);

    viewPager.setAdapter(new MSSFragmentAdapter(getSupportFragmentManager(), list));
    viewPager.setCurrentItem(0);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
            case 0:
                title.setText(R.string.app_name);
                event.setImageResource(R.drawable.ic_event_press);
                preview.setImageResource(R.drawable.ic_preview);
                break;
            case 1:
                title.setText(R.string.action_MSSPreview);
                event.setImageResource(R.drawable.ic_event);
                preview.setImageResource(R.drawable.ic_preview_press);
                break;
            default:
                break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    //      final TabHost tabHost = getTabHost();
    //      tabHost.addTab(tabHost
    //            .newTabSpec("event")
    //            .setIndicator(null,
    //                  getResources().getDrawable(R.drawable.ic_tab_event))
    //            .setContent(new Intent(this, MSSEvent.class)));
    //      tabHost.addTab(tabHost
    //            .newTabSpec("preview")
    //            .setIndicator(null,
    //                  getResources().getDrawable(R.drawable.ic_tab_preview))
    //            .setContent(new Intent(this, MSSPreview.class)));

    //      if (true) {
    //         getTabWidget().setVisibility(View.GONE);
    //
    //         final ActionBar bar = getActionBar();
    //         bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //
    //         bar.addTab(bar.newTab().setIcon(R.drawable.ic_tab_event)
    //               .setTabListener(this));
    //         bar.addTab(bar.newTab().setIcon(R.drawable.ic_tab_preview)
    //               .setTabListener(this));
    //
    //         // View????
    //         // bar.addTab(bar.newTab().setTabListener(this).setCustomView(R.layout.tab_widget_indicator).setTabListener(this));
    //
    //         // ActionBar Tab?
    //         SmartBarUtils.setActionBarTabsShowAtBottom(bar, true);
    //      }
    //
    //      SmartBarUtils.setBackIcon(getActionBar(),
    //            getResources().getDrawable(R.drawable.ic_back));

    // ?
    if (C.ISFREE) {
        sp_userinfo.edit().putString("UserVersionInfo", C.VERSION_FREE).commit();
    }

    // ?
    // sp_userinfo.edit().putString("UserVersionInfo", "OfficialVersion")
    // .commit();

    // ????
    if (!sp_userinfo.getString("UserVersionInfo", "").equals(C.VERSION_FREE)) {

        // ???????
        if (!sp_userinfo.getString("UserVersionInfo", "").equals("OfficialVersion")) {
            // ? ????
            if (mLicensingService == null) {

                Intent intent = new Intent();
                intent.setAction(ILicensingService.class.getName());
                bindService(intent, mLicenseConnection, Context.BIND_AUTO_CREATE);

            }

            hd = new LicenseHandler();
            Message msg = hd.obtainMessage();
            msg.obj = "License";
            hd.sendMessageDelayed(msg, 3000);
        }
    }

    mSc = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            mssservice = ((MSSService.MSSSBinder) service).getMSSService();

        }
    };
}

From source file:se.springworks.android.utils.iab.IabHelper.java

@Override
public void startSetup(final OnIabSetupFinishedListener listener) {
    // If already set up, can't do it again.
    checkNotDisposed();//w  w w.ja  v a 2s .  c  o m
    if (mSetupDone) {
        //         throw new IllegalStateException("IAB helper is already set up.");         
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
        }
        return;
    }

    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(
                                new IabResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are
                    // subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);

                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    logDebug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}

From source file:src.com.nustats.pacelogger.RecordingActivity.java

void setListener() {
    Intent rService = new Intent(this, RecordingService.class);
    ServiceConnection sc = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName name) {
        }//from  www  .  j ava2  s .  com

        public void onServiceConnected(ComponentName name, IBinder service) {
            IRecordService rs = (IRecordService) service;
            if (RecordingActivity.this.isRecording) {
                rs.resumeRecording();
            } else {
                rs.pauseRecording();
            }
            unbindService(this);
        }
    };
    // This should block until the onServiceConnected (above) completes, but doesn't
    bindService(rService, sc, Context.BIND_AUTO_CREATE);
}

From source file:com.zentri.otademo.OTAActivity.java

private void initServiceConnection() {
    mBound = false;/*w  w w . j  a  v a 2 s  .com*/
    mUnbinding = false;

    mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            Log.d(TAG, "Connected to service");
            cancelTimeout(mServiceConnectionTimeoutTask);

            ZentriOSBLEService.LocalBinder binder = (ZentriOSBLEService.LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            mService.setSettings(mSettings);

            if (!mService.isConnected()) {
                Log.d(TAG, "Not connected to device on service bind!");
                //service was destroyed and re-created, connection lost
                setStatus(R.string.error_connection_lost_message, R.color.zentri_red);
                showErrorDialog(R.string.error_connection_lost_message, FINISH_ON_CLOSE);
            } else {
                Log.d(TAG, "Connection to device found");
                //set window title to name of connected device
                OTAActivity.this.setTitle(mService.getDeviceName());
                setUIValues();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;

            if (!mUnbinding) {
                //connection lost unexpectedly
                setStatusError(getString(R.string.error_connection_lost_message));
            }

            mUnbinding = false;
        }
    };
}

From source file:com.kyokomi.example.iabhelpersampleapp.iab.util.IabHelper.java

/**
 * Starts the setup process. This will start up the setup process asynchronously.
 * You will be notified through the listener when the setup process is complete.
 * This method is safe to call from a UI thread.
 *
 * @param listener The listener to notify when the setup process is complete.
 *///from ww w  .  j av a2s.c  om
public void startSetup(final OnIabSetupFinishedListener listener) {
    // If already set up, can't do it again.
    checkNotDisposed();
    if (mSetupDone)
        throw new IllegalStateException("IAB helper is already set up.");

    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mDisposed)
                return;
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(
                                new IabResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);

                //                    // check for v3 subscriptions support
                //                    response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                //                    if (response == BILLING_RESPONSE_RESULT_OK) {
                //                        logDebug("Subscriptions AVAILABLE.");
                //                        mSubscriptionsSupported = true;
                //                    }
                //                    else {
                //                        logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                //                    }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}

From source file:com.welmo.andengine.utility.inappbilling.IabHelper.java

/**
 * Starts the setup process. This will start up the setup process asynchronously.
 * You will be notified through the listener when the setup process is complete.
 * This method is safe to call from a UI thread.
 *
 * @param listener The listener to notify when the setup process is complete.
 *///ww w  . j  av a2s  .c o m
public void startSetup(final IAPurchasing listener) {
    // If already set up, can't do it again.
    checkNotDisposed();
    if (mSetupDone)
        throw new IllegalStateException("IAB helper is already set up.");

    // Connection to IAB service
    logDebug("Starting in-app billing setup.");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            logDebug("Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (mDisposed)
                return;
            logDebug("Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = mContext.getPackageName();
            try {
                logDebug("Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != BILLING_RESPONSE_RESULT_OK) {
                    if (listener != null)
                        listener.onIabSetupFinished(
                                new IabResult(response, "Error checking for billing v3 support."));

                    // if in-app purchases aren't supported, neither are subscriptions.
                    mSubscriptionsSupported = false;
                    return;
                }
                logDebug("In-app billing version 3 supported for " + packageName);

                // check for v3 subscriptions support
                response = mService.isBillingSupported(3, packageName, ITEM_TYPE_SUBS);
                if (response == BILLING_RESPONSE_RESULT_OK) {
                    logDebug("Subscriptions AVAILABLE.");
                    mSubscriptionsSupported = true;
                } else {
                    logDebug("Subscriptions NOT AVAILABLE. Response: " + response);
                }

                mSetupDone = true;
            } catch (RemoteException e) {
                if (listener != null) {
                    listener.onIabSetupFinished(new IabResult(IABHELPER_REMOTE_EXCEPTION,
                            "RemoteException while setting up in-app billing."));
                }
                e.printStackTrace();
                return;
            }

            if (listener != null) {
                listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_OK, "Setup successful."));
            }
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
        // service available to handle that Intent
        mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        if (listener != null) {
            listener.onIabSetupFinished(new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                    "Billing service unavailable on device."));
        }
    }
}