3

I am using following code to find weather this user is premium user or not. Weather the user has purchase in app billing or not. But when i call this method isPremium(). it gives right result first time only but when i do it after first time, it always give wrong result. The mService variable of IInAppBillingService is null. Can some one tell me what could be reason of it. The code is as below.

public boolean isPremium() {
        boolean mIsPremium = false;
        Log.d(TAG, "::isPremium:" + "mService:"+mService);
        if(mService==null){
            return mIsPremium;
        }

        try {
            Bundle ownedItems = mService.getPurchases(3, getPackageName(),
                    "inapp", null);
            if (ownedItems != null) {
                int response = ownedItems.getInt("RESPONSE_CODE");
                if (response == 0) {
                    ArrayList ownedSkus = ownedItems
                            .getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
                    ArrayList purchaseDataList = ownedItems
                            .getStringArrayList("INAPP_PURCHASE_DATA_LIST");
                    ArrayList signatureList = ownedItems
                            .getStringArrayList("INAPP_DATA_SIGNATURE");
                    String continuationToken = ownedItems
                            .getString("INAPP_CONTINUATION_TOKEN");

                    for (int i = 0; i < purchaseDataList.size(); ++i) {
                        String signature = null;
                        String purchaseData = (String) purchaseDataList.get(i);
                        if (signatureList != null)
                            signature = (String) signatureList.get(i);
                        String sku = (String) ownedSkus.get(i);
                        Log.d(TAG, "::isPremium:" + "sku:" + sku);
                        Log.d(TAG, "::isPremium:" + "purchaseData:"
                                + purchaseData);
                        Log.d(TAG, "::isPremium:" + "signature:" + signature);
                        if (sku.equalsIgnoreCase(SKU_PREMIUM)) {
                            Log.d(TAG, "::isPremium:" + "Already Purchased");
                            return true;
                        }

                        // do something with this purchase information
                        // e.g. display the updated list of products owned by
                        // user
                    }

                    // if continuationToken != null, call getPurchases again
                    // and pass in the token to retrieve more items
                }
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return mIsPremium;
    }

The following is code to initialize service. where every time i come to service. i only get "onServiceConnected " log. and never got log ":onServiceDisconnected:"

ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "::onServiceDisconnected:" + "");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "::onServiceConnected:" + "");
            mService = IInAppBillingService.Stub.asInterface(service);

        }
    };

So can some one give me idea what could be reason of mService becoming null after first time ? Should we only call it 1 time only ? is my service getting disconnected ? But i could not see it in my log.

4

1 回答 1

1

根据你对你的问题所说的:

但是当我调用这个方法时 isPremium()。它给出了正确的结果,但是当我第一次这样做时。

您的 mService 第一次可能未绑定,但在第二次调用时。当你绑定一个服务时,它需要一些时间来建立,这可能就是为什么你第二次访问它时你的 mService 有一个值,isPremium 也是如此。

如果您查看此链接(扩展 Binder 类),该示例使用布尔值mBound来查看服务是否已绑定,如下所示

在服务端

public class IInAppBillingService extends Service {
    // Binder given to clients
    private final IBinder mBinder = new LocalBinder();


    public class LocalBinder extends Binder {
        IInAppBillingService getService() {
        // Return this instance of IInAppBillingService so clients can call public methods
        return IInAppBillingService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    /** method for clients */
    public Bundle getPurchases(...) {
      //...
    }
}

在活动方面

public class BindingActivity extends Activity {
    IInAppBillingService mService;
    boolean mBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Bind to IInAppBillingService
        Intent intent = new Intent(this, IInAppBillingService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Unbind from the service
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }


    public void doStuff(View v) {
        if (mBound) {
            if(isPremium()){
                 //...
            }
        }
    }

    /** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
             IBinder service) {
            // We've bound to IInAppBillingService, cast the IBinder and get IInAppBillingServiceinstance
            //The connection has been established
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            //You can use a boolean or you can call to isPremium if you are going to use it once, depends of your scenario
            mBound = true;
        }

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

您只发布了 isPremium 功能,但没有发布您初始化 mService 的方式。如果您没有在上面的链接上扩展 binder 类,您可以找到不同的方法,一种应该与您使用的相同。

于 2013-04-29T09:20:23.517 回答