我正在使用以下代码查找此用户是否为高级用户的天气。用户在应用计费中购买的天气与否。但是当我称这种方法为isPremium()时。它只在第一次给出了正确的结果,但是当我第一次做到这一点时,它总是给出错误的结果。 IInAppBillingService的mService变量为null。有人可以告诉我它可能是什么原因。代码如下。
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;
}
以下是初始化服务的代码。我每次来服务的地方。我只获得“onServiceConnected”日志。并且从未得到日志“: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);
}
};
那么有人可以让我知道mService在第一次出现后会变成空的原因是什么?我们应该只拨打一次电话吗?我的服务断开了吗?但我在日志中看不到它。
答案 0 :(得分:1)
根据你在问题上的说法:
但是当我称这个方法为isPremium()时。它给出了正确的结果,但是当我第一次这样做的时候。
你的mService第一次可能没有绑定,但它是第二次调用。 当你绑定一个服务时,它需要一些时间来建立,这可能是你第二次访问它时你的mService有一个值,isPremium也是如此。
如果查看 this link(Extending the Binder class),该示例使用布尔值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类,你可以找到不同的方法,一个应该与你使用的相同。